"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitizeV2RequestBody = exports.transcribeAudioToTextTranslateToEnglishFalsyValues = exports.transcribeAudioToTextTranslateToEnglishTruthyValues = void 0;
const protobuf_1 = require("@bufbuild/protobuf");
const graphqlintegration_js_1 = require("../plugins/templates/graphqlintegration.js");
const gsheets_js_1 = require("../plugins/templates/gsheets.js");
const mariadb_js_1 = require("../plugins/templates/mariadb.js");
const mysql_js_1 = require("../plugins/templates/mysql.js");
const openai_js_1 = require("../plugins/templates/openai.js");
const integration_js_1 = require("../types/plugin/integration.js");
exports.transcribeAudioToTextTranslateToEnglishTruthyValues = ['checked', 'true', true];
exports.transcribeAudioToTextTranslateToEnglishFalsyValues = [null, undefined, 'false', false];
const sanitizeV2RequestBody = (apiBody) => {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const traverseJSON = (obj) => {
        for (const key in obj) {
            if (key === 'step') {
                if (obj.step[openai_js_1.OpenAiPlugin.id]) {
                    const value = obj.step[openai_js_1.OpenAiPlugin.id].transcribeAudioToTextTranslateToEnglish;
                    obj.step[openai_js_1.OpenAiPlugin.id].transcribeAudioToTextTranslateToEnglish =
                        exports.transcribeAudioToTextTranslateToEnglishTruthyValues.includes(value);
                    // cast generateTextNewTextMaxTokens as string
                    if (obj.step[openai_js_1.OpenAiPlugin.id].generateTextNewTextMaxTokens) {
                        obj.step[openai_js_1.OpenAiPlugin.id].generateTextNewTextMaxTokens = obj.step[openai_js_1.OpenAiPlugin.id].generateTextNewTextMaxTokens.toString();
                    }
                }
                else if (obj.step[mariadb_js_1.MariaDBPlugin.id] && 'newValues' in obj.step[mariadb_js_1.MariaDBPlugin.id]) {
                    // check if it's an array
                    if (Array.isArray(obj.step[mariadb_js_1.MariaDBPlugin.id].newValues)) {
                        obj.step[mariadb_js_1.MariaDBPlugin.id].newValues = '';
                    }
                }
                else if (obj.step[mysql_js_1.MySQLPlugin.id] && 'newValues' in obj.step[mysql_js_1.MySQLPlugin.id]) {
                    // check if it's an array
                    if (Array.isArray(obj.step[mysql_js_1.MySQLPlugin.id].newValues)) {
                        obj.step[mysql_js_1.MySQLPlugin.id].newValues = '';
                    }
                }
                else if (obj.step[gsheets_js_1.GOOGLE_SHEETS_PLUGIN_ID] && 'rowNumber' in obj.step[gsheets_js_1.GOOGLE_SHEETS_PLUGIN_ID]) {
                    obj.step[gsheets_js_1.GOOGLE_SHEETS_PLUGIN_ID].rowNumber = obj.step[gsheets_js_1.GOOGLE_SHEETS_PLUGIN_ID].rowNumber.toString();
                }
                else if (obj.step[graphqlintegration_js_1.GraphQLIntegrationPlugin.id]) {
                    const stepConfig = obj.step[graphqlintegration_js_1.GraphQLIntegrationPlugin.id];
                    stepConfig.path = '';
                    stepConfig.headers = (stepConfig.headers ?? []).filter((header) => header.editable !== false);
                }
                for (const stepProperty of Object.keys(obj.step)) {
                    if (stepProperty === 'integration')
                        continue;
                    if ((0, integration_js_1.extendsRestApiIntegrationPlugin)(stepProperty)) {
                        const stepConfig = obj.step[stepProperty];
                        stepConfig.urlBase = '';
                        stepConfig.headers = (stepConfig.headers ?? []).filter((header) => header.editable !== false);
                        stepConfig.params = (stepConfig.params ?? []).filter((param) => param.editable !== false);
                    }
                }
            }
            else if (obj?.trigger?.workflow?.parameters?.query) {
                const queryParams = obj.trigger.workflow.parameters.query;
                // iterate over the keys of the query params
                for (const queryParamKey in queryParams) {
                    // check if the query param is a string
                    if (typeof queryParams[queryParamKey] === 'string') {
                        queryParams[queryParamKey] = { values: [queryParams[queryParamKey]] };
                    }
                }
            }
            else if (obj[key] === null) {
                obj[key] = protobuf_1.Value.fromJson(null);
            }
            else if (key === 'variables' && 'items' in obj[key]) {
                const variables = getVariables(obj[key]['items']);
                obj[key]['items'] = variables;
            }
            else if (typeof obj[key] === 'object') {
                (0, exports.sanitizeV2RequestBody)(obj[key]);
            }
        }
    };
    traverseJSON(apiBody);
    return apiBody;
};
exports.sanitizeV2RequestBody = sanitizeV2RequestBody;
function getVariables(items) {
    const variables = [];
    for (const key in items) {
        const variableWithKey = {
            key,
            ...sanitizeVariableValue(items[key])
        };
        variables.push(variableWithKey);
    }
    return variables;
}
function sanitizeVariableValue(variable) {
    if (Array.isArray(variable.value)) {
        variable.value = `{{${variable.value.join(',')}}}`;
    }
    return variable;
}
//# sourceMappingURL=apiv2.js.map