export function isNullOrUnknownLabel(label: string | null | undefined): boolean {
  const key = (label || '').trim().toLowerCase();
  return key === '' || key === 'null' || key === 'unknown' || key === 'none' || key === 'undefined';
}

export const REPAIR_COMPLETION_BAR_COLORS = [
  '#8B5CF6',
  '#6366F1',
  '#3B82F6',
  '#06B6D4',
  '#14B8A6',
  '#EC4899',
  '#A855F7',
  '#0EA5E9',
  '#10B981',
  '#F472B6',
  '#7C3AED',
  '#2DD4BF',
];

export type CompletionByTypeRate = {
  type: string;
  total: number;
  open?: number;
  inProgress?: number;
  pendingReview?: number;
  completed?: number;
  closed?: number;
  completedCombined?: number;
  rejected?: number;
  cancelled?: number;
};

export type CompletionByTypeData = {
  labels?: string[];
  rates?: CompletionByTypeRate[];
};

export function getCombinedCompletedCount(
  completed?: number,
  closed?: number
): number {
  return (completed || 0) + (closed || 0);
}

export function getRepairCompletionRate(rate: CompletionByTypeRate): number {
  const total = rate.total || 0;
  if (total <= 0) return 0;
  const combined = rate.completedCombined ?? ((rate.completed || 0) + (rate.closed || 0));
  return Math.round((combined / total) * 10000) / 100;
}
