"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mablApi_1 = require("../../../mablApi");
const importer_1 = require("@mablhq/test-model/importer");
const mablscript_step_types_1 = require("../mablscript-step-types");
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const flowstep_types_1 = require("./flowstep-types");
const AccessibilityCheck_1 = require("@mablhq/test-model/steps/AccessibilityCheck");
const AccessibilityCheckStep_1 = __importDefault(require("./canonical/AccessibilityCheckStep"));
const AssertStep_1 = require("@mablhq/test-model/steps/AssertStep");
const AssertStep_2 = __importDefault(require("./canonical/AssertStep"));
const assert_utils_1 = require("./canonical/assert-utils");
const AwaitTabStep_1 = require("@mablhq/test-model/steps/AwaitTabStep");
const AwaitTabStep_2 = __importDefault(require("./canonical/AwaitTabStep"));
const AwaitUploadsStep_1 = require("@mablhq/test-model/steps/AwaitUploadsStep");
const AwaitUploadStep_1 = __importDefault(require("./canonical/AwaitUploadStep"));
const ClearCookiesStep_1 = require("@mablhq/test-model/steps/ClearCookiesStep");
const ClearCookiesStep_2 = __importDefault(require("./canonical/ClearCookiesStep"));
const ClickStep_1 = require("@mablhq/test-model/steps/ClickStep");
const ClickStep_2 = __importDefault(require("./canonical/ClickStep"));
const ClickAndHoldStep_1 = require("@mablhq/test-model/steps/ClickAndHoldStep");
const ClickAndHoldStep_2 = __importDefault(require("./canonical/ClickAndHoldStep"));
const CreateVariableStep_1 = require("@mablhq/test-model/steps/CreateVariableStep");
const CreateVariableStep_2 = __importDefault(require("./canonical/CreateVariableStep"));
const DatabaseQueryStep_1 = require("@mablhq/test-model/steps/DatabaseQueryStep");
const DatabaseQueryStep_2 = __importDefault(require("./canonical/DatabaseQueryStep"));
const DoubleClickStep_1 = require("@mablhq/test-model/steps/DoubleClickStep");
const DoubleClickStep_2 = __importDefault(require("./canonical/DoubleClickStep"));
const DownloadStep_1 = require("@mablhq/test-model/steps/DownloadStep");
const DownloadPdfStep_1 = __importDefault(require("./canonical/DownloadPdfStep"));
const DownloadStep_2 = __importDefault(require("./canonical/DownloadStep"));
const EchoStep_1 = require("@mablhq/test-model/steps/EchoStep");
const EchoStep_2 = __importDefault(require("./canonical/EchoStep"));
const ElseStep_1 = require("@mablhq/test-model/steps/ElseStep");
const ElseStep_2 = __importDefault(require("./canonical/ElseStep"));
const EndStep_1 = require("@mablhq/test-model/steps/EndStep");
const EndStep_2 = __importDefault(require("./canonical/EndStep"));
const EnterAuthCodeStep_1 = require("@mablhq/test-model/steps/EnterAuthCodeStep");
const EnterAuthCodeStep_2 = __importDefault(require("./canonical/EnterAuthCodeStep"));
const EnterTextStep_1 = require("@mablhq/test-model/steps/EnterTextStep");
const EnterTextStep_2 = __importDefault(require("./canonical/EnterTextStep"));
const EvaluateFlowStep_1 = require("@mablhq/test-model/steps/EvaluateFlowStep");
const EvaluateJavaScriptStep_1 = require("@mablhq/test-model/steps/EvaluateJavaScriptStep");
const EvaluateJSStep_1 = __importStar(require("./canonical/EvaluateJSStep"));
const HoverStep_1 = require("@mablhq/test-model/steps/HoverStep");
const HoverStep_2 = __importDefault(require("./canonical/HoverStep"));
const SendKeyStep_1 = require("@mablhq/test-model/steps/SendKeyStep");
const KeyPressStep_1 = __importDefault(require("./canonical/KeyPressStep"));
const NavigateStep_1 = require("@mablhq/test-model/steps/NavigateStep");
const NavigationStep_1 = __importDefault(require("./canonical/NavigationStep"));
const OpenEmailStep_1 = require("@mablhq/test-model/steps/OpenEmailStep");
const OpenEmailStep_2 = __importDefault(require("./canonical/OpenEmailStep"));
const ReleaseStep_1 = require("@mablhq/test-model/steps/ReleaseStep");
const ReleaseStep_2 = __importDefault(require("./canonical/ReleaseStep"));
const RemoveCookieStep_1 = require("@mablhq/test-model/steps/RemoveCookieStep");
const RemoveCookieStep_2 = __importDefault(require("./canonical/RemoveCookieStep"));
const RightClickStep_1 = require("@mablhq/test-model/steps/RightClickStep");
const RightClickStep_2 = __importDefault(require("./canonical/RightClickStep"));
const SelectStep_1 = require("@mablhq/test-model/steps/SelectStep");
const SelectStep_2 = __importDefault(require("./canonical/SelectStep"));
const SendHttpRequestStep_1 = require("@mablhq/test-model/steps/SendHttpRequestStep");
const SendHttpRequestStep_2 = __importDefault(require("./canonical/SendHttpRequestStep"));
const SetCookieStep_1 = require("@mablhq/test-model/steps/SetCookieStep");
const SetCookieStep_2 = __importDefault(require("./canonical/SetCookieStep"));
const SetFilesStep_1 = require("@mablhq/test-model/steps/SetFilesStep");
const SetFilesStep_2 = __importDefault(require("./canonical/SetFilesStep"));
const SwitchContextStep_1 = require("@mablhq/test-model/steps/SwitchContextStep");
const SwitchContextToStep_1 = __importDefault(require("./canonical/SwitchContextToStep"));
const StepGroupStep_1 = require("@mablhq/test-model/steps/StepGroupStep");
const VisitUrlStep_1 = require("@mablhq/test-model/steps/VisitUrlStep");
const VisitUrlStep_2 = __importDefault(require("./canonical/VisitUrlStep"));
const WaitStep_1 = require("@mablhq/test-model/steps/WaitStep");
const WaitStep_2 = __importDefault(require("./canonical/WaitStep"));
const ViewportStep_1 = __importDefault(require("./canonical/ViewportStep"));
const SetViewportStep_1 = require("@mablhq/test-model/steps/SetViewportStep");
const MablStepAdapter_1 = require("./canonical/MablStepAdapter");
const ExecuteFlowStep_1 = __importDefault(require("./extended/ExecuteFlowStep"));
const ExecuteStepGroupStep_1 = __importDefault(require("./extended/ExecuteStepGroupStep"));
const MablStepToTrainerStep = {
    [AccessibilityCheck_1.AccessibilityCheckStep.mablScriptStepNames[0]]: AccessibilityCheckStep_1.default,
    [AccessibilityCheck_1.AccessibilityCheckStep.yamlMablScriptNames[0]]: AccessibilityCheckStep_1.default,
    [AssertStep_1.AssertStep.mablScriptStepNames[0]]: AssertStep_2.default,
    ['AssertEquals']: AssertStep_2.default,
    ['AssertPresent']: AssertStep_2.default,
    ['AssertNotPresent']: AssertStep_2.default,
    ['AssertDoesNotEqual']: AssertStep_2.default,
    ['AssertContains']: AssertStep_2.default,
    ['AssertDoesNotContain']: AssertStep_2.default,
    ['AssertStartsWith']: AssertStep_2.default,
    ['AssertStartsWithout']: AssertStep_2.default,
    ['AssertEndsWith']: AssertStep_2.default,
    ['AssertEndsWithout']: AssertStep_2.default,
    ['AssertGreaterThan']: AssertStep_2.default,
    ['AssertGreaterThanOrEquals']: AssertStep_2.default,
    ['AssertLessThan']: AssertStep_2.default,
    ['AssertLessThanOrEquals']: AssertStep_2.default,
    ['AssertMatchesRegex']: AssertStep_2.default,
    ['AssertAIPrompt']: AssertStep_2.default,
    [AwaitTabStep_1.AwaitTabStep.mablScriptStepNames[0]]: AwaitTabStep_2.default,
    [AwaitTabStep_1.AwaitTabStep.yamlMablScriptNames[0]]: AwaitTabStep_2.default,
    [AwaitUploadsStep_1.AwaitUploadsStep.mablScriptStepNames[0]]: AwaitUploadStep_1.default,
    [AwaitUploadsStep_1.AwaitUploadsStep.yamlMablScriptNames[0]]: AwaitUploadStep_1.default,
    [ClearCookiesStep_1.ClearCookiesStep.mablScriptStepNames[0]]: ClearCookiesStep_2.default,
    [ClearCookiesStep_1.ClearCookiesStep.yamlMablScriptNames[0]]: ClearCookiesStep_2.default,
    [ClickStep_1.ClickStep.mablScriptStepNames[0]]: ClickStep_2.default,
    [ClickStep_1.ClickStep.yamlMablScriptNames[0]]: ClickStep_2.default,
    [ClickAndHoldStep_1.ClickAndHoldStep.mablScriptStepNames[0]]: ClickAndHoldStep_2.default,
    [ClickAndHoldStep_1.ClickAndHoldStep.yamlMablScriptNames[0]]: ClickAndHoldStep_2.default,
    [CreateVariableStep_1.CreateVariableStep.mablScriptStepNames[0]]: CreateVariableStep_2.default,
    [CreateVariableStep_1.CreateVariableStep.yamlMablScriptNames[0]]: CreateVariableStep_2.default,
    [DatabaseQueryStep_1.DatabaseQueryStep.mablScriptStepNames[0]]: DatabaseQueryStep_2.default,
    [DatabaseQueryStep_1.DatabaseQueryStep.yamlMablScriptNames[0]]: DatabaseQueryStep_2.default,
    [DoubleClickStep_1.DoubleClickStep.mablScriptStepNames[0]]: DoubleClickStep_2.default,
    [DoubleClickStep_1.DoubleClickStep.yamlMablScriptNames[0]]: DoubleClickStep_2.default,
    [DownloadStep_1.DownloadStep.mablScriptStepNames[0]]: DownloadStep_2.default,
    [DownloadStep_1.DownloadStep.yamlMablScriptNames[0]]: DownloadStep_2.default,
    ['DownloadPdfStep']: DownloadPdfStep_1.default,
    [EchoStep_1.EchoStep.mablScriptStepNames[0]]: EchoStep_2.default,
    [EchoStep_1.EchoStep.yamlMablScriptNames[0]]: EchoStep_2.default,
    [ElseStep_1.ElseStep.mablScriptStepNames[0]]: ElseStep_2.default,
    [ElseStep_1.ElseStep.yamlMablScriptNames[0]]: ElseStep_2.default,
    [EndStep_1.EndStep.mablScriptStepNames[0]]: EndStep_2.default,
    [EndStep_1.EndStep.yamlMablScriptNames[0]]: EndStep_2.default,
    [EnterAuthCodeStep_1.EnterAuthCodeStep.mablScriptStepNames[0]]: EnterAuthCodeStep_2.default,
    [EnterAuthCodeStep_1.EnterAuthCodeStep.yamlMablScriptNames[0]]: EnterAuthCodeStep_2.default,
    [EnterTextStep_1.EnterTextStep.mablScriptStepNames[0]]: EnterTextStep_2.default,
    [EnterTextStep_1.EnterTextStep.yamlMablScriptNames[0]]: EnterTextStep_2.default,
    [EvaluateJavaScriptStep_1.EvaluateJavaScriptStep.mablScriptStepNames[0]]: EvaluateJSStep_1.default,
    [EvaluateJavaScriptStep_1.EvaluateJavaScriptStep.yamlMablScriptNames[0]]: EvaluateJSStep_1.default,
    [HoverStep_1.HoverStep.mablScriptStepNames[0]]: HoverStep_2.default,
    [HoverStep_1.HoverStep.yamlMablScriptNames[0]]: HoverStep_2.default,
    [SendKeyStep_1.SendKeyStep.mablScriptStepNames[0]]: KeyPressStep_1.default,
    [SendKeyStep_1.SendKeyStep.yamlMablScriptNames[0]]: KeyPressStep_1.default,
    [NavigateStep_1.NavigateStep.mablScriptStepNames[0]]: NavigationStep_1.default,
    [NavigateStep_1.NavigateStep.yamlMablScriptNames[0]]: NavigationStep_1.default,
    [OpenEmailStep_1.OpenEmailStep.mablScriptStepNames[0]]: OpenEmailStep_2.default,
    [OpenEmailStep_1.OpenEmailStep.yamlMablScriptNames[0]]: OpenEmailStep_2.default,
    [ReleaseStep_1.ReleaseStep.mablScriptStepNames[0]]: ReleaseStep_2.default,
    [ReleaseStep_1.ReleaseStep.yamlMablScriptNames[0]]: ReleaseStep_2.default,
    [RemoveCookieStep_1.RemoveCookieStep.mablScriptStepNames[0]]: RemoveCookieStep_2.default,
    [RemoveCookieStep_1.RemoveCookieStep.yamlMablScriptNames[0]]: RemoveCookieStep_2.default,
    [RightClickStep_1.RightClickStep.mablScriptStepNames[0]]: RightClickStep_2.default,
    [RightClickStep_1.RightClickStep.yamlMablScriptNames[0]]: RightClickStep_2.default,
    [SelectStep_1.SelectStep.mablScriptStepNames[0]]: SelectStep_2.default,
    [SelectStep_1.SelectStep.yamlMablScriptNames[0]]: SelectStep_2.default,
    [SendHttpRequestStep_1.SendHttpRequestStep.mablScriptStepNames[0]]: SendHttpRequestStep_2.default,
    [SendHttpRequestStep_1.SendHttpRequestStep.yamlMablScriptNames[0]]: SendHttpRequestStep_2.default,
    [SetCookieStep_1.SetCookieStep.mablScriptStepNames[0]]: SetCookieStep_2.default,
    [SetCookieStep_1.SetCookieStep.yamlMablScriptNames[0]]: SetCookieStep_2.default,
    [SetFilesStep_1.SetFilesStep.mablScriptStepNames[0]]: SetFilesStep_2.default,
    [SetFilesStep_1.SetFilesStep.yamlMablScriptNames[0]]: SetFilesStep_2.default,
    [SetViewportStep_1.SetViewportStep.mablScriptStepNames[0]]: ViewportStep_1.default,
    [SetViewportStep_1.SetViewportStep.yamlMablScriptNames[0]]: ViewportStep_1.default,
    [SwitchContextStep_1.SwitchContextStep.mablScriptStepNames[0]]: SwitchContextToStep_1.default,
    [SwitchContextStep_1.SwitchContextStep.yamlMablScriptNames[0]]: SwitchContextToStep_1.default,
    [VisitUrlStep_1.VisitUrlStep.mablScriptStepNames[0]]: VisitUrlStep_2.default,
    [VisitUrlStep_1.VisitUrlStep.yamlMablScriptNames[0]]: VisitUrlStep_2.default,
    ['WaitUntil']: AssertStep_2.default,
    [WaitStep_1.WaitStep.mablScriptStepNames[0]]: WaitStep_2.default,
    [WaitStep_1.WaitStep.yamlMablScriptNames[0]]: WaitStep_2.default,
    [EvaluateFlowStep_1.EvaluateFlowStep.mablScriptStepNames[0]]: ExecuteFlowStep_1.default,
    [EvaluateFlowStep_1.EvaluateFlowStep.yamlMablScriptNames[0]]: ExecuteFlowStep_1.default,
};
class MablscriptImporter {
    constructor(flowSteps, variableContext, snippets) {
        this.snippets = snippets || {};
        this.flowSteps = flowSteps;
        this.tabSelectors = new mablscriptFind_1.TabSelectors();
        this.emailSelectors = new mablscriptFind_1.EmailSelectors();
        this.variables = variableContext;
    }
    run(flow) {
        if ((0, flowstep_types_1.isFlowFetchFailure)(flow) && flow?.errorcode) {
            console.log('add execute flow error step');
            this.addStep(new ExecuteFlowStep_1.default({ flowError: flow, isError: true }));
        }
        else {
            if (this.flowNeedsBoundaries(flow)) {
                this.flowSteps.flowInvariantId = flow.invariant_id;
            }
            this.importFlow(flow, this.flowSteps.steps);
        }
    }
    importFlow(flow, steps) {
        const isMobileFlow = flow.flow_type === mablApi_1.FlowTypeEnum.Mobile;
        const hasJsonSteps = flow.json_steps?.steps && flow.json_steps.steps.length > 0;
        const parsed = hasJsonSteps
            ? (0, importer_1.interpretStepsFromObjects)(flow.json_steps?.steps, isMobileFlow)
            : (0, importer_1.parseMablScriptIntoSteps)(flow);
        const maybeLegacyStepIds = this.shouldApplyLegacyStepIds(flow, hasJsonSteps, parsed.length);
        parsed.forEach((mablStep, index) => {
            if (maybeLegacyStepIds) {
                mablStep.setStepId(maybeLegacyStepIds[index]);
            }
            this.populateUndefinedVariables(mablStep);
            const step = this.convertMablStepToTrainerStep(mablStep, isMobileFlow);
            step.setDisabled(mablStep.isDisabled());
            steps.push(step);
        });
        this.augmentFlowStepsWithAnnotations(flow, steps);
    }
    shouldApplyLegacyStepIds(flow, hasJsonSteps, parsedStepCount) {
        if (hasJsonSteps) {
            return undefined;
        }
        const legacyStepIds = this.flowSteps.legacyStepIds;
        if (!legacyStepIds) {
            return undefined;
        }
        if (legacyStepIds.length !== parsedStepCount) {
            console.warn(`Step ID count mismatch for flow ${flow.id}: expected ${parsedStepCount} steps but got ${legacyStepIds.length} step IDs. Skipping step ID backfill.`);
            return undefined;
        }
        return legacyStepIds;
    }
    parseStepFromMablscript(mablscript) {
        return this.convertMablStepToTrainerStep((0, importer_1.parseMablScriptIntoV2Step)(mablscript), false);
    }
    addStep(step) {
        this.flowSteps.steps.push(step);
    }
    flowNeedsBoundaries(flow) {
        return flow.reusable || flow.flow_type === mablApi_1.FlowTypeEnum.Login;
    }
    augmentFlowStepsWithAnnotations(flow, steps) {
        if (!flow?.step_annotations) {
            return;
        }
        Object.entries(flow.step_annotations).forEach(([key, value]) => {
            if (!/^\d+$/.test(key)) {
                return;
            }
            const index = parseInt(key, 10);
            if (steps[index]) {
                steps[index].annotations = value || undefined;
            }
        });
    }
    convertMablStepToTrainerStep(mablStep, isMobileFlow) {
        const stepName = mablStep.getStepName();
        if (stepName === StepGroupStep_1.StepGroupStep.stepName ||
            mablStep instanceof StepGroupStep_1.StepGroupStep) {
            return this.convertStepGroupStep(mablStep, isMobileFlow);
        }
        let trainerStep = MablStepToTrainerStep[stepName];
        if (!trainerStep) {
            if ((0, assert_utils_1.isIfConditionStep)(mablStep) || (0, assert_utils_1.isElseIfConditionStep)(mablStep)) {
                trainerStep = (0, EvaluateJSStep_1.isJavascriptAction)(mablStep.primaryAction)
                    ? EvaluateJSStep_1.default
                    : AssertStep_2.default;
            }
            else {
                return new MablStepAdapter_1.MablStepAdapter(mablStep);
            }
        }
        let step;
        switch (trainerStep.getStepType()) {
            case mablscript_step_types_1.StepType.ACCESSIBILITY_CHECK:
                step = AccessibilityCheckStep_1.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.ASSERTION:
                step = AssertStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.AWAIT_TAB:
                step = AwaitTabStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.AWAIT_UPLOAD:
                step = AwaitUploadStep_1.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.CLEAR_COOKIES:
                step = ClearCookiesStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.CLICK:
                step = ClickStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.CLICK_HOLD:
                step = ClickAndHoldStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.CREATE_VARIABLE:
                step = CreateVariableStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.DATABASE_QUERY:
                step = DatabaseQueryStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.DOUBLE_CLICK:
                step = DoubleClickStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.DOWNLOAD:
                step = DownloadStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.DOWNLOAD_PDF:
                step = DownloadPdfStep_1.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.ECHO:
                step = EchoStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.CONDITIONAL_ELSE:
                step = ElseStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.CONDITIONAL_END:
                step = EndStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.ENTER_AUTH_CODE:
                step = EnterAuthCodeStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.ENTER_TEXT:
                step = isMobileFlow
                    ? new MablStepAdapter_1.MablStepAdapter(mablStep)
                    : EnterTextStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.EVALUATE_JS:
                step = EvaluateJSStep_1.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.HOVER:
                step = HoverStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.KEYPRESS:
                step = KeyPressStep_1.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.NAVIGATION:
                step = NavigationStep_1.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.OPEN_EMAIL:
                step = OpenEmailStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.RELEASE:
                step = ReleaseStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.REMOVE_COOKIE:
                step = RemoveCookieStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.RIGHT_CLICK:
                step = RightClickStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.SELECT:
                step = SelectStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.SET_VIEWPORT:
                step = ViewportStep_1.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.SEND_HTTP_REQUEST:
                step = SendHttpRequestStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.SET_COOKIE:
                step = SetCookieStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.SET_FILES:
                step = SetFilesStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.SWITCH_CONTEXT:
                step = SwitchContextToStep_1.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.VISIT_URL:
                step = VisitUrlStep_2.default.generateStepFromMablStep(mablStep, this);
                break;
            case mablscript_step_types_1.StepType.WAIT:
                step = WaitStep_2.default.generateStepFromMablStep(mablStep);
                break;
            case mablscript_step_types_1.StepType.EXECUTE_FLOW:
                step = ExecuteFlowStep_1.default.generateStepFromMablStep(mablStep);
                break;
            default:
                throw new Error(`Unable to map legacy trainer step type [${trainerStep.getStepType()}] to a MablStep from MablStep: [${mablStep.getStepName()}]`);
        }
        const existingStepId = mablStep.stepId();
        if (existingStepId) {
            step.setStepId(existingStepId);
        }
        step.annotations = mablStep.getAnnotation() ?? {};
        return step;
    }
    populateUndefinedVariables(mablStep) {
        const mablStepAsString = JSON.stringify(mablStep);
        const regex = /{{@(?:user.)?([_a-zA-Z0-9]+)(?:\[[0-9]+\])?}}/g;
        let regexMatch;
        while ((regexMatch = regex.exec(mablStepAsString)) !== null) {
            if (regexMatch.index === regex.lastIndex) {
                regex.lastIndex++;
            }
            const variableName = regexMatch[1];
            if (!this.variables.user) {
                this.variables.user = {};
            }
            if (this.variables.user[variableName]) {
                return;
            }
            this.variables.user[variableName] = {
                name: `user.${variableName}`,
                humanizedName: variableName,
                value: undefined,
            };
        }
    }
    convertStepGroupStep(mablStepGroup, isMobileFlow) {
        const convertedSteps = mablStepGroup.steps.map((nestedMablStep) => {
            const step = this.convertMablStepToTrainerStep(nestedMablStep, isMobileFlow);
            step.setDisabled(nestedMablStep.isDisabled());
            return step;
        });
        const intentDescription = mablStepGroup.intent?.description;
        const stepGroup = new ExecuteStepGroupStep_1.default(convertedSteps, intentDescription);
        const existingStepId = mablStepGroup.stepId();
        if (existingStepId) {
            stepGroup.setStepId(existingStepId);
        }
        return stepGroup;
    }
}
exports.default = MablscriptImporter;
