"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordAssertCookieExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const ConditionDescriptor_1 = require("@mablhq/test-model/types/ConditionDescriptor");
const stepCapture_1 = require("../../background/capture/stepCapture");
const AssertStep_1 = require("@mablhq/test-model/steps/AssertStep");
const COMPARISON_TYPE_MAP = {
    equals: ConditionDescriptor_1.AssertionConditionField.Equals,
    does_not_equal: ConditionDescriptor_1.AssertionConditionField.DoesNotEqual,
    contains: ConditionDescriptor_1.AssertionConditionField.Contains,
    does_not_contain: ConditionDescriptor_1.AssertionConditionField.DoesNotContain,
    starts_with: ConditionDescriptor_1.AssertionConditionField.StartsWith,
    ends_with: ConditionDescriptor_1.AssertionConditionField.EndsWith,
    matches_regex: ConditionDescriptor_1.AssertionConditionField.MatchesRegex,
};
exports.recordAssertCookieExecutor = {
    needsFind: () => false,
    getStepType: () => 'assert',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        if (!isAssertCookieParams(toolParams)) {
            logger.error('RecordAssertCookieExecutor requires cookieName, cookieAttribute, assertionType, and expectedValue parameters');
            return { success: false };
        }
        const contextInfo = payload.contextInfo ?? (await stateProvider.getActiveContextInfo());
        const mappedAssertionType = COMPARISON_TYPE_MAP[toolParams.assertionType];
        if (!mappedAssertionType) {
            logger.warn(`Unknown assertionType "${toolParams.assertionType}", defaulting to equals`);
        }
        const finalAssertionType = mappedAssertionType ?? ConditionDescriptor_1.AssertionConditionField.Equals;
        const step = {
            action: 'record',
            listener: 'assert-cookie',
            findDescriptor: {
                type: 'cookie',
                cookieSelector: { name: toolParams.cookieName },
            },
            assertedAttrProp: toolParams.cookieAttribute,
            expectedValue: toolParams.expectedValue,
            assertion: {
                assertionType: finalAssertionType,
                assertionTarget: 'element',
                selectionType: 'cookie',
                caseInsensitive: false,
                onFailure: AssertStep_1.OnFailure.FailImmediately,
                elementScreenshot: false,
            },
        };
        return (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(step, contextInfo, true, payload.toolCall.toolDescription, stateProvider, eventHandler), 0, stateProvider, eventHandler, logger);
    },
};
function isAssertCookieParams(toolParams) {
    return (typeof toolParams === 'object' &&
        !!toolParams &&
        'cookieName' in toolParams &&
        'cookieAttribute' in toolParams &&
        'assertionType' in toolParams &&
        'expectedValue' in toolParams);
}
