"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TmuxControlParser = void 0;
/**
 * Minimal tmux control mode parser for Phase 2.
 *
 * Current behavior:
 * - Treats `%output ` lines as output events
 * - Treats `%exit` lines as exit events
 * - Emits raw non-control lines as output to avoid dropping data
 *
 * The parser is intentionally conservative: unknown control messages are ignored
 * instead of being misinterpreted as terminal content.
 */
class TmuxControlParser {
    remainder = '';
    push(chunk) {
        if (chunk.length === 0) {
            return [];
        }
        const combined = this.remainder + chunk;
        const lines = combined.split('\n');
        this.remainder = lines.pop() ?? '';
        const events = [];
        for (const line of lines) {
            const normalizedLine = line.endsWith('\r') ? line.slice(0, -1) : line;
            const parsed = this.parseLine(normalizedLine);
            if (parsed) {
                events.push(parsed);
            }
        }
        return events;
    }
    flush() {
        if (this.remainder.length === 0) {
            return [];
        }
        const parsed = this.parseLine(this.remainder);
        this.remainder = '';
        return parsed ? [parsed] : [];
    }
    parseLine(line) {
        if (line.length === 0) {
            return { type: 'output', data: '\n' };
        }
        if (line.startsWith('%output ')) {
            return { type: 'output', data: `${line.slice('%output '.length)}\n` };
        }
        if (line === '%exit' || line.startsWith('%exit ')) {
            return { type: 'exit', exitCode: null };
        }
        if (line.startsWith('%error ')) {
            return { type: 'error', error: new Error(line.slice('%error '.length)) };
        }
        if (line.startsWith('%')) {
            return null;
        }
        return { type: 'output', data: `${line}\n` };
    }
}
exports.TmuxControlParser = TmuxControlParser;
