"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnterTextStep = void 0;
const typescript_api_client_1 = require("@mablhq-internal/typescript-api-client");
const MablAction_1 = require("../MablAction");
const MablSymbol_1 = require("../MablSymbol");
const mablscriptFind_1 = require("../mablscriptFind");
const MablStepWithFindAction_1 = require("../MablStepWithFindAction");
const ActionsUtils_1 = require("./ActionsUtils");
class EnterTextStep extends MablStepWithFindAction_1.MablStepWithFindAction {
    static stepName = 'EnterText';
    static actionCode = 'enter_text';
    static mablScriptStepNames = [EnterTextStep.actionCode];
    static yamlMablScriptNames = [EnterTextStep.stepName];
    text;
    constructor(descriptor, findAction) {
        super(EnterTextStep.stepName, descriptor, EnterTextStep.actionCode, findAction);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${EnterTextStep.stepName}: descriptor.text must be a string or MablSymbol`);
        }
        this.text = this.descriptor.text;
        if (this.isPasswordField() && this.description) {
            const masked = this.stepDescription();
            this.description = masked;
            this.descriptor.description = masked;
        }
    }
    isValidDescriptor(value) {
        return typeof value.text === 'string' || value.text instanceof MablSymbol_1.MablSymbol;
    }
    getStepName() {
        return EnterTextStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        if (stepArgs.text?.name) {
            stepArgs.text = new MablSymbol_1.MablSymbol(stepArgs.text.name);
        }
        return new EnterTextStep(stepArgs);
    }
    static fromLegacyMablscript(args, actions) {
        const findAction = ActionsUtils_1.ActionsUtils.validateSingleFindAction(actions);
        const textArg = args[0];
        const text = textArg?.name
            ? new MablSymbol_1.MablSymbol(textArg.name)
            : (0, MablAction_1.parseArgument)(textArg, false);
        const stepDescriptor = {
            find: findAction.toDescriptor(),
            text,
            actionCode: EnterTextStep.actionCode,
        };
        const optionsArg = args[1];
        if (optionsArg && typeof optionsArg === 'object') {
            const source = optionsArg;
            const mode = source.enter_text_mode;
            if (mode === typescript_api_client_1.EnterTextModeEnum.Fill || mode === typescript_api_client_1.EnterTextModeEnum.Press) {
                stepDescriptor.enterTextMode = mode;
            }
            if (source.do_not_retry === true) {
                stepDescriptor.doNotRetry = true;
            }
        }
        const step = new EnterTextStep(stepDescriptor, findAction);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        const enterText = typeof this.text === 'string'
            ? `"${(0, mablscriptFind_1.escapeMablscriptString)(this.text)}"`
            : this.substituteMablscriptVariable(this.text);
        const optionsString = serializeEnterTextOptions(this.descriptor);
        return `${this.findAction.toMablscript()}.enter_text(${enterText}${optionsString})`;
    }
    getInputVariables() {
        return (0, MablAction_1.distinctStrings)([
            ...this.findAction.getInputVariables(),
            ...MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.text),
        ]);
    }
    stepDescription() {
        const elementDescription = this.humanizeFind();
        if (this.isPasswordField()) {
            return `Enter a password into ${elementDescription}`;
        }
        const displayText = typeof this.text === 'string'
            ? `"${this.text}"`
            : `{{${this.text.name}}}`;
        const verb = this.descriptor.enterTextMode === typescript_api_client_1.EnterTextModeEnum.Fill
            ? 'Fill'
            : 'Enter';
        return `${verb} ${displayText} into ${elementDescription}`;
    }
    isPasswordField() {
        const findDescriptor = this.findAction.findDescriptor;
        if (findDescriptor.findType !== mablscriptFind_1.FindType.FIND_ONE) {
            return false;
        }
        const selector = findDescriptor.findTarget?.selector;
        return selector?.inputElementType === 'password';
    }
}
exports.EnterTextStep = EnterTextStep;
function serializeEnterTextOptions(descriptor) {
    const entries = [];
    if (descriptor.enterTextMode !== undefined) {
        entries.push(`enter_text_mode: "${descriptor.enterTextMode}"`);
    }
    if (descriptor.doNotRetry === true) {
        entries.push('do_not_retry: true');
    }
    return entries.length === 0 ? '' : `, {${entries.join(', ')}}`;
}
