"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordClickExecutor = void 0;
exports.buildNativeSelectErrorMessage = buildNativeSelectErrorMessage;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
const CLICK_STEP_WAIT_TIME_MS = 3_000;
function buildNativeSelectErrorMessage(elementDescription, elementBoundingBox, selectOptions) {
    const boundingBoxStr = elementBoundingBox
        ? ` (normalized bounding box: top=${elementBoundingBox.top?.toFixed(3)}, bottom=${elementBoundingBox.bottom?.toFixed(3)}, left=${elementBoundingBox.left?.toFixed(3)}, right=${elementBoundingBox.right?.toFixed(3)})`
        : '';
    const maxOptions = 50;
    const displayOptions = selectOptions.slice(0, maxOptions);
    const optionsText = displayOptions.length > 0
        ? displayOptions.map((option) => `"${option.text}"`).join(', ')
        : 'no options available';
    const optionsSuffix = selectOptions.length > maxOptions
        ? `, and ${selectOptions.length - maxOptions} more`
        : '';
    const errorMessage = `Click on native HTML Selects are not supported. ` +
        `The element "${elementDescription}"${boundingBoxStr} is a native <select>. ` +
        `Use enter_text on THIS SAME ELEMENT to set the value. ` +
        `Available options: ${optionsText}${optionsSuffix}`;
    const fullOptionsText = selectOptions
        .map((option) => `"${option.text}"`)
        .join(', ');
    const applicationStatement = `${elementDescription} is a select element with options: ${fullOptionsText}`;
    return { errorMessage, applicationStatement };
}
exports.recordClickExecutor = {
    needsFind: () => true,
    getStepType: () => 'click',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        if (!payload.find || !payload.contextInfo) {
            logger.error('Record click executor requires find and contextInfo');
            return {
                success: false,
            };
        }
        const toolParams = payload.toolCall.toolParams;
        if ((0, toolExecutor_types_1.isFindOneEventMessage)(payload.find) && payload.find.selectOptions) {
            const elementDescription = toolParams.elementDescription || 'unknown element';
            const elementBoundingBox = toolParams.elementBoundingBox;
            const { errorMessage, applicationStatement } = buildNativeSelectErrorMessage(elementDescription, elementBoundingBox, payload.find.selectOptions);
            return {
                success: false,
                errorCause: 'WrongToolCall',
                errorMessage,
                applicationInfo: [applicationStatement],
            };
        }
        return (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(payload.find, payload.contextInfo, true, undefined, stateProvider, eventHandler), CLICK_STEP_WAIT_TIME_MS, stateProvider, eventHandler, logger, payload.visualFindContext);
    },
};
