"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ControlModeTmuxTransport = void 0;
exports.getControlModeTmuxTransport = getControlModeTmuxTransport;
const tmux_1 = require("./tmux");
const tmux_control_mode_metrics_1 = require("./tmux-control-mode-metrics");
const tmux_control_registry_1 = require("./tmux-control-registry");
const CONTROL_MODE_CAPABILITIES = {
    streamingOutput: true,
    explicitResize: true,
    snapshotFallback: true,
};
class ControlModeTmuxTransport {
    registry;
    constructor(options = {}) {
        this.registry = options.registry ?? (0, tmux_control_registry_1.getTmuxControlRegistry)();
    }
    async ensureSession(sessionName, cwd) {
        const exists = await (0, tmux_1.hasSession)(sessionName);
        if (!exists) {
            await (0, tmux_1.createSession)(sessionName, cwd);
        }
    }
    async sessionExists(sessionName) {
        return (0, tmux_1.hasSession)(sessionName);
    }
    async sendInput(sessionName, input) {
        if (this.registry.hasSession(sessionName)) {
            this.registry.sendInput(sessionName, input);
            return;
        }
        await (0, tmux_1.sendKeys)(sessionName, input);
    }
    async sendSpecialKey(sessionName, key) {
        if (key !== 'Escape' && key !== 'C-c' && key !== 'C-d' && key !== 'C-m' && key !== 'Enter') {
            throw new Error(`Unsupported special key for ControlModeTmuxTransport: ${key}`);
        }
        await (0, tmux_1.sendSpecialKey)(sessionName, key);
    }
    async resize(sessionName, cols, rows) {
        this.registry.resize(sessionName, cols, rows);
    }
    async captureSnapshot(sessionName, opts) {
        (0, tmux_control_mode_metrics_1.incrementTmuxControlCapturePaneCalls)();
        return (0, tmux_1.capturePane)(sessionName, opts);
    }
    async subscribe(sessionName, handlers) {
        const subscriberId = `sub-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
        const unsubscribe = this.registry.subscribe(sessionName, subscriberId, (event) => {
            if (event.type === 'output') {
                handlers.onOutput(event.data);
            }
            else if (event.type === 'exit') {
                handlers.onExit?.({ exitCode: event.exitCode });
            }
            else {
                handlers.onError(event.error);
            }
        });
        return {
            async unsubscribe() {
                unsubscribe();
            },
        };
    }
    getCapabilities() {
        return CONTROL_MODE_CAPABILITIES;
    }
    getSubscriberCount(sessionName) {
        return this.registry.getSubscriberCount(sessionName);
    }
    async killSession(sessionName) {
        return (0, tmux_1.killSession)(sessionName);
    }
}
exports.ControlModeTmuxTransport = ControlModeTmuxTransport;
let controlModeTransportSingleton = null;
function getControlModeTmuxTransport() {
    if (!controlModeTransportSingleton) {
        controlModeTransportSingleton = new ControlModeTmuxTransport();
    }
    return controlModeTransportSingleton;
}
