"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetCookieStep = void 0;
const MablStepV2_1 = require("../MablStepV2");
const MablAction_1 = require("../MablAction");
const CookieUtils_1 = require("./CookieUtils");
const mablscriptFind_1 = require("../mablscriptFind");
class SetCookieStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'SetCookie';
    static actionCode = 'set_cookie';
    static mablScriptStepNames = [SetCookieStep.actionCode];
    static yamlMablScriptNames = [SetCookieStep.stepName];
    cookieName;
    cookieValue;
    cookieOptions;
    constructor(descriptor) {
        super(SetCookieStep.stepName, descriptor, SetCookieStep.actionCode);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${SetCookieStep.stepName}: descriptor.name and descriptor.value must be strings`);
        }
        this.cookieName = this.descriptor.name;
        this.cookieValue = this.descriptor.value;
        this.cookieOptions = this.descriptor.options;
    }
    isValidDescriptor(value) {
        return typeof value.name === 'string' && typeof value.value === 'string';
    }
    getStepName() {
        return SetCookieStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        return new SetCookieStep(stepArgs);
    }
    static fromLegacyMablscript(args, _actions) {
        const cookieSelector = args[0];
        const cookieName = CookieUtils_1.CookieUtils.normalizeName(cookieSelector.name);
        const cookieValue = CookieUtils_1.CookieUtils.normalizeValue(cookieSelector.value);
        const cookieOptions = {
            domain: cookieSelector.domain,
            path: cookieSelector.path,
            expires: cookieSelector.expiry,
            httpOnly: cookieSelector.http_only,
            secure: cookieSelector.secure,
            sameSite: cookieSelector.same_site,
            url: cookieSelector.url,
        };
        const hasOptions = Object.values(cookieOptions).filter((optionValue) => optionValue !== undefined).length > 0;
        const stepDescriptor = {
            name: cookieName,
            value: cookieValue,
            options: hasOptions ? cookieOptions : undefined,
            actionCode: SetCookieStep.actionCode,
        };
        const step = new SetCookieStep(stepDescriptor);
        step.actions = _actions;
        return step;
    }
    toMablscript() {
        const cookieSelector = {
            name: this.cookieName,
            value: this.cookieValue,
            domain: this.cookieOptions?.domain,
            path: this.cookieOptions?.path,
            expiry: this.cookieOptions?.expires,
            http_only: this.cookieOptions?.httpOnly,
            secure: this.cookieOptions?.secure,
            same_site: this.cookieOptions?.sameSite,
            url: this.cookieOptions?.url,
        };
        return `set_cookie(${(0, mablscriptFind_1.buildStepArgumentString)({
            params: cookieSelector,
            legacy: false,
        })})`;
    }
    getInputVariables() {
        return (0, MablAction_1.distinctStrings)([
            ...MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.cookieName),
            ...MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.cookieValue),
        ]);
    }
    stepDescription() {
        return `Set cookie: "${this.cookieName}"`;
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new SetCookieStep(descriptor);
    }
}
exports.SetCookieStep = SetCookieStep;
