// @ts-check

const fs = require("fs");
const dbHandler = require("./dbHandler");

/**
 * DB handler for specific database
 * ==============================================================================
 * @async
 * @param {object} params - Single object params
 * @param {string} params.queryString - SQL string
 * @param {string[]} [params.queryValuesArray] - Values Array
 * @param {string} [params.database] - Database name
 * @param {import("@/package-shared/types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
 * @returns {Promise<any>}
 */
module.exports = async function varDatabaseDbHandler({
    queryString,
    queryValuesArray,
    database,
    tableSchema,
}) {
    /**
     * Declare variables
     *
     * @description Declare "results" variable
     */
    let results;

    /**
     * Fetch from db
     *
     * @description Fetch data from db if no cache
     */
    try {
        if (
            queryString &&
            queryValuesArray &&
            Array.isArray(queryValuesArray) &&
            queryValuesArray[0]
        ) {
            results = await dbHandler({
                query: queryString,
                values: queryValuesArray,
                database,
            });
        } else {
            results = await dbHandler({
                query: queryString,
                database,
            });
        }

        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
    } catch (/** @type {any} */ error) {
        console.log("Shell Vardb Error =>", error.message);
    }

    /**
     * Return results
     *
     * @description Return results add to cache if "req" param is passed
     */
    return results;

    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////
};
