"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadFileExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
const UPLOAD_STEP_WAIT_TIME_MS = 3_000;
exports.uploadFileExecutor = {
    needsFind: () => true,
    getStepType: () => 'set-files',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        if (!payload.find || !payload.contextInfo) {
            logger.error('Upload file executor requires find and contextInfo');
            return {
                success: false,
                errorMessage: 'Could not locate the file input element. Retry using customFind with selector "input[type=\\"file\\"]" and selectorType "css" to target it directly.',
            };
        }
        const toolParams = payload.toolCall
            .toolParams;
        const files = toolParams?.files;
        if (!files || files.length === 0) {
            logger.error('Upload file executor requires files in toolParams');
            return { success: false, errorMessage: 'No file data provided by server' };
        }
        const findEvent = payload.find;
        const setFilesEvent = {
            ...findEvent,
            listener: 'set-files',
            synthetic: false,
            files,
        };
        return (0, toolExecutor_types_1.recordStepAndTrackResult)(async () => {
            const result = await (0, stepCapture_1.recordTrainedStep)(setFilesEvent, payload.contextInfo, true, payload.toolCall.toolDescription, stateProvider, eventHandler);
            await Promise.all(files.map((file) => eventHandler.downloadUploadedFile(file)));
            return result;
        }, UPLOAD_STEP_WAIT_TIME_MS, stateProvider, eventHandler, logger);
    },
};
