import sqlGenerator from "../functions/dsql/sql/sql-generator";
import { ApiGetQueryObject } from "../types";

type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
    query: string | ApiGetQueryObject<T>;
    values?: (string | number)[];
};
export default function apiGetGrabQueryAndValues<
    T extends { [key: string]: any } = { [key: string]: any }
>({ query, values }: Param<T>) {
    const queryGenObject =
        typeof query == "string"
            ? undefined
            : sqlGenerator({
                  tableName: query.table,
                  genObject: query.query,
                  dbFullName: query.dbFullName || "__db",
              });

    return {
        query:
            typeof query == "string"
                ? String(
                      query.replace(/\n|\r|\n\r/g, "").replace(/ {2,}/g, " ")
                  )
                : queryGenObject?.string || "",
        values: values || queryGenObject?.values,
        valuesString:
            typeof query == "string"
                ? values
                    ? JSON.stringify(values)
                    : undefined
                : queryGenObject?.values
                ? JSON.stringify(queryGenObject.values)
                : undefined,
    };
}
