/**
 * NLG Renderer — consent-aware Natural Language Generation.
 *
 * Produces human-readable reasoning summaries and consent eligibility reports.
 * Uses i18n for bilingual (KO/EN) support.
 * Integrates with ThemePalette for styled output.
 *
 * Key principle: "사용자에게는 theme/NLG만"
 */
import { summarizeTrace, generateConsentReport } from "./reasoning-trace.js";
import { t } from "../util/i18n.js";
// ─── Helpers ─────────────────────────────────────────────────────────────────
function themed(p, token, text) {
    if (!p || !p.supportsColor)
        return text;
    return p.render(token, text);
}
function outcomeIcon(outcome) {
    switch (outcome) {
        case "success": return "✓";
        case "partial": return "◐";
        case "failed": return "✗";
        case "blocked": return "⊘";
        default: return "·";
    }
}
function formatDuration(ms) {
    if (ms >= 60_000)
        return `${(ms / 60_000).toFixed(1)}m`;
    if (ms >= 1000)
        return `${(ms / 1000).toFixed(1)}s`;
    return `${ms}ms`;
}
// ─── NLG Renderer Factory ────────────────────────────────────────────────────
export function createNlgRenderer(options) {
    const palette = options?.palette;
    const lines = [];
    function write(text) {
        if (options?.write) {
            options.write(text + "\n");
        }
        else {
            lines.push(text);
        }
    }
    return {
        renderTrace(trace) {
            const summary = summarizeTrace(trace);
            const out = [];
            // Header
            out.push(themed(palette, "header", t("nlg.traceHeader")));
            out.push("");
            // Intent + confidence
            out.push(`${themed(palette, "labelKey", t("nlg.intent"))} ${themed(palette, "info", summary.intent)} ${themed(palette, "dim", `(${Math.round(summary.confidence * 100)}%)`)}`);
            // Plan
            out.push(`${themed(palette, "labelKey", t("nlg.plan"))} ${summary.planSummary}`);
            // Tools used
            if (summary.toolsUsed.length > 0) {
                out.push(`${themed(palette, "labelKey", t("nlg.tools"))} ${summary.toolsUsed.map(tool => themed(palette, "tool", tool)).join(", ")}`);
            }
            // Test result
            if (summary.testResult) {
                out.push(`${themed(palette, "labelKey", t("nlg.tests"))} ${summary.testResult}`);
            }
            // Outcome
            const icon = outcomeIcon(summary.outcome);
            const outcomeToken = summary.outcome === "success" ? "success" : summary.outcome === "failed" ? "error" : "warning";
            out.push(`${themed(palette, "labelKey", t("nlg.outcome"))} ${themed(palette, outcomeToken, `${icon} ${summary.outcome}`)}`);
            // Duration
            out.push(`${themed(palette, "labelKey", t("nlg.duration"))} ${formatDuration(trace.execution.durationMs)}`);
            // Failure reason
            if (trace.result.failureReason) {
                out.push(`${themed(palette, "labelKey", t("nlg.failure"))} ${themed(palette, "error", trace.result.failureReason)}`);
            }
            // Decision records
            if (trace.execution.decisionRecords.length > 0) {
                out.push("");
                out.push(themed(palette, "subheader", t("nlg.decisions")));
                for (const d of trace.execution.decisionRecords) {
                    out.push(`  ${themed(palette, "bullet", "•")} ${themed(palette, "labelKey", d.point)} → ${themed(palette, "info", d.chosen)} ${themed(palette, "dim", `(${d.reason})`)}`);
                }
            }
            const result = out.join("\n");
            write(result);
            return result;
        },
        renderSummary(summary) {
            const icon = outcomeIcon(summary.outcome);
            const outcomeToken = summary.outcome === "success" ? "success" : summary.outcome === "failed" ? "error" : "warning";
            const line = `${themed(palette, outcomeToken, icon)} ${themed(palette, "info", summary.intent)} → ${themed(palette, outcomeToken, summary.outcome)} ${themed(palette, "dim", `(${summary.duration})`)}`;
            write(line);
            return line;
        },
        renderConsentReport(trace, consentLevel) {
            const language = options?.language ?? "en";
            const report = generateConsentReport({
                trace,
                consentLevel,
                includeFiles: options?.includeFiles ?? false,
                includeCommands: options?.includeCommands ?? false,
                language,
            });
            write(report.report);
            return report;
        },
        renderTurnResult(trace) {
            const summary = summarizeTrace(trace);
            const out = [];
            // Compact result line
            const icon = outcomeIcon(summary.outcome);
            const outcomeToken = summary.outcome === "success" ? "success" : summary.outcome === "failed" ? "error" : "warning";
            out.push(`${themed(palette, outcomeToken, icon)} ${themed(palette, "info", summary.intent)} → ${themed(palette, outcomeToken, summary.outcome)} ${themed(palette, "dim", `(${summary.duration})`)}`);
            out.push(`  ${themed(palette, "dim", summary.planSummary)}`);
            // Tools used (compact)
            if (summary.toolsUsed.length > 0) {
                out.push(`  ${themed(palette, "dim", summary.toolsUsed.join(", "))}`);
            }
            // Failure reason
            if (trace.result.failureReason) {
                out.push(`  ${themed(palette, "error", trace.result.failureReason)}`);
            }
            const result = out.join("\n");
            write(result);
            return result;
        },
        flush() {
            const output = lines.join("\n");
            lines.length = 0;
            return output;
        },
    };
}
