/**
 * Group-level coverage findings card.
 * Renders above a task's file list when periodic_coverage rules detect gaps
 * or are skipped due to unresolved period bounds.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { LuCircleAlert, LuCircleHelp, LuChevronDown, LuChevronRight } from 'react-icons/lu';
import { bucket_dates_by_quarter, format_month } from '../../../utils/issue_bucketing.js';
export function CoverageCard({ results }) {
    // Filter to items that need surfacing, then de-dupe across groups: when
    // multiple groups produced the same rule + same gap set (typical when a
    // task has only one logical group but classification produced duplicate
    // tag groupings), collapse to a single card.
    const surfaced = results.filter(r => r.skipped || r.gaps.length > 0);
    const deduped = dedupe_results(surfaced);
    if (deduped.length === 0)
        return null;
    return (_jsx("div", { className: "space-y-2 mb-3", children: deduped.map((cov, i) => (_jsx(CoverageCardRow, { cov: cov }, `${cov.rule_id}_${i}`))) }));
}
function CoverageCardRow({ cov }) {
    const [expanded, set_expanded] = useState(false);
    if (cov.skipped) {
        return (_jsxs("div", { className: "flex items-start gap-2 border-l-4 border-amber-400 bg-amber-50/50 dark:bg-amber-950/20 px-3 py-2 rounded-md", children: [_jsx(LuCircleHelp, { className: "h-4 w-4 text-amber-600 mt-0.5 shrink-0" }), _jsxs("p", { className: "text-sm flex-1 min-w-0", children: [_jsx("span", { className: "font-medium text-foreground", children: cov.rule_name }), _jsx("span", { className: "text-muted-foreground", children: " \u2014 skipped (period bounds did not resolve)" })] })] }));
    }
    const range = format_overall_range(cov.gaps);
    const gap_count = cov.gaps.length;
    return (_jsxs("div", { className: "border-l-4 border-destructive bg-destructive/5 rounded-md", children: [_jsxs("button", { type: "button", onClick: () => set_expanded(e => !e), className: "w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-destructive/10 transition-colors rounded-md", children: [_jsx(LuCircleAlert, { className: "h-4 w-4 text-destructive shrink-0" }), _jsxs("span", { className: "text-sm flex-1 min-w-0 truncate", children: [_jsx("span", { className: "font-medium text-foreground", children: cov.rule_name }), _jsx("span", { className: "text-muted-foreground", children: " \u2014 " }), _jsxs("span", { className: "text-destructive font-medium", children: [gap_count, " missing period", gap_count === 1 ? '' : 's'] }), range && _jsxs("span", { className: "text-muted-foreground", children: [" (", range, ")"] })] }), expanded
                        ? _jsx(LuChevronDown, { className: "h-3.5 w-3.5 text-muted-foreground shrink-0" })
                        : _jsx(LuChevronRight, { className: "h-3.5 w-3.5 text-muted-foreground shrink-0" })] }), expanded && (_jsx("ul", { className: "px-3 pb-2 pl-9 space-y-0.5 text-xs text-muted-foreground", children: bucket_dates_by_quarter(cov.gaps.map(g => ({ date: g.start, gap: g }))).map((bucket, bi) => (_jsx(QuarterRow, { bucket: bucket }, bi))) }))] }));
}
function QuarterRow({ bucket }) {
    const [open, set_open] = useState(false);
    return (_jsxs("li", { children: [_jsxs("button", { type: "button", onClick: () => set_open(o => !o), className: "w-full flex items-center gap-1 text-left hover:text-foreground transition-colors", children: [open
                        ? _jsx(LuChevronDown, { className: "h-3 w-3 text-muted-foreground/70 shrink-0" })
                        : _jsx(LuChevronRight, { className: "h-3 w-3 text-muted-foreground/70 shrink-0" }), _jsx("span", { className: "font-medium text-foreground/80", children: bucket.label }), _jsx("span", { className: "text-muted-foreground", children: " \u2014 " }), _jsx("span", { className: "font-mono", children: bucket.count }), _jsx("span", { children: " missing" })] }), open && (_jsx("ul", { className: "pl-4 mt-0.5 space-y-0.5 font-mono", children: bucket.issues.map(({ gap }, gi) => (_jsxs("li", { className: "text-muted-foreground", children: [gap.start, " \u2026 ", gap.end] }, gi))) }))] }));
}
/** Collapse the gap list to a "Mon YYYY – Mon YYYY" overall range. */
function format_overall_range(gaps) {
    if (gaps.length === 0)
        return null;
    const first = gaps[0].start;
    const last = gaps[gaps.length - 1].end;
    return `${format_month(first)} – ${format_month(last)}`;
}
/** De-dupe results that share the same rule_id and identical gap sets. */
function dedupe_results(results) {
    const seen = new Set();
    const out = [];
    for (const r of results) {
        const sig = `${r.rule_id}|${r.skipped ? 'skip' : r.gaps.map(g => `${g.start}_${g.end}`).join(',')}`;
        if (seen.has(sig))
            continue;
        seen.add(sig);
        out.push(r);
    }
    return out;
}
//# sourceMappingURL=coverage_card.js.map