"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwitchContextStep = void 0;
const mablscriptFind_1 = require("../mablscriptFind");
const FindAction_1 = require("../actions/FindAction");
const MablStepV2_1 = require("../MablStepV2");
const SwitchContextStepDescriptor_1 = require("../types/SwitchContextStepDescriptor");
class SwitchContextStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'SwitchContext';
    static actionCode = 'switch_context_to';
    static mablScriptStepNames = [SwitchContextStep.actionCode];
    static yamlMablScriptNames = [SwitchContextStep.stepName];
    findAction;
    frame;
    tab;
    constructor(descriptor) {
        super(SwitchContextStep.stepName, descriptor, SwitchContextStep.actionCode);
        if ((0, SwitchContextStepDescriptor_1.isSwitchRoot)(descriptor) || (0, SwitchContextStepDescriptor_1.isSwitchFrame)(descriptor)) {
            this.frame = descriptor.frame;
        }
        else if ((0, SwitchContextStepDescriptor_1.isSwitchTab)(descriptor)) {
            this.tab = descriptor.tab;
        }
        else {
            throw new Error(`Invalid SwitchContextStepDescriptor: ${JSON.stringify(descriptor)}`);
        }
        if (descriptor.find) {
            this.findAction = FindAction_1.FindAction.findActionFromStepArgs(descriptor);
            this.actions.push(this.findAction);
        }
    }
    validate() {
        if (this.actions.length === 0 ||
            (this.actions.length === 1 && !(this.actions[0] instanceof FindAction_1.FindAction))) {
            throw new Error('Invalid switch context step');
        }
    }
    getStepName() {
        return SwitchContextStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        if (stepArgs.switch) {
            return SwitchContextStep.fromLegacyMablscript([stepArgs.switch], []);
        }
        else if (stepArgs.selector) {
            return SwitchContextStep.fromLegacyMablscript([], [FindAction_1.FindAction.findActionFromStepArgs(stepArgs)]);
        }
        return new SwitchContextStep(stepArgs);
    }
    toMablscript() {
        if ((0, SwitchContextStepDescriptor_1.isSwitchRoot)(this.descriptor) && this.descriptor.frame === 'root') {
            return 'switch_context_to("root")';
        }
        return `${this.findAction?.toMablscript()}.switch_context_to()`;
    }
    getInputVariables() {
        return this.findAction?.getInputVariables() ?? [];
    }
    stepDescription() {
        if ((0, SwitchContextStepDescriptor_1.isSwitchRoot)(this.descriptor) && this.descriptor.frame === 'root') {
            return 'Switch context to the root frame';
        }
        if (this.findAction) {
            const findDescriptor = this.findAction.toDescriptor();
            if (findDescriptor.findType === mablscriptFind_1.FindType.FIND_TAB) {
                if (typeof findDescriptor.findTarget === 'string') {
                    return `Switch context to ${findDescriptor.findTarget} tab`;
                }
                return `Switch context to ${(0, mablscriptFind_1.humanizeTabSelector)(findDescriptor.findTarget)}`;
            }
            else if (findDescriptor.findType === mablscriptFind_1.FindType.FIND_ONE) {
                return `Switch context to ${(0, mablscriptFind_1.humanizeFindOneDescriptor)(findDescriptor.findTarget)}`;
            }
        }
        return 'Switch context';
    }
    static fromLegacyMablscript(args, actions) {
        const maybeRoot = args[0];
        if (maybeRoot !== undefined && maybeRoot !== 'root') {
            throw new Error(`Unexpected argument for ${SwitchContextStep.stepName} step: ${JSON.stringify(maybeRoot)}`);
        }
        if (maybeRoot === 'root') {
            const step = new SwitchContextStep({
                frame: 'root',
            });
            step.actions = actions;
            return step;
        }
        if (actions.length !== 1) {
            throw new Error(`No find action for ${SwitchContextStep.stepName} step`);
        }
        const stepDescriptor = buildStepDescriptor(actions[0]);
        const step = new SwitchContextStep(stepDescriptor);
        step.actions = actions;
        return step;
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new SwitchContextStep(descriptor);
    }
}
exports.SwitchContextStep = SwitchContextStep;
function buildStepDescriptor(findAction) {
    if (!findAction) {
        return {
            frame: 'root',
            actionCode: SwitchContextStep.actionCode,
        };
    }
    const findDescriptor = findAction.toDescriptor();
    if (findDescriptor.findType === mablscriptFind_1.FindType.FIND_TAB) {
        return {
            tab: findDescriptor,
            find: findDescriptor,
            actionCode: SwitchContextStep.actionCode,
        };
    }
    if (findDescriptor.findType === mablscriptFind_1.FindType.FIND_ONE) {
        return {
            frame: findDescriptor,
            find: findDescriptor,
            actionCode: SwitchContextStep.actionCode,
        };
    }
    throw new Error(`Unexpected find type for SwitchContext step: ${findDescriptor.findType}`);
}
