"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordAssertUrlExecutor = 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 conditionalClauseUtils_1 = require("./conditionalClauseUtils");
const COMPARISON_TYPE_MAP = {
    equals: ConditionDescriptor_1.AssertionConditionField.Equals,
    contains: ConditionDescriptor_1.AssertionConditionField.Contains,
    starts_with: ConditionDescriptor_1.AssertionConditionField.StartsWith,
    does_not_equal: ConditionDescriptor_1.AssertionConditionField.DoesNotEqual,
    does_not_contain: ConditionDescriptor_1.AssertionConditionField.DoesNotContain,
};
exports.recordAssertUrlExecutor = {
    needsFind: () => false,
    getStepType: () => 'assert',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        if (!isAssertUrlParams(toolParams)) {
            logger.error('RecordAssertUrlExecutor requires urlProperty, expectedValue, and comparisonType parameters');
            return {
                success: false,
            };
        }
        const contextInfo = payload.contextInfo ?? (await stateProvider.getActiveContextInfo());
        const assertionType = COMPARISON_TYPE_MAP[toolParams.comparisonType];
        if (!assertionType) {
            logger.warn(`Unknown comparisonType "${toolParams.comparisonType}", defaulting to equals`);
        }
        const finalAssertionType = assertionType ?? ConditionDescriptor_1.AssertionConditionField.Equals;
        const step = {
            action: 'record',
            listener: 'location',
            url: undefined,
            assertedAttrProp: toolParams.urlProperty,
            expectedValue: toolParams.expectedValue,
            assertion: {
                assertionType: finalAssertionType,
                selectionType: 'location',
                caseInsensitive: false,
                ...(toolParams.conditionalType && {
                    wrapInConditional: true,
                    wrapInConditionalType: toolParams.conditionalType,
                }),
            },
        };
        const result = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(step, contextInfo, true, payload.toolCall.toolDescription, stateProvider, eventHandler, !!toolParams.conditionalType), 0, stateProvider, eventHandler, logger);
        if (toolParams.conditionalType) {
            await (0, conditionalClauseUtils_1.prepopulateConditionalClause)(`${toolParams.conditionalType}: ${toolParams.urlProperty} ${toolParams.comparisonType} ${toolParams.expectedValue}`, eventHandler);
        }
        return result;
    },
};
function isAssertUrlParams(toolParams) {
    return (typeof toolParams === 'object' &&
        !!toolParams &&
        'urlProperty' in toolParams &&
        'expectedValue' in toolParams &&
        'comparisonType' in toolParams);
}
