"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AwaitTabStep = void 0;
exports.hasCustomFindOptions = hasCustomFindOptions;
const mablscriptFind_1 = require("../mablscriptFind");
const MablAction_1 = require("../MablAction");
const MablStepV2_1 = require("../MablStepV2");
class AwaitTabStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'AwaitTab';
    static actionCode = 'await_tab';
    static mablScriptStepNames = [AwaitTabStep.actionCode];
    static yamlMablScriptNames = [AwaitTabStep.stepName];
    tab;
    constructor(descriptor) {
        super(AwaitTabStep.stepName, descriptor, AwaitTabStep.actionCode);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${AwaitTabStep.stepName}: descriptor.tab must be defined`);
        }
        this.tab = this.descriptor.tab;
    }
    isValidDescriptor(value) {
        return value.tab !== undefined;
    }
    getStepName() {
        return AwaitTabStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        return new AwaitTabStep(stepArgs);
    }
    static fromLegacyMablscript(args, actions) {
        const stepDescriptor = {
            actionCode: AwaitTabStep.actionCode,
            tab: args[0],
        };
        const step = new AwaitTabStep(stepDescriptor);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        return `await_tab(${AwaitTabStep.getMablscriptSelectorFromTabFindAction(this.tab)})`;
    }
    getInputVariables() {
        return MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.tab);
    }
    stepDescription() {
        return `Wait for ${(0, mablscriptFind_1.humanizeTabSelector)(this.tab)}`;
    }
    static hasCustomFindOptions(tab) {
        return Object.keys(tab.configuration?.propertyPreferences ?? {}).length > 0;
    }
    static getMablscriptSelectorFromTabFindAction(tab) {
        const mablscriptSelectorChunks = ['selector_type: "tab"'];
        if (tab.title !== undefined) {
            mablscriptSelectorChunks.push(`title: "${(0, mablscriptFind_1.escapeMablscriptString)(tab.title)}"`);
        }
        if (tab.url !== undefined) {
            mablscriptSelectorChunks.push(`url: "${(0, mablscriptFind_1.escapeMablscriptString)(tab.url)}"`);
        }
        if (tab.uuid !== undefined) {
            mablscriptSelectorChunks.push(`uuid: "${(0, mablscriptFind_1.escapeMablscriptString)(tab.uuid)}"`);
        }
        let descriptor = mablscriptSelectorChunks.join(', ');
        if (tab.configuration) {
            descriptor = `${descriptor}, ${AwaitTabStep.getConfigurationString(tab)}`;
        }
        return `{${descriptor}}`;
    }
    static getConfigurationString(tabSelector) {
        let configString;
        if (hasCustomFindOptions(tabSelector)) {
            configString = (0, mablscriptFind_1.buildStepArgumentString)({
                params: { ...tabSelector.configuration },
                legacy: true,
            });
        }
        if (configString) {
            return `configuration: ${configString}`;
        }
        return '';
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new AwaitTabStep(descriptor);
    }
}
exports.AwaitTabStep = AwaitTabStep;
function hasCustomFindOptions(tab) {
    return AwaitTabStep.hasCustomFindOptions(tab);
}
