"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStepToV2Format = parseStepToV2Format;
exports.parseFormattedStepToBaseStep = parseFormattedStepToBaseStep;
const uuid_1 = require("uuid");
const importer_1 = require("@mablhq/test-model/importer");
const mablscript_step_types_1 = require("../mablscript-step-types");
const MablscriptImporter_1 = __importDefault(require("./MablscriptImporter"));
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const variable_utils_1 = require("../variable-utils");
const humanize_1 = require("./humanize");
const StepGroupStepName = 'StepGroup';
const StepGroupActionCode = 'step_group';
function parseStepToV2Format(step, isMablscriptFlow, variables, aliasedVariables) {
    let finalStep;
    if ((0, mablscript_step_types_1.isStepGroupStep)(step)) {
        const nestedStepsFormatted = step.stepsInGroup.map((nestedStep) => parseStepToV2Format(nestedStep, isMablscriptFlow, variables, aliasedVariables));
        const stepId = step.id ?? (0, uuid_1.v4)();
        return {
            [StepGroupStepName]: {
                steps: nestedStepsFormatted,
                intent: {
                    description: step.description ?? '',
                },
                actionCode: StepGroupActionCode,
                id: stepId,
                ...(step.disabled ? { disabled: true } : {}),
            },
        };
    }
    else if (!isMablscriptFlow && (0, mablscript_step_types_1.isStepWithMobileCliStep)(step)) {
        finalStep = step.generateMobileCliStep();
    }
    else {
        finalStep = step.generateMablStep();
    }
    if (step.annotations) {
        finalStep.setAnnotation(step.annotations);
    }
    try {
        const humanized = (0, humanize_1.humanizeStepDescription)(step, variables ?? {});
        if (humanized) {
            finalStep.setDescription(humanized);
        }
    }
    catch (e) {
        console.warn('Failed to humanize step description during serialization:', e);
    }
    const trainerStepId = step.id;
    if (trainerStepId) {
        finalStep.setStepId(trainerStepId);
    }
    else if (!finalStep.id) {
        finalStep.setStepId((0, uuid_1.v4)());
    }
    const formatted = finalStep.getFormattedStep();
    const stepId = finalStep.stepId();
    if (stepId) {
        const stepName = finalStep.getStepName();
        const formattedStep = formatted;
        if (formattedStep[stepName] && !formattedStep[stepName].id) {
            formattedStep[stepName].id = stepId;
        }
    }
    if (step.annotations &&
        !step.annotations.description &&
        !step.annotations.note) {
        const stepName = finalStep.getStepName();
        const formattedStep = formatted;
        if (formattedStep[stepName]?.annotation) {
            delete formattedStep[stepName].annotation;
        }
    }
    if (variables) {
        return (0, variable_utils_1.canonicalizeVariablePatternsInObjectWithAliases)(formatted, aliasedVariables ?? (0, variable_utils_1.aliasVariables)(variables));
    }
    return formatted;
}
function parseFormattedStepToBaseStep(formattedStep, variables, isMobile, snippets) {
    const mablSteps = (0, importer_1.interpretStepsFromObjects)([formattedStep], isMobile);
    if (!mablSteps || mablSteps.length === 0) {
        throw new Error('Failed to deserialize step from formatted object');
    }
    const mablStep = mablSteps[0];
    const importer = new MablscriptImporter_1.default({
        steps: [],
        flowSelectors: new mablscriptFind_1.ElementSelectorStore(),
    }, variables || {}, snippets || {});
    const step = importer.convertMablStepToTrainerStep(mablStep, isMobile ?? false);
    step.setDisabled(mablStep.isDisabled());
    return step;
}
