"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.openPdfExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
const uuid_1 = require("uuid");
const switchTabHelper_1 = require("./switchTabHelper");
const DownloadBaseStep_1 = require("../../sharedUtils/mablscript/canonical/DownloadBaseStep");
exports.openPdfExecutor = {
    needsFind: () => false,
    getStepType: () => 'download-pdf',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const recentDownload = eventHandler.getRecentDownload?.();
        if (!recentDownload) {
            logger.error('No download detected. The execute_open_pdf tool must be called after an action that triggers a PDF download.');
            return {
                success: false,
                errorMessage: 'No download was detected. Make sure a prior action (e.g., clicking a download link) triggered a PDF download before calling this tool.',
            };
        }
        if (recentDownload.file_mime_type !== 'application/pdf') {
            return {
                success: false,
                errorMessage: `The most recent download "${recentDownload.file_name}" is ${recentDownload.file_mime_type}, not a PDF. ` +
                    'Use execute_assert_download instead for non-PDF files, or ensure a PDF download was triggered before calling this tool.',
            };
        }
        const tabUuid = (0, uuid_1.v4)();
        logger.info(`Using captured download metadata: ${recentDownload.file_name} (${recentDownload.file_size_bytes} bytes)`);
        const assertions = [
            {
                name: DownloadBaseStep_1.DownloadProperty.FileName,
                type: 'string',
                desc: 'File name',
                init: recentDownload.file_name ?? '',
                expected: recentDownload.file_name,
            },
        ];
        if (recentDownload.file_mime_type) {
            assertions.push({
                name: DownloadBaseStep_1.DownloadProperty.MimeType,
                type: 'string',
                desc: 'Mime type',
                init: recentDownload.file_mime_type,
                expected: recentDownload.file_mime_type,
            });
        }
        const eventMessage = {
            action: 'record',
            listener: 'download-pdf',
            downloadForm: {
                detectedProps: {
                    file_name: recentDownload.file_name,
                    file_mime_type: recentDownload.file_mime_type,
                    file_size_bytes: recentDownload.file_size_bytes,
                },
                assertions,
                awaitParams: {
                    uuid: tabUuid,
                },
            },
        };
        const contextInfo = await stateProvider.getActiveContextInfo();
        const openResult = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(eventMessage, contextInfo, true, payload.toolCall.toolDescription, stateProvider, eventHandler), 0, stateProvider, eventHandler, logger);
        if (!openResult.success) {
            return openResult;
        }
        const trainingContext = stateProvider.getTrainingContext();
        trainingContext.trackTrainingTab(tabUuid);
        const switchResult = await (0, switchTabHelper_1.recordSwitchTabAndSync)(tabUuid, stateProvider, eventHandler, logger);
        const allCreatedSteps = [
            ...(openResult.createdStepsInfo ?? []),
            ...(switchResult.createdStepsInfo ?? []),
        ];
        return {
            success: switchResult.success,
            errorMessage: switchResult.errorMessage,
            createdStepsInfo: allCreatedSteps,
        };
    },
};
