/**
 * Structured events for Vigthoria Workbench (VIGTHORIA_DECK_MODE=1).
 * Emitted as single lines: @vigthoria-deck:{json}
 * Workbench strips these from terminal display and updates UI panels.
 */
const DECK_PREFIX = '@vigthoria-deck:';
export function isDeckModeEnabled() {
    return /^(1|true|yes)$/i.test(String(process.env.VIGTHORIA_DECK_MODE || ''));
}
export function emitDeckEvent(event) {
    if (!isDeckModeEnabled())
        return;
    const payload = {
        ...event,
        ts: event.ts ?? Date.now(),
    };
    // Force the marker onto its own fresh line. Callers may have just updated an
    // in-place spinner (e.g. ora) which redraws via "\r" and never emits a
    // trailing "\n", so without this leading newline the marker can land stuck
    // mid-line after visible text (e.g. "Executing plan...@vigthoria-deck:{...}")
    // and the Workbench's line-start prefix check won't strip it, leaking raw
    // JSON into the terminal.
    process.stdout.write(`\n${DECK_PREFIX}${JSON.stringify(payload)}\n`);
}
export function parseDeckLine(line) {
    const trimmed = line.trim();
    if (!trimmed.startsWith(DECK_PREFIX))
        return null;
    try {
        return JSON.parse(trimmed.slice(DECK_PREFIX.length));
    }
    catch {
        return null;
    }
}
export function stripDeckLines(text) {
    return text
        .split('\n')
        .filter((line) => !line.trim().startsWith(DECK_PREFIX))
        .join('\n');
}
