import { TranslateService } from '@ngx-translate/core';

function polishPluralSuffix(count: number): string {
  const mod10 = count % 10;
  const mod100 = count % 100;
  if (count === 1) return 'one';
  if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 'few';
  return 'many';
}

export function translatePlural(
  translate: TranslateService,
  key: string,
  count: number,
): string {
  const lang = translate.getCurrentLang() ?? translate.getFallbackLang() ?? 'en';
  let suffix: string;
  if (lang.startsWith('pl')) {
    suffix = polishPluralSuffix(count);
  } else {
    suffix = count === 1 ? 'one' : 'other';
  }
  const fullKey = `${key}_${suffix}`;
  const translated = translate.instant(fullKey, { count });
  if (translated === fullKey) {
    return translate.instant(`${key}_other`, { count });
  }
  return translated;
}
