"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliRecordingHandler = void 0;
const stepCapture_1 = require("../background/capture/stepCapture");
const messages_1 = require("../sharedUtils/messages");
const humanize_1 = require("../sharedUtils/mablscript/humanize");
class CliRecordingHandler {
    constructor(options) {
        this.isRecording = true;
        this.stateProvider = options.stateProvider;
        this.eventHandler = options.eventHandler;
        this.state = options.state;
        this.onStepRecorded = options.onStepRecorded;
    }
    canHandle(message) {
        return message?.action === messages_1.RECORD;
    }
    async handleMessage(message) {
        if (!this.canHandle(message)) {
            return { acknowledged: false, error: 'Not a RECORD message' };
        }
        if (!this.isRecording) {
            return { acknowledged: true };
        }
        try {
            await this.recordStep(message);
            return { acknowledged: true };
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            console.error('Error recording step:', errorMessage);
            return { acknowledged: false, error: errorMessage };
        }
    }
    async recordStep(eventMessage) {
        const contextInfo = eventMessage.frameInfo
            ? {
                tabId: eventMessage.frameInfo.tabId,
                frameId: eventMessage.frameInfo.frameId,
            }
            : {
                tabId: this.stateProvider.getTrainingContext().getCurrentTab(),
                frameId: this.stateProvider.getTrainingContext().getCurrentFrame(),
            };
        const stepCaptureMessage = {
            ...eventMessage,
            listener: eventMessage.listener ?? 'unknown',
        };
        const result = await (0, stepCapture_1.recordTrainedStep)(stepCaptureMessage, contextInfo, false, undefined, this.stateProvider, this.eventHandler, false);
        if (result.steps.length > 0) {
            const variables = this.stateProvider.getVariables();
            for (const stepGenerated of result.steps) {
                const step = stepGenerated;
                this.state.addStep(step);
                const stepIndex = this.state.getSteps().length;
                const humanized = (0, humanize_1.humanizeStepDescription)(step, variables);
                if (this.onStepRecorded) {
                    this.onStepRecorded(step, humanized, stepIndex);
                }
            }
        }
        if (contextInfo.tabId !==
            this.stateProvider.getTrainingContext().getCurrentTab()) {
            this.stateProvider
                .getTrainingContext()
                .setCurrentTab(`${contextInfo.tabId}`);
        }
        if (contextInfo.frameId !==
            this.stateProvider.getTrainingContext().getCurrentFrame()) {
            this.stateProvider
                .getTrainingContext()
                .setCurrentFrame(contextInfo.frameId);
        }
    }
    startRecording() {
        this.isRecording = true;
    }
    stopRecording() {
        this.isRecording = false;
    }
    isCurrentlyRecording() {
        return this.isRecording;
    }
    getSteps() {
        return this.state.getSteps();
    }
    getHumanizedSteps() {
        const steps = this.state.getSteps();
        const variables = this.stateProvider.getVariables();
        return steps.map((step) => (0, humanize_1.humanizeStepDescription)(step, variables));
    }
}
exports.CliRecordingHandler = CliRecordingHandler;
