export const MONTH_NAMES = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const CATEGORY_ORDER = ['missing_file', 'file_issue'];
const CATEGORY_LABELS = {
    missing_file: 'Missing entries',
    file_issue: 'Issues found',
};
function quarter_ref(iso_date) {
    const m = iso_date.match(/^(\d{4})-(\d{2})/);
    if (!m)
        return null;
    const year = parseInt(m[1], 10);
    const month_idx = parseInt(m[2], 10) - 1;
    if (month_idx < 0 || month_idx > 11)
        return null;
    const quarter = Math.floor(month_idx / 3);
    const start_m = quarter * 3;
    const end_m = start_m + 2;
    return {
        key: `${year}-${quarter}`,
        label: `${MONTH_NAMES[start_m]}–${MONTH_NAMES[end_m]} ${year}`,
    };
}
export function bucket_dates_by_quarter(items) {
    // Sort first so insertion order into the Map is itself chronological.
    const dated = items.filter(i => typeof i.date === 'string' && i.date.length > 0);
    const sorted = [...dated].sort((a, b) => a.date.localeCompare(b.date));
    const buckets = new Map();
    for (const item of sorted) {
        const ref = quarter_ref(item.date);
        if (!ref)
            continue;
        const existing = buckets.get(ref.key);
        if (existing) {
            existing.count += 1;
            existing.issues.push(item);
        }
        else {
            buckets.set(ref.key, { key: ref.key, label: ref.label, count: 1, issues: [item] });
        }
    }
    return Array.from(buckets.values());
}
function effective_kind(issue) {
    return issue.kind === 'missing_file' ? 'missing_file' : 'file_issue';
}
export function bucket_issues(issues) {
    if (issues.length === 0)
        return { categories: [], date_range: null };
    // Group by kind preserving original order
    const by_kind = new Map();
    for (const issue of issues) {
        const k = effective_kind(issue);
        const list = by_kind.get(k);
        if (list)
            list.push(issue);
        else
            by_kind.set(k, [issue]);
    }
    const categories = [];
    for (const kind of CATEGORY_ORDER) {
        const list = by_kind.get(kind);
        if (!list || list.length === 0)
            continue;
        const dated = list.filter(i => typeof i.date === 'string' && i.date.length > 0);
        const undated = list.filter(i => !(typeof i.date === 'string' && i.date.length > 0));
        categories.push({
            kind,
            label: CATEGORY_LABELS[kind],
            count: list.length,
            time_buckets: bucket_dates_by_quarter(dated),
            undated_issues: undated,
        });
    }
    // Date range across all dated issues
    const all_dates = [];
    for (const issue of issues) {
        if (typeof issue.date === 'string' && issue.date.length > 0)
            all_dates.push(issue.date);
    }
    let date_range = null;
    if (all_dates.length > 0) {
        all_dates.sort();
        date_range = { earliest: all_dates[0], latest: all_dates[all_dates.length - 1] };
    }
    return { categories, date_range };
}
export function build_issue_tree(result) {
    const { categories, date_range } = bucket_issues(result.issues);
    return {
        rule_id: result.rule_id,
        rule_name: result.rule_name ?? result.rule_id,
        total_count: result.issues.length,
        date_range,
        categories,
    };
}
/** Format an ISO YYYY-MM-DD date as "Mon YYYY" (e.g. "Jul 2024"). Returns the input unchanged when malformed. */
export function format_month(iso) {
    const m = iso.match(/^(\d{4})-(\d{2})/);
    if (!m)
        return iso;
    const month_idx = parseInt(m[2], 10) - 1;
    if (month_idx < 0 || month_idx > 11)
        return iso;
    return `${MONTH_NAMES[month_idx]} ${m[1]}`;
}
//# sourceMappingURL=issue_bucketing.js.map