"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.humanizeSetCookieStep = humanizeSetCookieStep;
exports.setCookieStepToAIString = setCookieStepToAIString;
const mablscript_step_types_1 = require("../../mablscript-step-types");
const type_utils_1 = require("../../type-utils");
const variable_names_util_1 = require("../../variable-names-util");
const BaseStep_1 = __importDefault(require("./BaseStep"));
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const yaml_utils_1 = require("../yaml-utils");
class SetCookieStep extends BaseStep_1.default {
    static getStepType() {
        return mablscript_step_types_1.StepType.SET_COOKIE;
    }
    constructor(cookie) {
        super(SetCookieStep.getStepType());
        this.cookie = cookie;
        this.options = {
            editable: true,
            executionContext: 'background',
        };
    }
    generateMablscript() {
        return `set_cookie(${this.generateCookieIngredients()})`;
    }
    generateCookieIngredients() {
        const stringifiedCookie = (0, mablscriptFind_1.buildStepArgumentString)({
            params: this.cookie,
            legacy: false,
        });
        if (!stringifiedCookie) {
            throw new Error('Invalid step; cookie cannot be generated.');
        }
        return stringifiedCookie;
    }
    inputVariables() {
        const patterns = [
            this.cookie?.name,
            this.cookie?.value,
            this.cookie?.domain,
            this.cookie?.path,
            this.cookie?.url,
        ].filter(type_utils_1.isDefined);
        const variableNames = [];
        patterns.forEach((pattern) => variableNames.push(...(0, variable_names_util_1.extractVariableNamesFromPattern)(pattern)));
        return (0, type_utils_1.distinct)(variableNames);
    }
    static generateStepFromMablStep(mablSetCookieStep) {
        return new SetCookieStep({
            name: mablSetCookieStep.cookieName,
            value: mablSetCookieStep.cookieValue,
            domain: mablSetCookieStep.cookieOptions?.domain,
            path: mablSetCookieStep.cookieOptions?.path,
            expiry: mablSetCookieStep.cookieOptions?.expires,
            http_only: mablSetCookieStep.cookieOptions?.httpOnly,
            secure: mablSetCookieStep.cookieOptions?.secure,
            same_site: mablSetCookieStep.cookieOptions?.sameSite,
            url: mablSetCookieStep.cookieOptions?.url,
        });
    }
    static create(descriptor) {
        const step = new SetCookieStep({
            name: descriptor.name,
            value: descriptor.value,
            domain: descriptor.options?.domain,
            path: descriptor.options?.path,
            expiry: descriptor.options?.expires,
            http_only: descriptor.options?.httpOnly,
            secure: descriptor.options?.secure,
            same_site: descriptor.options?.sameSite,
            url: descriptor.options?.url,
        });
        step.generateStepId();
        return step;
    }
}
exports.default = SetCookieStep;
function humanizeSetCookieStep(step) {
    if (!step.cookie) {
        throw new Error('Invalid step; humanized description cannot be generated.');
    }
    let humanizedString = '';
    humanizedString += `Set cookie "${step.cookie.name}" with value "${step.cookie.value}"`;
    if (Object.keys(step.cookie)
        .filter((key) => key !== 'name' && key !== 'value')
        .filter((key) => step.cookie[key] !== undefined)
        .length > 0) {
        humanizedString += ` and advanced options`;
    }
    return humanizedString;
}
function setCookieStepToAIString(step) {
    return (0, yaml_utils_1.toYaml)({
        stepType: step.stepType,
        name: step.cookie?.name,
        value: step.cookie?.value,
        domain: step.cookie?.domain,
        path: step.cookie?.path,
        secure: step.cookie?.secure,
        http_only: step.cookie?.http_only,
        same_site: step.cookie?.same_site,
        expiry: step.cookie?.expiry,
    });
}
