"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectStep = void 0;
const FindAction_1 = require("../actions/FindAction");
const MablAction_1 = require("../MablAction");
const mablscriptFind_1 = require("../mablscriptFind");
const MablStepWithFindAction_1 = require("../MablStepWithFindAction");
class SelectStep extends MablStepWithFindAction_1.MablStepWithFindAction {
    static stepName = 'Select';
    static actionCode = 'select';
    static mablScriptStepNames = [SelectStep.actionCode];
    static yamlMablScriptNames = [SelectStep.stepName];
    constructor(descriptor, findAction) {
        super(SelectStep.stepName, descriptor, SelectStep.actionCode, findAction);
    }
    static validateActions(actions) {
        if (actions.length !== 1 || !(actions[0] instanceof FindAction_1.FindAction)) {
            throw new Error('Select steps should have exactly one sub action and it should be a find');
        }
        return actions[0];
    }
    getStepName() {
        return SelectStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        return new SelectStep(stepArgs);
    }
    static fromLegacyMablscript(args, actions) {
        const selectOptions = args[0];
        const selectOptionType = args[1];
        const findAction = SelectStep.validateActions(actions);
        const stepDescriptor = buildStepDescriptor(findAction, selectOptions, selectOptionType);
        const step = new SelectStep(stepDescriptor, findAction);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        const descriptor = this.descriptor;
        const selectOptionType = descriptor.selectOptionType;
        const selectOptions = {
            index: descriptor.index,
            position: descriptor.position,
            selector_type: descriptor.selector_type,
            text: descriptor.text,
            uuid: descriptor.uuid,
            value: descriptor.value,
        };
        const mablscriptSelectOptions = (0, MablAction_1.convertObjectToMablscriptArgs)(selectOptions);
        const mablscriptSelectType = selectOptionType
            ? `, "${selectOptionType}"`
            : '';
        const mablscriptSelectArgs = mablscriptSelectOptions + mablscriptSelectType;
        return `${this.findAction.toMablscript()}.select(${mablscriptSelectArgs})`;
    }
    getInputVariables() {
        return (0, MablAction_1.distinctStrings)([
            ...this.findAction.getInputVariables(),
            ...MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.descriptor),
        ]);
    }
    stepDescription() {
        let selectionDescription = 'option';
        const descriptor = this.descriptor;
        if (descriptor.value) {
            selectionDescription = `"${descriptor.value}"`;
        }
        else if (descriptor.text) {
            selectionDescription = `"${descriptor.text}"`;
        }
        else if (descriptor.index !== undefined) {
            selectionDescription = `option ${descriptor.index}`;
        }
        const elementDescription = this.humanizeFind();
        return `Select ${selectionDescription} from ${elementDescription}`;
    }
}
exports.SelectStep = SelectStep;
function buildStepDescriptor(findAction, selectOptions, selectOptionType) {
    const find = findAction.toDescriptor();
    switch (find.findType) {
        case mablscriptFind_1.FindType.FIND_FIRST:
        case mablscriptFind_1.FindType.FIND_LAST:
        case mablscriptFind_1.FindType.FIND_ANY:
        case mablscriptFind_1.FindType.FIND_ONE:
            return {
                find,
                selectOptionType,
                ...selectOptions,
            };
        default:
            throw new Error(`Error generating step descriptor for ${SelectStep.stepName}: Unexpected find type ${find.findType}`);
    }
}
