export { fmtDate } from "./dates";
export { fmtPhone } from "./phone";

const intFmt = new Intl.NumberFormat("en-US");
export const fmtInt = (v?: number) =>
  v === undefined || Number.isNaN(v) ? "—" : intFmt.format(Math.round(v));
export const fmtPct = (v?: number) =>
  v === undefined || Number.isNaN(v) ? "—" : `${v.toFixed(1)} %`;
export const fmtName = (first: string, father: string, grandfather: string) =>
  `${first} ${father} ${grandfather}`;

export function fmtDateTime(iso: string): string {
  return new Intl.DateTimeFormat("en-GB", {
    timeZone: "Africa/Addis_Ababa",
    day: "2-digit",
    month: "2-digit",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  }).format(new Date(iso));
}
export function fmtTime(iso: string): string {
  return new Intl.DateTimeFormat("en-GB", {
    timeZone: "Africa/Addis_Ababa",
    hour: "2-digit",
    minute: "2-digit",
  }).format(new Date(iso));
}
export function relativeTime(iso: string, now = Date.now()): string {
  const s = Math.round((now - Date.parse(iso)) / 1000);
  if (s < 60) return "just now";
  if (s < 3600) return `${Math.floor(s / 60)} min ago`;
  if (s < 86400) return `${Math.floor(s / 3600)} h ago`;
  return `${Math.floor(s / 86400)} d ago`;
}
/** i18n-ready string helper (English only for now). */
export const t = (s: string, vars?: Record<string, string | number>) =>
  vars ? s.replace(/\{(\w+)\}/g, (_, k: string) => String(vars[k] ?? `{${k}}`)) : s;
