// This file was moved from the `server` package to here and `server` has `strictNullChecks` disabled.
// This code was originally written with the assumption that `widget.children` is never undefined, despite the type saying otherwise.
// So we are using `!` to assert that `widget.children` is never undefined.
// Note that `!` does not change the runtime behavior of the code so the code will work the same way as before.
/* eslint-disable @typescript-eslint/no-explicit-any */
import { cloneDeep } from 'lodash';
import { describe, it, expect } from 'vitest';
import containerV4 from '../../utils/pageFixtures/v4/container.js';
import codeTrigger from '../../utils/pageFixtures/v5/codeTrigger.js';
import complexTriggers from '../../utils/pageFixtures/v5/complexTriggers.js';
import duplicateTriggers from '../../utils/pageFixtures/v5/duplicateTriggers.js';
import missingTriggers from '../../utils/pageFixtures/v5/missingTriggers.js';
import { migrate5to6 } from './v5to6.js';
describe('version 5 to version 6', () => {
    it('should migrate simple button code', () => {
        const migrated = migrate5to6(cloneDeep(containerV4));
        const button = migrated.children[0].children[0].children[0].children[0].children[0];
        expect(button.onClick).toEqual([{ id: 'id0', type: 'controlSlideout', name: 'Slideout_Trigger_the_BOT', direction: 'open' }]);
        expect(button.dynamicTriggerPathList).toEqual([{ key: 'onClick' }]);
    });
    it('should not migrate code that uses special character', () => {
        const migrated = migrate5to6(cloneDeep(containerV4));
        const button1 = migrated.children[0].children[0].children[0].children[1].children[1].children[0].children[0];
        const button2 = migrated.children[0].children[0].children[0].children[1].children[1].children[0].children[1];
        expect(button1.onSubmit).toEqual([{ id: 'id0', type: 'runJs', code: 'API_invoice_header.run()' }]);
        expect(button2.onSubmit).toEqual([{ id: 'id0', type: 'executeApi', apiNames: ['API_invoice_header'] }]);
        expect(button1.dynamicTriggerPathList).toContainEqual({ key: 'onSubmit' });
        expect(button2.dynamicTriggerPathList).toEqual([{ key: 'onSubmit' }]);
    });
    it('should migrate a table button with string', () => {
        const migrated = migrate5to6(cloneDeep(containerV4));
        const table = migrated.children[0].children[0].children[0].children[1].children[3];
        expect(table.primaryColumns.customColumn3.onClick).toEqual([
            { id: 'id0', type: 'runJs', code: 'API_Icp_Payroll_Item.run(); slideout_icp_payroll_items.open()' }
        ]);
        expect(table.dynamicTriggerPathList).toContainEqual({ key: 'primaryColumns.customColumn3.onClick' });
    });
    it('should migrate a dynamic navigateTo URL', () => {
        const widget = {
            text: 'Trigger the BOT',
            type: 'BUTTON_WIDGET',
            topRow: 1,
            onClick: ['{{navigateTo("https://google.com?q=" + Input1.value)}}'],
            parentId: 'nhcpl240ad',
            widgetId: '33wah1ou3a',
            bottomRow: 2,
            isLoading: false,
            isVisible: true,
            textColor: '#000000',
            isDisabled: false,
            leftColumn: 1,
            widgetName: 'Button9',
            buttonStyle: 'PRIMARY_BUTTON',
            rightColumn: 7,
            parentRowSpace: 40,
            backgroundColor: '#FF9F35',
            parentColumnSpace: 47.625,
            dynamicBindingPathList: [
                {
                    key: 'onClick'
                }
            ],
            dynamicTriggerPathList: [
                {
                    key: 'onClick'
                }
            ],
            isDefaultClickDisabled: true
        };
        const migrated = migrate5to6(widget);
        expect(migrated.onClick).toEqual([{ id: 'id0', type: 'runJs', code: 'navigateTo("https://google.com?q=" + Input1.value)' }]);
        expect(migrated.dynamicTriggerPathList).toEqual([{ key: 'onClick' }]);
        expect(migrated.dynamicBindingPathList).toEqual([]);
    });
    it('should correctly migrate complex triggers', () => {
        const migrated = migrate5to6(cloneDeep(complexTriggers));
        expect(migrated.children[0].onClick).toEqual([
            {
                id: 'id0',
                code: '\nget_tiny_filter.run(() => {\n\tdeafult_filters.run();\n})\n',
                type: 'runJs'
            },
            {
                id: 'id1',
                code: "\nresetComponent('location_dropdown') && resetComponent('sector_dropdown') && resetComponent('investor_dropdown') && resetComponent('revenue_dropdown') && \nresetComponent('founded_dropdown') && resetComponent('number_of_employee_dropdown')\n",
                type: 'runJs'
            },
            {
                id: 'id2',
                type: 'executeApi',
                apiNames: ['sleep']
            },
            {
                id: 'id3',
                code: 'apply_filters.run();map_feed.run();storeValue("sessionId", report_page_load.response)\n;',
                type: 'runJs'
            },
            {
                id: 'id4',
                code: 'valuation_line_feed.run();money_raised_bar_feed.run();investor_count_feed.run(); ',
                type: 'runJs'
            },
            {
                id: 'id5',
                code: 'revenue_bar_feed.run();sector_pie_feed.run();location_pie_feed.run(); revenue_pie_feed.run();employee_per_org.run();employee_pie_feed.run();',
                type: 'runJs'
            },
            {
                id: 'id6',
                code: 'storeValue("sessionId", report_page_load.response)\n;',
                type: 'runJs'
            },
            {
                id: 'id7',
                code: 'report_search.run();',
                type: 'runJs'
            }
        ]);
        expect(migrated.children[1].onClick).toEqual([
            {
                id: 'id0',
                type: 'runJs',
                code: ''
            },
            {
                id: 'id1',
                type: 'runJs',
                code: ''
            }
        ]);
    });
    it('should correctly migrate triggers with duplicates', () => {
        const migrated = migrate5to6(cloneDeep(duplicateTriggers));
        expect(migrated.children[0].onTextChanged).toEqual([
            {
                id: 'id0',
                code: '',
                type: 'runJs'
            }
        ]);
        expect(migrated.children[0].dynamicTriggerPathList).toEqual([{ key: 'onTextChanged' }]);
    });
    it('should correctly migrate triggers with code mode enabled', () => {
        const migrated = migrate5to6(cloneDeep(codeTrigger));
        expect(migrated.children[0].onTextChanged).toEqual([
            {
                id: 'id0',
                apiNames: ['API'],
                type: 'executeApi'
            }
        ]);
        expect(migrated.children[0].dynamicTriggerPathList).toEqual([{ key: 'onTextChanged' }]);
        expect(migrated.children[0].dynamicPropertyPathList).toEqual([]);
        expect(migrated.children[1].onTextChanged).toEqual([
            {
                id: 'id0',
                code: `API.run()
showAlert('hello')`,
                type: 'runJs'
            }
        ]);
        expect(migrated.children[1].dynamicTriggerPathList).toEqual([{ key: 'onTextChanged' }]);
        expect(migrated.children[1].dynamicPropertyPathList).toEqual([]);
    });
    it('should correctly migrate onSubmit properties that were missing a dynamicTriggerPath', () => {
        const migrated = migrate5to6(cloneDeep(missingTriggers));
        expect(migrated.children[0].onSubmit).toEqual([
            {
                id: 'id0',
                code: '',
                type: 'runJs'
            }
        ]);
        expect(migrated.children[0].dynamicTriggerPathList).toEqual([{ key: 'onSubmit' }, { key: 'onTextChanged' }]);
    });
});
//# sourceMappingURL=v5to6.test.js.map