/** "3 month" / "1 year" → عدد + واحد ترجمه‌شده (بدون وابستگی به کش API) */
const UNIT_FA: Record<string, string> = {
  month: "ماه",
  months: "ماه",
  day: "روز",
  days: "روز",
  year: "سال",
  years: "سال",
  week: "هفته",
  weeks: "هفته",
  hour: "ساعت",
  hours: "ساعت",
  minute: "دقیقه",
  minutes: "دقیقه",
};

export const formatDuration = (time: string | undefined | null, t?: (key: string) => string) => {
  if (!time) return "";
  const trimmed = String(time).trim();
  const match = trimmed.match(/^(\d+)\s+([a-zA-Z]+)$/);
  if (!match) return trimmed;
  const [, count, unitRaw] = match;
  const unit = unitRaw.toLowerCase();
  const singular = unit.replace(/s$/, "");
  const fromT = t ? t(singular) : singular;
  const unitLabel =
    UNIT_FA[unit] ||
    UNIT_FA[singular] ||
    (fromT && fromT !== singular && fromT !== unit ? fromT : null) ||
    singular;
  return `${count} ${unitLabel}`;
};
