"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.openMablEmailExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
exports.openMablEmailExecutor = {
    needsFind: () => false,
    getStepType: () => 'open-email',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall.toolParams;
        if (!toolParams || !isOpenMablEmailParams(toolParams)) {
            logger.error('Open mabl email executor requires at least one of: subject, body, from, or mailboxAddress');
            return { success: false };
        }
        const emailSelector = {
            selector_type: 'email',
        };
        if (toolParams.subject) {
            emailSelector.subject = toolParams.subject;
        }
        if (toolParams.body) {
            emailSelector.body_text = toolParams.body;
        }
        if (toolParams.from) {
            emailSelector.from = toolParams.from;
        }
        if (toolParams.mailboxAddress) {
            emailSelector.to_received = toolParams.mailboxAddress;
        }
        const emailFindDescriptor = {
            type: 'email',
            emailSelector,
        };
        if (toolParams.mailboxAddress && toolParams.lookbackMinutes) {
            const emailConfiguration = {
                externalEmailPreferences: {
                    lookbackTimeMs: toolParams.lookbackMinutes * 60 * 1000,
                },
            };
            emailFindDescriptor.emailConfiguration = emailConfiguration;
        }
        const eventMessage = {
            action: 'record',
            listener: 'open-email',
            findDescriptor: emailFindDescriptor,
        };
        const contextInfo = await stateProvider.getActiveContextInfo();
        const result = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(eventMessage, contextInfo, true, payload.toolCall.toolDescription, stateProvider, eventHandler), 0, stateProvider, eventHandler, logger);
        return {
            ...result,
        };
    },
};
function isOpenMablEmailParams(toolParams) {
    return (typeof toolParams === 'object' &&
        (typeof toolParams.subject === 'string' ||
            typeof toolParams.body === 'string' ||
            typeof toolParams.from === 'string' ||
            typeof toolParams.mailboxAddress === 'string'));
}
