export type ReportStatusSummary = { status: string; approved: number; total: number; }; const FAILED_REPORT_STATUSES = new Set(['failed', 'rejected']); export function summarizeReportStatuses(statuses: string[]): ReportStatusSummary { if (!statuses.length) return { status: 'not_applicable', approved: 0, total: 0 }; const approved = statuses.filter((status) => status === 'approved').length; const failed = statuses.filter((status) => FAILED_REPORT_STATUSES.has(status)).length; const abandoned = statuses.filter((status) => status === 'abandoned').length; if (approved === statuses.length) return { status: 'approved', approved, total: statuses.length }; if (abandoned === statuses.length) return { status: 'abandoned', approved, total: statuses.length }; // Overall failure means every current target failed. A single failed channel must not // erase successful channels or targets that can still finish reporting. if (failed === statuses.length) return { status: 'failed', approved, total: statuses.length }; if (approved > 0) return { status: 'partial_success', approved, total: statuses.length }; if (failed > 0) return { status: 'reporting', approved, total: statuses.length }; if (statuses.some((status) => ['reporting', 'exporting', 'partial', 'partial_success'].includes(status))) { return { status: 'reporting', approved, total: statuses.length }; } if (statuses.some((status) => status === 'waiting_material')) { return { status: 'waiting_material', approved, total: statuses.length }; } return { status: 'pending', approved, total: statuses.length }; }