"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.accessibilityCheckExecutor = void 0;
const toolExecutor_types_1 = require("../toolExecutor-types");
const stepCapture_1 = require("../../background/capture/stepCapture");
const THRESHOLD_BREACH_MESSAGE_PREFIX = 'Accessibility check failed due to violations with a minimum severity of';
exports.accessibilityCheckExecutor = {
    needsFind: () => false,
    getStepType: () => 'accessibility-check',
    execute: async (payload, stateProvider, eventHandler, logger) => {
        const toolParams = payload.toolCall
            .toolParams;
        const activeContext = payload.contextInfo ?? (await stateProvider.getActiveContextInfo());
        const contextInfo = { ...activeContext, frameId: 0 };
        const eventMessage = {
            action: 'record',
            listener: 'accessibility-check',
            runInMainFrame: true,
            ...(toolParams?.tags !== undefined && { tags: toolParams.tags }),
            ...(toolParams?.disabled_rules !== undefined && {
                disabledRules: toolParams.disabled_rules,
            }),
            ...(toolParams?.fail_on_severity !== undefined && {
                failOn: {
                    severity: toolParams.fail_on_severity,
                },
            }),
        };
        const result = await (0, toolExecutor_types_1.recordStepAndTrackResult)(() => (0, stepCapture_1.recordTrainedStep)(eventMessage, contextInfo, true, undefined, stateProvider, eventHandler), 0, stateProvider, eventHandler, logger);
        const replayMessage = stateProvider.getReplayResponse().message;
        const isThresholdBreach = (result.createdStepsInfo?.length ?? 0) > 0 &&
            replayMessage?.startsWith(THRESHOLD_BREACH_MESSAGE_PREFIX) === true;
        if (result.success || isThresholdBreach) {
            return {
                success: true,
                message: replayMessage || undefined,
                createdStepsInfo: result.createdStepsInfo,
            };
        }
        return result;
    },
};
