import type { PlanItem } from './types';

const MAIN_PENDING_WORKER_STATUSES = new Set(['main반영대기', 'main반영중', 'main반영실패']);
const AUTOMATION_FAILURE_WORKER_STATUSES = new Set([
  '브랜치실패',
  '자동작업실패',
  'release반영실패',
  'main반영실패',
]);

export type PlanQuickFilter = 'working' | 'release-pending-main' | 'automation-failed';

export function isWorkingPlanItem(item: Pick<PlanItem, 'status'>) {
  return item.status === '작업중';
}

export function normalizeWorkerStatus(workerStatus: string | null) {
  return workerStatus?.replace(/\s+/g, '') ?? '';
}

export function isReleasePendingMainItem(item: Pick<PlanItem, 'status' | 'workerStatus'>) {
  const normalizedWorkerStatus = normalizeWorkerStatus(item.workerStatus);

  if (MAIN_PENDING_WORKER_STATUSES.has(normalizedWorkerStatus)) {
    return true;
  }

  return item.status === '릴리즈완료';
}

export function isAutomationFailedItem(item: Pick<PlanItem, 'workerStatus'>) {
  return AUTOMATION_FAILURE_WORKER_STATUSES.has(normalizeWorkerStatus(item.workerStatus));
}

export function getPlanQuickFilterLabel(filter: PlanQuickFilter | null) {
  if (filter === 'working') {
    return '현재 작업중';
  }

  if (filter === 'release-pending-main') {
    return '현재 release 상태';
  }

  if (filter === 'automation-failed') {
    return '현재 자동화 실패';
  }

  return null;
}
