"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VisitUrlStep = void 0;
const MablAction_1 = require("../MablAction");
const mablscriptFind_1 = require("../mablscriptFind");
const MablStepV2_1 = require("../MablStepV2");
const MablSymbol_1 = require("../MablSymbol");
class VisitUrlStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'VisitUrl';
    static actionCode = 'visit_url';
    static mablScriptStepNames = [VisitUrlStep.actionCode];
    static yamlMablScriptNames = [VisitUrlStep.stepName];
    url;
    constructor(descriptor) {
        descriptor.url = VisitUrlStep.sanitizeUrl(descriptor.url);
        super(VisitUrlStep.stepName, descriptor, VisitUrlStep.actionCode);
        this.url = descriptor.url;
    }
    getStepName() {
        return VisitUrlStep.stepName;
    }
    static fromYaml(_stepName, stepDescriptor) {
        return new VisitUrlStep(stepDescriptor);
    }
    static fromLegacyMablscript(args, _actions) {
        const stepDescriptor = {
            url: VisitUrlStep.sanitizeUrl(args[0]),
        };
        const step = new VisitUrlStep(stepDescriptor);
        step.actions = _actions;
        return step;
    }
    toMablscript() {
        const resolvedUrl = this.substituteMablscriptVariable((0, mablscriptFind_1.escapeMablscriptString)(this.url.toString()));
        if (VisitUrlStep.isFileUrl(resolvedUrl) && !VisitUrlStep.isTestOverride()) {
            throw new Error('Access to local files is not permitted');
        }
        return `visit_url(${resolvedUrl})`;
    }
    getInputVariables() {
        return MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.url);
    }
    stepDescription() {
        const inputVariables = this.getInputVariables();
        if (inputVariables.length > 0) {
            const variableName = inputVariables[0];
            return `Visit URL assigned to variable "${variableName}"`;
        }
        return `Visit URL "${this.url}"`;
    }
    static sanitizeUrl(param) {
        const paramStr = (0, MablSymbol_1.isMablSymbolObject)(param) ? (0, MablSymbol_1.formatSymbol)(param) : param;
        const trimmed = paramStr.trim();
        if (trimmed === '') {
            throw new Error('Missing URL');
        }
        if (trimmed.indexOf('[not yet evaluated]') >= 0) {
            throw new Error('Unresolved variable found in URL');
        }
        if (VisitUrlStep.isFileUrl(trimmed)) {
            if (VisitUrlStep.isTestOverride()) {
                return trimmed;
            }
            throw new Error('Access to local files is not permitted');
        }
        return trimmed;
    }
    static isTestOverride() {
        return process.env.MABL_SIMULATION === 'mabl-local-testing';
    }
    static isFileUrl(param) {
        return (/^file:\/\//.test(param) || /^\\\\/.test(param) || /^\/+/.test(param));
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new VisitUrlStep(descriptor);
    }
}
exports.VisitUrlStep = VisitUrlStep;
