"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrate5to6 = void 0;
const lodash_1 = require("lodash");
const WIDGET_TO_TRIGGER = {
    INPUT_WIDGET: ['onSubmit', 'onTextChanged'],
    TABLE_WIDGET: ['onRowClicked', 'onRowSelected', 'onPageChange', 'onFiltersChanged', 'onSearchTextChanged'],
    GRID_WIDGET: ['onCellClicked'],
    CHART_WIDGET: ['onSelectData'],
    RICH_TEXT_EDITOR_WIDGET: ['onSelectData'],
    DROP_DOWN_WIDGET: ['onOptionChange'],
    BUTTON_WIDGET: ['onClick'],
    FORM_BUTTON_WIDGET: ['onClick'],
    IMAGE_WIDGET: ['onClick'],
    DATE_PICKER_WIDGET: ['onDateSelected'],
    VIDEO_WIDGET: ['onPlay', 'onPause', 'onEnd'],
    RADIO_GROUP_WIDGET: ['onSelectionChange'],
    CHECKBOX_WIDGET: ['onSelectionChange'],
    TABS_WIDGET: ['onTabSelected'],
    CODE_WIDGET: ['onChange'],
    IFRAME_WIDGET: ['onMessageReceived'],
    MAP_WIDGET: ['onMarkerClick'],
    SLIDEOUT_WIDGET: ['onOpen', 'onClose'],
    MODAL_WIDGET: ['onOpen', 'onClose'],
    ICON_WIDGET: ['onClick']
};
// This migration code uses strings instead of variables like CANVAS_WIDGET for two reasons:
// 1. These strings will never change, even if we change the types in the future
// 2. We do want to make these types much stricter in the future, but for now
const migrate5to6 = (widget) => {
    if ((0, lodash_1.isArray)(widget.dynamicTriggerPathList)) {
        const bindingKeysToRemove = [];
        // This is a special case we've found where the dynamicTriggerPathList was entirely missing from some properties
        WIDGET_TO_TRIGGER[widget.type]?.forEach((path) => {
            if (!(0, lodash_1.isEmpty)(widget[path]) &&
                (0, lodash_1.isArray)(widget.dynamicTriggerPathList) &&
                !widget.dynamicTriggerPathList.some(({ key }) => key === path)) {
                widget.dynamicTriggerPathList.push({ key: path });
            }
        });
        widget.dynamicTriggerPathList.forEach(({ key }) => {
            // Some apps have duplicate trigger keys, these will get cleaned up later
            if (bindingKeysToRemove.includes(key))
                return;
            const value = (0, lodash_1.get)(widget, key);
            bindingKeysToRemove.push(key);
            if ((0, lodash_1.isArray)(value)) {
                const newEvents = value.map(extractActionFromString);
                (0, lodash_1.set)(widget, key, newEvents);
            }
            else if ((0, lodash_1.isString)(value)) {
                const newEvent = extractActionFromString(value, 0);
                (0, lodash_1.set)(widget, key, [newEvent]);
            }
        });
        // Remove duplicates
        widget.dynamicTriggerPathList = (0, lodash_1.uniq)(widget.dynamicTriggerPathList.map(({ key }) => key)).map((key) => ({ key }));
        widget.dynamicBindingPathList = (widget.dynamicBindingPathList ?? []).filter(({ key }) => {
            return !bindingKeysToRemove.includes(key);
        });
        widget.dynamicPropertyPathList = (widget.dynamicPropertyPathList ?? []).filter(({ key }) => {
            return !bindingKeysToRemove.includes(key);
        });
    }
    widget.children?.forEach((child) => {
        if (!child)
            return;
        (0, exports.migrate5to6)(child);
    });
    return widget;
};
exports.migrate5to6 = migrate5to6;
const strictlyMatchApiRun = /^\s*\{\{\s*(\w+)\.run\(\)\s*\}\}\s*$/;
const strictlyMatchOpenClose = /^\s*\{\{\s*(\w+)\.(open|close)\(\)\s*\}\}\s*$/;
const DATA_BIND_REGEX_GLOBAL = /{{([\s\S]*?)}}/g;
function extractActionFromString(value, index) {
    if (!(0, lodash_1.isString)(value)) {
        throw new Error(`Value is not string: ${JSON.stringify(value)}`);
    }
    const matchesApiRun = value.match(strictlyMatchApiRun);
    if (matchesApiRun?.[1]) {
        return {
            id: `id${index}`,
            type: 'executeApi',
            apiNames: [matchesApiRun?.[1]]
        };
    }
    const matchesSlideoutOpenClose = value.match(strictlyMatchOpenClose);
    if (matchesSlideoutOpenClose?.[1]) {
        const slideoutName = matchesSlideoutOpenClose?.[1];
        return {
            id: `id${index}`,
            type: 'controlSlideout',
            name: slideoutName,
            direction: matchesSlideoutOpenClose[2]
        };
    }
    const codeMatches = [...value.matchAll(DATA_BIND_REGEX_GLOBAL)];
    return {
        id: `id${index}`,
        type: 'runJs',
        code: codeMatches.map((matches) => matches.slice(1).join('')).join('\n')
    };
}
//# sourceMappingURL=v5to6.js.map