/**
 * Rendering helpers shared by the board and the controls. Pure, no React.
 *
 * `money` mirrors `protocol.money()` in the Python so the board shows an
 * amount the way the model was shown it — the same digits, the same currency
 * code after them. The currency is fictional on held-out seeds (KRN), which is
 * exactly why it is a code and never a symbol.
 */

export function money(cents: number, currency: string): string {
  const sign = cents < 0 ? '-' : '';
  const abs = Math.abs(cents);
  const whole = Math.trunc(abs / 100).toLocaleString('en-US');
  const minor = String(abs % 100).padStart(2, '0');
  return `${sign}${whole}.${minor} ${currency}`;
}

/** Whole units, no minor part, for the summary table. */
export function wholeUnits(cents: number): string {
  return Math.trunc(cents / 100).toLocaleString('en-US');
}

export function hours(minutes: number): string {
  return (minutes / 60).toFixed(2);
}

const CHANNEL_LABEL: Record<string, string> = {
  cash_in: 'cash in',
  cash_out: 'cash out',
  wire_in: 'wire in',
  wire_out: 'wire out',
  ach_in: 'ACH in',
  ach_out: 'ACH out',
  p2p_in: 'P2P in',
  p2p_out: 'P2P out',
};

export function channelLabel(channel: string): string {
  return CHANNEL_LABEL[channel] ?? channel.replace('_', ' ');
}

/** `kyc.expected_monthly_cash` -> `expected monthly cash`. */
export function fieldLabel(key: string): string {
  return key.replace(/^kyc\./, '').replace(/_/g, ' ');
}

/** `CONSISTENT_WITH_PROFILE` -> `consistent with profile`. */
export function enumLabel(value: string): string {
  return value.toLowerCase().replace(/_/g, ' ');
}

/** The KYC keys the surface renders, in the order the Python renders them (sorted). */
export function kycEntries(customer: Record<string, unknown>): [string, unknown][] {
  return Object.keys(customer)
    .filter((key) => key !== 'id' && key !== 'name')
    .sort()
    .map((key) => [key, customer[key]]);
}
