"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTriggerStepRenamer = createTriggerStepRenamer;
const lodash_1 = require("lodash");
const uuid_1 = require("uuid");
const event_js_1 = require("../types/page/event.js");
const refactor_js_1 = require("./refactor.js");
function createTriggerStepRenamer({ oldName, newName, dataTree, addUpdate, extractEvaluationPairs, updateStepIds, namespace }) {
    const renameInTriggerStep = async (step, key, index) => {
        if (!(0, event_js_1.isValidStepDef)(step))
            return;
        if (updateStepIds) {
            addUpdate({
                propertyName: `${key}[${index}].id`,
                propertyValue: (0, uuid_1.v4)()
            });
        }
        switch (step.type) {
            case event_js_1.TriggerStepType.RUN_APIS: {
                if (step.apiNames) {
                    const newValue = step.apiNames.map((name) => (name === oldName ? newName : name));
                    if (!(0, lodash_1.isEqual)(step.apiNames, newValue)) {
                        addUpdate({
                            propertyName: `${key}[${index}].apiNames`,
                            propertyValue: newValue
                        });
                    }
                }
                if (step.onSuccess && (0, event_js_1.isValidMultiStepDef)(step.onSuccess)) {
                    let i = 0;
                    for (const nestedStep of step.onSuccess) {
                        await renameInTriggerStep(nestedStep, `${key}[${index}].onSuccess`, i++);
                    }
                }
                if (step.onError && (0, event_js_1.isValidMultiStepDef)(step.onError)) {
                    let i = 0;
                    for (const nestedStep of step.onError) {
                        await renameInTriggerStep(nestedStep, `${key}[${index}].onError`, i++);
                    }
                }
                break;
            }
            case event_js_1.TriggerStepType.CANCEL_APIS: {
                if (step.apiNames) {
                    const newValue = step.apiNames.map((name) => (name === oldName ? newName : name));
                    if (!(0, lodash_1.isEqual)(step.apiNames, newValue)) {
                        addUpdate({
                            propertyName: `${key}[${index}].apiNames`,
                            propertyValue: newValue
                        });
                    }
                }
                break;
            }
            case event_js_1.TriggerStepType.RUN_JS: {
                const newValue = await (0, refactor_js_1.refactorNameInCode)({
                    value: step.code ?? '',
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.code, newValue)) {
                    addUpdate({
                        propertyName: `${key}[${index}].code`,
                        propertyValue: newValue
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.CONTROL_SLIDEOUT:
            /* falls through */
            case event_js_1.TriggerStepType.CONTROL_MODAL:
            /* falls through */
            case event_js_1.TriggerStepType.CONTROL_TIMER: {
                if (step.name === oldName) {
                    addUpdate({
                        propertyName: `${key}[${index}].name`,
                        propertyValue: newName
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.NAVIGATE_TO: {
                const newValue = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.url,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.url, newValue)) {
                    addUpdate({
                        propertyName: `${key}[${index}].url`,
                        propertyValue: newValue
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.NAVIGATE_TO_APP: {
                const newValue = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.queryParams,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.queryParams, newValue)) {
                    addUpdate({
                        propertyName: `${key}[${index}].queryParams`,
                        propertyValue: newValue
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.NAVIGATE_TO_ROUTE: {
                const newQueryParams = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.queryParams,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                const newRouteParams = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.routeParams,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.queryParams, newQueryParams)) {
                    addUpdate({
                        propertyName: `${key}[${index}].queryParams`,
                        propertyValue: newQueryParams
                    });
                }
                if (!(0, lodash_1.isEqual)(step.routeParams, newRouteParams)) {
                    addUpdate({
                        propertyName: `${key}[${index}].routeParams`,
                        propertyValue: newRouteParams
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.SET_QUERY_PARAMS: {
                const newQueryParams = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.queryParams,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.queryParams, newQueryParams)) {
                    addUpdate({
                        propertyName: `${key}[${index}].queryParams`,
                        propertyValue: newQueryParams
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.SET_STATE_VAR: {
                const newValue = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.value,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.value, newValue)) {
                    addUpdate({
                        propertyName: `${key}[${index}].value`,
                        propertyValue: newValue
                    });
                }
                if (step.state?.name === oldName) {
                    addUpdate({
                        propertyName: `${key}[${index}].state.name`,
                        propertyValue: newName
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.RESET_STATE_VAR: {
                if (step.state?.name === oldName) {
                    addUpdate({
                        propertyName: `${key}[${index}].state.name`,
                        propertyValue: newName
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.RESET_COMPONENT: {
                if (step.widget?.name === oldName) {
                    addUpdate({
                        propertyName: `${key}[${index}].widget.name`,
                        propertyValue: newName
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.SET_COMPONENT_PROPERTY: {
                if (step.widget?.name === oldName) {
                    addUpdate({
                        propertyName: `${key}[${index}].widget.name`,
                        propertyValue: newName
                    });
                }
                const newValue = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.propertyValue,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (step?.propertyValue !== newValue) {
                    addUpdate({
                        propertyName: `${key}[${index}].propertyValue`,
                        propertyValue: newValue
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.SHOW_ALERT: {
                const newValue = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.message,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.message, newValue)) {
                    addUpdate({
                        propertyName: `${key}[${index}].message`,
                        propertyValue: newValue
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.SET_PROFILE: {
                const newValue = await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: step.profileId,
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                });
                if (!(0, lodash_1.isEqual)(step.profileId, newValue)) {
                    addUpdate({
                        propertyName: `${key}[${index}].profileId`,
                        propertyValue: newValue
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.TRIGGER_EVENT: {
                const payload = step.eventPayload ?? {};
                // 1. Refactor the name in the values of the eventPayload
                const newValues = (await (0, refactor_js_1.refactorNameInValue)({
                    oldValue: Object.values(payload),
                    oldName,
                    newName,
                    dataTree,
                    extractPairs: extractEvaluationPairs,
                    namespace
                }));
                const newEventPayload = Object.keys(payload).reduce((acc, key, i) => {
                    acc[key] = newValues[i];
                    return acc;
                }, {});
                if (!(0, lodash_1.isEqual)(payload, newEventPayload)) {
                    addUpdate({
                        propertyName: `${key}[${index}].eventPayload`,
                        propertyValue: newEventPayload
                    });
                }
                // 2. Update the event.name if it matches the oldName
                if (step.event?.name === oldName) {
                    addUpdate({
                        propertyName: `${key}[${index}].event.name`,
                        propertyValue: newName
                    });
                }
                break;
            }
            case event_js_1.TriggerStepType.RUN_SUPERBLOCKS_OPTIMIZED_APIS: {
                break;
            }
            default: {
                const exhaustiveCheck = step;
                throw new Error(`Unhandled action case: ${exhaustiveCheck}`);
            }
        }
    };
    return renameInTriggerStep;
}
//# sourceMappingURL=rename.js.map