"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.humanizeVisitUrlStep = humanizeVisitUrlStep;
exports.visitUrlStepToAIString = visitUrlStepToAIString;
const BaseStep_1 = __importDefault(require("./BaseStep"));
const variable_names_util_1 = require("../../variable-names-util");
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const type_utils_1 = require("../../type-utils");
const mablscript_step_types_1 = require("../../mablscript-step-types");
const MablAction_1 = require("@mablhq/test-model/MablAction");
const yaml_utils_1 = require("../yaml-utils");
class VisitUrlStep extends BaseStep_1.default {
    static getStepType() {
        return mablscript_step_types_1.StepType.VISIT_URL;
    }
    constructor(url, variable) {
        super(VisitUrlStep.getStepType());
        this.url = url;
        this.variable = variable;
        this.options = {
            executionContext: 'background',
            editable: true,
        };
    }
    static getAdditionalDetailsList() {
        return 'No additional info';
    }
    generateMablscript() {
        const urlArg = this.variable
            ? this.variable.name
            : `"${(0, mablscriptFind_1.escapeMablscriptString)(this.url)}"`;
        return `visit_url(${urlArg})`;
    }
    inputVariables() {
        if (this.variable?.name) {
            return [this.variable.name];
        }
        return (0, type_utils_1.distinct)((0, variable_names_util_1.extractVariableNamesFromPattern)(this.url));
    }
    static generateStepFromMablStep(mablVisitUrlStep, importer) {
        const visitUrlArg = mablVisitUrlStep.url;
        if ((0, MablAction_1.argumentIsMablVariable)(mablVisitUrlStep.url)) {
            const extractedVariable = this.maybeExtractVariable(mablVisitUrlStep.url, importer.variables);
            if (typeof extractedVariable?.value === 'string') {
                return new VisitUrlStep(extractedVariable?.value, extractedVariable);
            }
        }
        return new VisitUrlStep(visitUrlArg);
    }
    static create(descriptor) {
        const step = new VisitUrlStep(descriptor.url);
        step.generateStepId();
        return step;
    }
}
exports.default = VisitUrlStep;
function humanizeVisitUrlStep(step) {
    if (step.variable) {
        const varDisplay = step.variable.humanizedName || step.variable.name;
        return `Visit URL assigned to variable "${varDisplay}"`;
    }
    return `Visit URL "${step.url}"`;
}
function visitUrlStepToAIString(step) {
    if (step.variable) {
        return (0, yaml_utils_1.toYaml)({
            stepType: step.stepType,
            variable: step.variable.humanizedName || step.variable.name,
        });
    }
    return (0, yaml_utils_1.toYaml)({
        stepType: step.stepType,
        url: step.url,
    });
}
