"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordAssertElementExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const ConditionDescriptor_1 = require("@mablhq/test-model/types/ConditionDescriptor");
const ExtractDescriptor_1 = require("@mablhq/test-model/types/ExtractDescriptor");
const stepCapture_1 = require("../../background/capture/stepCapture");
const conditionalClauseUtils_1 = require("./conditionalClauseUtils");
exports.recordAssertElementExecutor = {
    needsFind: () => true,
    getStepType: () => 'assert',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall
            .toolParams;
        if (!payload.find || !payload.contextInfo) {
            logger.error('RecordAssertElementExecutor requires find and contextInfo');
            return {
                success: false,
            };
        }
        if (!toolParams?.textToAssert) {
            logger.error('Invalid toolParams for RecordAssertElementExecutor');
            return {
                success: false,
            };
        }
        const step = {
            ...payload.find,
            stepType: undefined,
            listener: 'mouse-select',
            assertedAttrProp: 'innerText',
            expectedValue: toolParams.textToAssert,
            assertion: {
                assertionType: ConditionDescriptor_1.AssertionConditionField.Contains,
                selectionType: 'element',
                assertionTarget: 'element',
                caseInsensitive: true,
                extractionType: ExtractDescriptor_1.ExtractType.EXTRACT_ATTRIBUTE,
                elementAttrProp: 'innerText',
                ...(toolParams.conditionalType && {
                    wrapInConditional: true,
                    wrapInConditionalType: toolParams.conditionalType,
                }),
            },
        };
        const result = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(step, payload.contextInfo, true, undefined, stateProvider, eventHandler, !!toolParams.conditionalType), 0, stateProvider, eventHandler, logger);
        if (toolParams.conditionalType) {
            await (0, conditionalClauseUtils_1.prepopulateConditionalClause)(`${toolParams.conditionalType}: ${toolParams.elementDescription} contains ${toolParams.textToAssert}`, eventHandler);
        }
        return result;
    },
};
