"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablStepAdapter = void 0;
exports.humanizeV2Step = humanizeV2Step;
exports.mablStepAdapterToAIString = mablStepAdapterToAIString;
const MablStepV2_1 = require("@mablhq/test-model/MablStepV2");
const mablscript_step_types_1 = require("../../mablscript-step-types");
const SetOrientationStep_1 = require("@mablhq/test-model/mobile/steps/SetOrientationStep");
const ScrollStep_1 = require("@mablhq/test-model/mobile/steps/ScrollStep");
const NavigateBackStep_1 = require("@mablhq/test-model/mobile/steps/NavigateBackStep");
const NavigateHomeStep_1 = require("@mablhq/test-model/mobile/steps/NavigateHomeStep");
const TapStep_1 = require("@mablhq/test-model/mobile/steps/TapStep");
const EnterTextStep_1 = require("@mablhq/test-model/mobile/steps/EnterTextStep");
const PushFileStep_1 = require("@mablhq/test-model/mobile/steps/PushFileStep");
const PrepareSessionStep_1 = require("@mablhq/test-model/mobile/steps/PrepareSessionStep");
const OpenLinkStep_1 = require("@mablhq/test-model/mobile/steps/OpenLinkStep");
const HideKeyboardStep_1 = require("@mablhq/test-model/mobile/steps/HideKeyboardStep");
const OpenAppStep_1 = require("@mablhq/test-model/mobile/steps/OpenAppStep");
const StepGroupStep_1 = require("@mablhq/test-model/steps/StepGroupStep");
const yaml_utils_1 = require("../yaml-utils");
class MablStepAdapter {
    constructor(mablStep) {
        this.sourceEventIdentifiers = [];
        this.mablStep = mablStep;
        try {
            this.stepType = cliStepTypeToTrainerStepType(mablStep.getStepName());
        }
        catch {
            console.warn(`Unknown step type in trainer conversion: ${mablStep.getStepName()}. Using generic step handling.`);
            this.stepType = mablscript_step_types_1.StepType.EXECUTE_STEP_GROUP;
        }
        this.options = cliStepTypeToDefaultOptions(mablStep.getStepName());
        if (isMablStepV2StepDescriptionCompatible(mablStep)) {
            this.description = mablStep.stepDescription();
        }
        this.disabled = mablStep.isDisabled() ?? false;
    }
    setStepId(id) {
        this.mablStep.setStepId(id);
    }
    get id() {
        return this.mablStep.id ?? '';
    }
    setDisabled(disabled) {
        this.disabled = disabled;
        this.mablStep.setDisabled(disabled);
    }
    mergeOptions(updatedOptions) {
        this.options = {
            ...(this.options ? this.options : {}),
            ...updatedOptions,
        };
    }
    willGenerateJsonSteps() {
        return (0, MablStepV2_1.isMablStepV2)(this.mablStep);
    }
    willGenerateMablscript() {
        return !(0, MablStepV2_1.isMablStepV2)(this.mablStep);
    }
    generateMablscript() {
        return this.mablStep.toMablscript();
    }
    generateMablStep() {
        const clone = Object.assign(Object.create(Object.getPrototypeOf(this.mablStep)), this.mablStep);
        const descriptor = clone.descriptor;
        if (descriptor) {
            clone.descriptor = { ...descriptor };
        }
        return clone;
    }
    inputVariables() {
        return this.mablStep.getInputVariables();
    }
}
exports.MablStepAdapter = MablStepAdapter;
function humanizeV2Step(step) {
    return step.description ?? `Record ${step.mablStep.name} step`;
}
function cliStepTypeToTrainerStepType(cliStepType) {
    switch (cliStepType) {
        case StepGroupStep_1.StepGroupStep.stepName:
            return mablscript_step_types_1.StepType.EXECUTE_STEP_GROUP;
        case EnterTextStep_1.EnterTextStep.stepName:
            return mablscript_step_types_1.StepType.ENTER_TEXT;
        case HideKeyboardStep_1.HideKeyboardStep.stepName:
            return mablscript_step_types_1.StepType.HIDE_KEYBOARD;
        case NavigateBackStep_1.NavigateBackStep.stepName:
            return mablscript_step_types_1.StepType.NAVIGATE_BACK;
        case NavigateHomeStep_1.NavigateHomeStep.stepName:
            return mablscript_step_types_1.StepType.NAVIGATE_HOME;
        case OpenLinkStep_1.OpenLinkStep.stepName:
            return mablscript_step_types_1.StepType.OPEN_DEEP_LINK;
        case OpenAppStep_1.OpenAppStep.stepName:
            return mablscript_step_types_1.StepType.OPEN_APP;
        case PrepareSessionStep_1.PrepareSessionStep.stepName:
            return mablscript_step_types_1.StepType.PREPARE_SESSION;
        case PushFileStep_1.PushFileStep.stepName:
            return mablscript_step_types_1.StepType.PUSH_FILE;
        case ScrollStep_1.ScrollStep.stepName:
            return mablscript_step_types_1.StepType.SCROLL;
        case SetOrientationStep_1.SetOrientationStep.stepName:
            return mablscript_step_types_1.StepType.SET_ORIENTATION;
        case TapStep_1.TapStep.stepName:
            return mablscript_step_types_1.StepType.TAP;
        default:
            throw new Error(`Unexpected step type in cli to trainer conversion: ${cliStepType}`);
    }
}
function cliStepTypeToDefaultOptions(cliStepType) {
    switch (cliStepType) {
        case StepGroupStep_1.StepGroupStep.stepName:
        case OpenLinkStep_1.OpenLinkStep.stepName:
        case PushFileStep_1.PushFileStep.stepName:
        case TapStep_1.TapStep.stepName:
        case EnterTextStep_1.EnterTextStep.stepName:
        case SetOrientationStep_1.SetOrientationStep.stepName:
        case ScrollStep_1.ScrollStep.stepName:
            return { editable: true };
        default:
            return {};
    }
}
function isMablStepV2StepDescriptionCompatible(value) {
    return !!value.stepDescription;
}
function mablStepAdapterToAIString(step) {
    return (0, yaml_utils_1.toYaml)({
        action: step.mablStep.getStepName(),
        description: step.description,
    });
}
