"use strict";
/**
 * Runtime Event Renderer (W3.1-support of F-NEW-PRODUCT-AGENT-VISIBILITY-CLUSTER-2026-05-12).
 *
 * Pure helpers that normalise heterogeneous governance events (live
 * PackEvaluator verdicts, OverrideEvents from W1.9, info/action-separation
 * verdicts from W1.6, pack-version transitions from W1.4, ReplayEvents
 * from W4.2 sandbox replay) into a single TimelineRow shape suitable for
 * rendering in the runtime live-view single-vertical timeline.
 *
 * Browser-side safe — no node-builtin imports, no framework dependencies.
 * The React UI layer (W3.1) consumes the TimelineRow shape directly.
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-RUNTIME-LIVE-VIEW (renderer support)
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromReplayEvent = fromReplayEvent;
exports.fromOverrideEvent = fromOverrideEvent;
exports.fromPackVersionEvent = fromPackVersionEvent;
exports.fromInfoActionVerdict = fromInfoActionVerdict;
exports.sortTimelineRows = sortTimelineRows;
exports.filterByMinTone = filterByMinTone;
const SEVERITY_TO_TONE = {
    INFO: 'info',
    NOTICE: 'notice',
    WARN: 'warn',
    ERROR: 'error',
    CRITICAL: 'critical',
};
const REPLAY_KIND_MAP = {
    TOOL_CALL_REQUESTED: { kind: 'tool-call', tone: 'info' },
    AUTHORITY_OBSERVED: { kind: 'authority', tone: 'notice' },
    VERDICT_ALLOW: { kind: 'verdict-allow', tone: 'info' },
    VERDICT_DENY: { kind: 'verdict-deny', tone: 'critical' },
    VERDICT_PENDING_APPROVAL: { kind: 'verdict-pending', tone: 'warn' },
    APPROVAL_GRANTED: { kind: 'approval-granted', tone: 'notice' },
    APPROVAL_DENIED: { kind: 'approval-denied', tone: 'critical' },
    STATE_CHANGED: { kind: 'state-change', tone: 'notice' },
    AUDIT_RECORDED: { kind: 'audit', tone: 'info' },
    TRUST_SCORE_DELTA: { kind: 'trust-delta', tone: 'notice' },
};
function fromReplayEvent(ev, runStartIso) {
    const mapping = REPLAY_KIND_MAP[ev.kind];
    const severityLabel = (ev.severity ?? 'INFO');
    const computedTone = SEVERITY_TO_TONE[severityLabel] ?? mapping.tone;
    const at = new Date(new Date(runStartIso).getTime() + ev.tMs).toISOString();
    return {
        at,
        tMs: ev.tMs,
        kind: mapping.kind,
        tone: computedTone,
        headline: shortHeadline(ev.detail),
        detail: ev.detail,
        packId: ev.packId,
        checkId: ev.checkId,
        toolName: ev.toolName,
        severityLabel,
    };
}
function fromOverrideEvent(ev) {
    return {
        at: ev.composedAt,
        kind: 'override',
        tone: 'warn',
        headline: `Override applied: ${ev.capability} ` +
            `(${ev.baselineAction ?? 'unbound'} → ${ev.relaxedAction}) by ${ev.approver}`,
        detail: ev.justification,
        actor: ev.approver,
        severityLabel: 'WARN',
    };
}
function fromPackVersionEvent(ev) {
    const map = {
        VERSION_PUBLISHED: { headline: `Pack version ${ev.version} published`, tone: 'info', severity: 'INFO' },
        VERSION_ACTIVATED: { headline: `Pack version ${ev.version} activated`, tone: 'notice', severity: 'NOTICE' },
        VERSION_SHADOW_ENABLED: { headline: `Pack version ${ev.version} entered shadow mode`, tone: 'notice', severity: 'NOTICE' },
        VERSION_ARCHIVED: { headline: `Pack version ${ev.version} archived`, tone: 'info', severity: 'INFO' },
        PACK_ROLLED_BACK: { headline: `Pack rolled back to ${ev.version}`, tone: 'warn', severity: 'WARN' },
    };
    const m = map[ev.kind];
    return {
        at: ev.timestamp,
        kind: 'pack-version-change',
        tone: m.tone,
        headline: m.headline,
        detail: ev.detail,
        packId: ev.packId,
        actor: ev.actor,
        severityLabel: m.severity,
    };
}
function fromInfoActionVerdict(v, ctx) {
    const map = {
        ALLOW: { kind: 'verdict-allow', tone: 'info', severity: 'INFO' },
        DENY: { kind: 'verdict-deny', tone: 'critical', severity: 'CRITICAL' },
        PENDING: { kind: 'verdict-pending', tone: 'warn', severity: 'WARN' },
    };
    const m = map[v.verdict];
    return {
        at: ctx.at,
        tMs: ctx.tMs,
        kind: m.kind,
        tone: m.tone,
        headline: `Info/Action separation (${v.mode}): ${v.verdict}`,
        detail: v.reason,
        toolName: ctx.toolName,
        severityLabel: m.severity,
    };
}
function shortHeadline(detail) {
    if (detail.length <= 80)
        return detail;
    return detail.slice(0, 77) + '...';
}
/** Sort rows in ascending time order (stable). */
function sortTimelineRows(rows) {
    return [...rows].sort((a, b) => {
        const ta = new Date(a.at).getTime();
        const tb = new Date(b.at).getTime();
        if (ta !== tb)
            return ta - tb;
        return 0;
    });
}
/** Filter rows by tone (e.g. show only WARN+CRITICAL on a triage view). */
function filterByMinTone(rows, minTone) {
    const TONE_RANK = {
        info: 0,
        notice: 1,
        warn: 2,
        error: 3,
        critical: 4,
    };
    const min = TONE_RANK[minTone];
    return rows.filter((r) => TONE_RANK[r.tone] >= min);
}
//# sourceMappingURL=runtime-event-renderer.js.map