/**
 * OutputFns — UI 出力ファサード
 *
 * TaskPrefixWriter が有効なとき（並列実行モード）はそちら経由で出力し、
 * 無効なとき（シングル実行モード）は shared/ui のモジュール関数に委譲する。
 */
import chalk from 'chalk';
import { header as rawHeader, info as rawInfo, warn as rawWarn, error as rawError, success as rawSuccess, status as rawStatus, blankLine as rawBlankLine, } from '../../../shared/ui/index.js';
export function createOutputFns(prefixWriter) {
    if (!prefixWriter) {
        return {
            header: rawHeader,
            info: rawInfo,
            warn: rawWarn,
            error: rawError,
            success: rawSuccess,
            status: rawStatus,
            blankLine: rawBlankLine,
            logLine: (text) => rawInfo(text),
        };
    }
    return {
        header: (title) => prefixWriter.writeLine(`=== ${title} ===`),
        info: (message) => prefixWriter.writeLine(`[INFO] ${message}`),
        warn: (message) => prefixWriter.writeLine(`[WARN] ${message}`),
        error: (message) => prefixWriter.writeLine(`[ERROR] ${message}`),
        success: (message) => prefixWriter.writeLine(message),
        status: (label, value) => prefixWriter.writeLine(`${label}: ${value}`),
        blankLine: () => prefixWriter.writeLine(''),
        logLine: (text) => prefixWriter.writeLine(text),
    };
}
/**
 * TaskPrefixWriter 経由でストリームイベントを行バッファリング出力するハンドラを作成する。
 */
export function createPrefixedStreamHandler(writer) {
    let lastType = 'other';
    return (event) => {
        switch (event.type) {
            case 'text':
                if (lastType === 'thinking')
                    writer.flush();
                writer.writeChunk(event.data.text);
                lastType = 'text';
                break;
            case 'tool_use':
                if (lastType === 'thinking' || lastType === 'text')
                    writer.flush();
                writer.writeLine(`[tool] ${event.data.tool}`);
                lastType = 'other';
                break;
            case 'tool_result': {
                const label = event.data.isError ? '✗' : '✓';
                writer.writeLine(`  ${label} ${event.data.content}`);
                lastType = 'other';
                break;
            }
            case 'tool_output':
                writer.writeChunk(event.data.output);
                break;
            case 'thinking':
                if (lastType !== 'thinking') {
                    if (lastType === 'text')
                        writer.flush();
                    writer.writeLine('💭 thinking:', (s) => chalk.magenta(s));
                }
                writer.writeChunk(event.data.thinking, (s) => chalk.dim(s));
                lastType = 'thinking';
                break;
            default: break;
        }
    };
}
//# sourceMappingURL=outputFns.js.map