"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordAssertionExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const generation_types_1 = require("../../sharedUtils/generation-types");
const ExtractDescriptor_1 = require("@mablhq/test-model/types/ExtractDescriptor");
const AssertStep_1 = require("@mablhq/test-model/steps/AssertStep");
const stepExecution_1 = require("../../sharedUtils/stepExecution");
const stepCapture_1 = require("../../background/capture/stepCapture");
const conditionalClauseUtils_1 = require("./conditionalClauseUtils");
exports.recordAssertionExecutor = {
    needsFind: () => false,
    getStepType: () => 'assert',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        if (!isAssertToolParams(toolParams)) {
            logger.error('Record assertion tool executor requires assert step params');
            return {
                success: false,
            };
        }
        const step = {
            selectionType: 'viewport',
            listener: 'viewport-assert',
            findDescriptor: {
                type: 'viewport',
            },
            action: 'record',
        };
        step.assertion = {
            assertionType: 'ai_prompt',
            assertionTarget: 'element',
            aiPrompt: {
                userPrompt: toolParams.assertionDescription,
                criteria: [],
            },
            extractionType: ExtractDescriptor_1.ExtractType.EXTRACT_PROMPT,
            onFailure: AssertStep_1.OnFailure.FailTestAtEnd,
            selectionType: 'viewport',
            elementScreenshot: false,
            ...(toolParams.conditionalType && {
                wrapInConditional: true,
                wrapInConditionalType: toolParams.conditionalType,
            }),
        };
        const contextInfo = payload.contextInfo ?? (await stateProvider.getActiveContextInfo());
        const findDescriptor = { type: 'viewport' };
        const variables = stateProvider.getVariables();
        const testAssertionResult = await (0, stepExecution_1.runAIAssertion)(eventHandler, variables, findDescriptor, step.assertion, contextInfo.frameId, `${contextInfo.tabId}`);
        step.assertion.aiPrompt.criteria =
            testAssertionResult.assertResult?.assertionCriteria ?? [];
        const latestTestGenStatus = stateProvider.getTestGenerationStatus();
        const latestTestGenSessionId = stateProvider.getActiveSession()?.sessionId;
        if (latestTestGenStatus !== generation_types_1.TestGenerationStatus.LearningSteps ||
            latestTestGenSessionId !== payload.session.sessionId) {
            return {
                success: false,
            };
        }
        const result = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(step, contextInfo, true, undefined, stateProvider, eventHandler, !!toolParams.conditionalType), 0, stateProvider, eventHandler, logger);
        if (toolParams.conditionalType) {
            await (0, conditionalClauseUtils_1.prepopulateConditionalClause)(`${toolParams.conditionalType}: ${toolParams.assertionDescription}`, eventHandler);
        }
        return result;
    },
};
function isAssertToolParams(toolParams) {
    return toolParams?.assertionDescription !== undefined;
}
