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

const ORDER: SettlementPhase[] = [
  'RECEIVED',
  'VALIDATED',
  'VERBIAGE_ROLLED',
  'FINERACT_POSTED',
  'HYBX_RAIL_DISPATCHED',
  'ISO20022_ARCHIVED',
  'CHAIN_MINT_REQUESTED',
  'SETTLED',
];

export function nextPhase(current: SettlementPhase): SettlementPhase | null {
  if (current === 'FAILED' || current === 'SETTLED') return null;
  const idx = ORDER.indexOf(current);
  if (idx < 0 || idx >= ORDER.length - 1) return null;
  return ORDER[idx + 1];
}

export function canAdvance(from: SettlementPhase, to: SettlementPhase): boolean {
  const fi = ORDER.indexOf(from);
  const ti = ORDER.indexOf(to);
  if (fi < 0 || ti < 0) return to === 'FAILED';
  return ti === fi + 1 || to === 'FAILED';
}
