'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateHumanizedSteps = generateHumanizedSteps;
exports.humanizeStepDescription = humanizeStepDescription;
exports.humanizeMablscriptForSave = humanizeMablscriptForSave;
const mablscript_step_types_1 = require("../mablscript-step-types");
const type_utils_1 = require("../type-utils");
const variable_utils_1 = require("../variable-utils");
const FlowValidation_1 = __importDefault(require("./FlowValidation"));
const ExecuteFlowStep_1 = require("./extended/ExecuteFlowStep");
const ExecuteStepGroupStep_1 = require("./extended/ExecuteStepGroupStep");
const AccessibilityCheckStep_1 = require("./canonical/AccessibilityCheckStep");
const DownloadBaseStep_1 = require("./canonical/DownloadBaseStep");
const AssertVariableStep_1 = require("./canonical/AssertVariableStep");
const AssertStep_1 = require("./canonical/AssertStep");
const AwaitTabStep_1 = require("./canonical/AwaitTabStep");
const ClearCookiesStep_1 = require("./canonical/ClearCookiesStep");
const ClickStep_1 = require("./canonical/ClickStep");
const ClickAndHoldStep_1 = require("./canonical/ClickAndHoldStep");
const ElseStep_1 = require("./canonical/ElseStep");
const EndStep_1 = require("./canonical/EndStep");
const CreateVariableStep_1 = require("./canonical/CreateVariableStep");
const DatabaseQueryStep_1 = require("./canonical/DatabaseQueryStep");
const MablStepAdapter_1 = require("./canonical/MablStepAdapter");
const DoubleClickStep_1 = require("./canonical/DoubleClickStep");
const DownloadPdfStep_1 = require("./canonical/DownloadPdfStep");
const EchoStep_1 = require("./canonical/EchoStep");
const EnterAuthCodeStep_1 = require("./canonical/EnterAuthCodeStep");
const EnterTextStep_1 = require("./canonical/EnterTextStep");
const EvaluateJSStep_1 = require("./canonical/EvaluateJSStep");
const HoverStep_1 = require("./canonical/HoverStep");
const KeyPressStep_1 = require("./canonical/KeyPressStep");
const NavigationStep_1 = require("./canonical/NavigationStep");
const OpenEmailStep_1 = require("./canonical/OpenEmailStep");
const ReleaseStep_1 = require("./canonical/ReleaseStep");
const RemoveCookieStep_1 = require("./canonical/RemoveCookieStep");
const RightClickStep_1 = require("./canonical/RightClickStep");
const SelectStep_1 = require("./canonical/SelectStep");
const SendHttpRequestStep_1 = require("./canonical/SendHttpRequestStep");
const SetCookieStep_1 = require("./canonical/SetCookieStep");
const SetFilesStep_1 = require("./canonical/SetFilesStep");
const SwitchContextToStep_1 = require("./canonical/SwitchContextToStep");
const ViewportStep_1 = require("./canonical/ViewportStep");
const VisitUrlStep_1 = require("./canonical/VisitUrlStep");
const WaitStep_1 = require("./canonical/WaitStep");
const humanizationByType = {
    'accessibility-check': AccessibilityCheckStep_1.humanizeAccessibilityCheck,
    'assert-variable': AssertVariableStep_1.humanizeAssertVariableStep,
    assertion: AssertStep_1.humanizeAssertStep,
    'await-tab': AwaitTabStep_1.humanizeAwaitTabStep,
    await_download: DownloadBaseStep_1.humanizeDownloadBaseStep,
    'await-uploads': DownloadBaseStep_1.humanizeDownloadBaseStep,
    'clear-cookies': ClearCookiesStep_1.humanizeClearCookiesStep,
    click: ClickStep_1.humanizeClickStep,
    click_hold: ClickAndHoldStep_1.humanizeClickAndHoldStep,
    conditional_else: ElseStep_1.humanizeElseStep,
    conditional_end: EndStep_1.humanizeEndStep,
    create_variable: CreateVariableStep_1.humanizeCreateVariableStep,
    database_query: DatabaseQueryStep_1.humanizeDatabaseQueryStep,
    'deep-link': MablStepAdapter_1.humanizeV2Step,
    double_click: DoubleClickStep_1.humanizeDoubleClickStep,
    download: DownloadBaseStep_1.humanizeDownloadBaseStep,
    'download-pdf': DownloadPdfStep_1.humanizeDownloadPdfStep,
    execute_flow: ExecuteFlowStep_1.humanizeExecuteFlow,
    execute_step_group: ExecuteStepGroupStep_1.humanizeExecuteStepGroupStep,
    echo: EchoStep_1.humanizeEchoStep,
    enter_auth_code: EnterAuthCodeStep_1.humanizeEnterAuthCodeStep,
    enter_text: EnterTextStep_1.humanizeEnterTextStep,
    'evaluate-js': EvaluateJSStep_1.humanizeEvaluateJSStep,
    hide_keyboard: MablStepAdapter_1.humanizeV2Step,
    hover: HoverStep_1.humanizeHoverStep,
    keypress: KeyPressStep_1.humanizeKeyPressStep,
    navigate_back: MablStepAdapter_1.humanizeV2Step,
    navigate_home: MablStepAdapter_1.humanizeV2Step,
    navigation: NavigationStep_1.humanizeNavigationStep,
    'open-app': MablStepAdapter_1.humanizeV2Step,
    'open-email': OpenEmailStep_1.humanizeOpenEmailStep,
    'prepare-session': MablStepAdapter_1.humanizeV2Step,
    'push-file': MablStepAdapter_1.humanizeV2Step,
    release: ReleaseStep_1.humanizeReleaseStep,
    'remove-cookie': RemoveCookieStep_1.humanizeRemoveCookieStep,
    right_click: RightClickStep_1.humanizeRightClickStep,
    scroll: MablStepAdapter_1.humanizeV2Step,
    select: SelectStep_1.humanizeSelectStep,
    'send-http-request': SendHttpRequestStep_1.humanizeSendHttpRequestStep,
    'set-cookie': SetCookieStep_1.humanizeSetCookieStep,
    'set-files': SetFilesStep_1.humanizeSetFilesStep,
    set_orientation: MablStepAdapter_1.humanizeV2Step,
    'switch-context-to': SwitchContextToStep_1.humanizeSwitchContextStep,
    tap: MablStepAdapter_1.humanizeV2Step,
    viewport: ViewportStep_1.humanizeViewportStep,
    visit_url: VisitUrlStep_1.humanizeVisitUrlStep,
    wait: WaitStep_1.humanizeWaitStep,
};
function generateHumanizedSteps(stepIndexOfFlow, steps, variables) {
    if (!variables) {
        console.trace('Variables are not provided. Humanization will not be 100% at');
    }
    const humanizedSteps = [];
    const flowValidator = new FlowValidation_1.default();
    function addHumanizedStep(input) {
        const { stepIndex, step } = input;
        const visibleIndex = stepIndex
            .map((index) => index + 1)
            .join('.');
        const flow = (0, mablscript_step_types_1.isStepWithReusableFlow)(step) ? step.flow : undefined;
        const humanizedStep = humanizeStep(step, variables, flow, stepIndex, visibleIndex);
        flowValidator.processStep(step, humanizedStep, flow);
        humanizedSteps.push(humanizedStep);
    }
    steps.forEach((step, index) => addHumanizedStep({ stepIndex: [...stepIndexOfFlow, index], step }));
    flowValidator.finalizeValidations(humanizedSteps);
    return humanizedSteps.filter((humanizedStep) => humanizedStep.step);
}
function humanizeStepDescription(step, variables) {
    const humanizationFunction = (0, mablscript_step_types_1.isMablStepAdapter)(step)
        ? (0, MablStepAdapter_1.humanizeV2Step)(step)
        : humanizationByType[step.stepType](step, false);
    return (0, variable_utils_1.humanizeVariablePattern)(variables, humanizationFunction ?? ((step) => step.stepType));
}
function humanizeStep(step, variables, flow, stepIndex, visibleIndex) {
    if ((0, mablscript_step_types_1.isStepGroupStep)(step)) {
        const humanizedSteps = step.stepsInGroup.map((step, index) => {
            const humanizedStep = humanizeStep(step, variables, flow, [...stepIndex, index], `${visibleIndex}.${index + 1}`);
            return humanizedStep;
        });
        return {
            step: humanizeStepDescription(step, variables),
            stepsInGroup: humanizedSteps,
            stepType: step.stepType,
            stepIndex,
            visibleIndex,
            isDisabledStep: step.disabled,
            isExpanded: !!step.isExpanded,
            isAutoLoginFlow: (0, type_utils_1.isAutoLoginFlow)(flow),
            collapsed: step.collapsed,
            flowId: (0, mablscript_step_types_1.isStepWithReusableFlow)(step) ? step.flow?.id : flow?.id,
            flowInstanceId: (0, mablscript_step_types_1.isStepWithReusableFlow)(step)
                ? step.instanceId
                : undefined,
            flowInvariantId: (0, mablscript_step_types_1.isStepWithReusableFlow)(step)
                ? step.flowInvariantId
                : flow?.invariant_id,
            userDescription: step.annotations?.description,
            validation: [],
        };
    }
    return {
        step: humanizeStepDescription(step, variables),
        stepType: step.stepType,
        stepIndex,
        visibleIndex,
        isDisabledStep: step.disabled,
        isExpanded: !!step.isExpanded,
        isAutoLoginFlow: (0, type_utils_1.isAutoLoginFlow)(flow),
        collapsed: (0, mablscript_step_types_1.isStepWithReusableFlow)(step) && step.collapsed,
        flowId: (0, mablscript_step_types_1.isStepWithReusableFlow)(step) ? step.flow?.id : flow?.id,
        flowInstanceId: (0, mablscript_step_types_1.isStepWithReusableFlow)(step) ? step.instanceId : undefined,
        flowInvariantId: (0, mablscript_step_types_1.isStepWithReusableFlow)(step)
            ? step.flowInvariantId
            : flow?.invariant_id,
        userDescription: step.annotations?.description,
        validation: [],
        assertionType: (0, mablscript_step_types_1.stepIsStepWithAssertion)(step)
            ? step.assertionType
            : undefined,
    };
}
function humanizeMablscriptForSave(steps, variables = {}) {
    if (!steps) {
        return '';
    }
    return generateHumanizedSteps([], steps, variables)
        .map((step) => step.step)
        .join('\n');
}
