"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordAssertVariableExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
const variable_constants_1 = require("../../../core/execution/variable-constants");
const conditionalClauseUtils_1 = require("./conditionalClauseUtils");
exports.recordAssertVariableExecutor = {
    needsFind: () => false,
    getStepType: () => 'assert-variable',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        if (!isAssertVariableParams(toolParams)) {
            logger.error('RecordAssertVariableExecutor requires variableName, assertionType, and expectedValue parameters');
            return {
                success: false,
            };
        }
        const humanizedName = toolParams.variableName;
        const fullName = humanizedName.includes('.')
            ? humanizedName
            : `${variable_constants_1.USER_NAMESPACE}${humanizedName}`;
        const contextInfo = payload.contextInfo ?? (await stateProvider.getActiveContextInfo());
        const variable = stateProvider.getVariables()[fullName];
        const step = {
            action: 'assert-variable',
            listener: 'assert-variable',
            assertion: {
                assertionType: toolParams.assertionType,
                selectionType: 'variable',
                assertionTarget: 'variable',
                ...(toolParams.conditionalType && {
                    wrapInConditional: true,
                    wrapInConditionalType: toolParams.conditionalType,
                }),
            },
            expectedValue: toolParams.expectedValue,
            variable: variable ?? {
                name: fullName,
                humanizedName,
            },
        };
        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}: ${humanizedName} ${toolParams.assertionType} ${toolParams.expectedValue}`, eventHandler);
        }
        return result;
    },
};
function isAssertVariableParams(toolParams) {
    return (typeof toolParams === 'object' &&
        !!toolParams &&
        'variableName' in toolParams &&
        'assertionType' in toolParams &&
        'expectedValue' in toolParams);
}
