"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.imports = void 0;
const node_html_parser_1 = require("node-html-parser");
const transpile_1 = require("../../transpile");
exports.imports = [
    { name: 'WmWizard', from: '@wavemaker/app-rn-runtime/components/container/wizard/wizard.component' }
];
// Function to process tap events
const processTapEvent = (node, element) => {
    // Map of button classes to wizard prop names
    const classnameActionMappings = {
        'app-wizard-cancel': 'on-cancel',
        'app-wizard-previous': 'on-prev',
        'app-wizard-next': 'on-next',
        'app-wizard-done': 'on-done',
        'app-wizard-skip': 'on-skip'
    };
    // Default actions to ignore which are added by default in studio.
    const defaultActions = ['fragment.cancel()', 'fragment.previous()', 'fragment.next()', 'fragment.done()', 'fragment.skip()'];
    const className = node.getAttribute('class');
    if (!className) {
        return;
    }
    // Find matching action type
    const actionType = Object.keys(classnameActionMappings).find(actionClass => className.includes(actionClass));
    if (!actionType) {
        return;
    }
    // Get tap event and process it
    const tapEvent = node.getAttribute('on-tap');
    if (tapEvent) {
        // Split the tap event by semicolon to handle multiple actions
        const actions = tapEvent.split(';').map(action => action.trim()).filter(Boolean);
        // Filter out default actions and keep only custom actions
        const customActions = actions.filter(action => {
            return !defaultActions.includes(action);
        });
        if (customActions.length > 0) {
            // Join remaining custom actions with semicolon
            const customActionString = customActions.join(';');
            element.setAttribute(classnameActionMappings[actionType], customActionString);
            if (customActions && customActions.length === actions.length) {
                element.setAttribute(`skipdefault${actionType.replace('app-wizard-', '')}`, 'true');
            }
            node.removeAttribute('on-tap');
        }
    }
};
// A work around to support the wizard action in the wizard component
function processWizardActions(element) {
    // Find wizardaction element
    const wizardActionNode = element.childNodes.find((node) => node instanceof node_html_parser_1.HTMLElement && node.rawTagName === 'wm-wizardaction');
    if (!wizardActionNode) {
        return;
    }
    // Find container within wizardaction
    const containerNode = wizardActionNode.childNodes.find((node) => node instanceof node_html_parser_1.HTMLElement && node.rawTagName === 'wm-container');
    // Find anchor within wizardaction
    const anchorNode = wizardActionNode.childNodes.find((node) => node instanceof node_html_parser_1.HTMLElement && node.rawTagName === 'wm-anchor');
    if (!containerNode) {
        return;
    }
    // Process each button
    containerNode.childNodes.forEach((node) => {
        if (node instanceof node_html_parser_1.HTMLElement) {
            processTapEvent(node, element);
        }
    });
    // Process anchor node if it exists
    if (anchorNode) {
        processTapEvent(anchorNode, element);
    }
    // Remove the wizardaction node since events are moved
    wizardActionNode.remove();
}
exports.default = {
    pre: (element, context) => {
        let i = 0;
        element.childNodes.forEach((c) => {
            if (c instanceof node_html_parser_1.HTMLElement) {
                c.setAttribute('index', '' + (i++));
            }
        });
        let menudataexpr = (0, transpile_1.createExpression)(element, 'menudataexpr');
        if (menudataexpr) {
            element.removeAttribute('menudataexpr');
        }
        processWizardActions(element);
        return `<WmWizard ${(0, transpile_1.transformAttrs)(element, context)} 
      ${menudataexpr ? `getmenudataexpression=${menudataexpr}` : ''}>`;
    },
    post: (element, context) => '</WmWizard>',
    imports: (element, context) => exports.imports,
    createComponent: (element, context) => (0, transpile_1.createComponent)(element, context)
};
//# sourceMappingURL=wizard.transformer.js.map