// Hungarian wording helpers shared by the demo pages: articles, month names, number words.

export const MONTHS = [
  'január',
  'február',
  'március',
  'április',
  'május',
  'június',
  'július',
  'augusztus',
  'szeptember',
  'október',
  'november',
  'december',
];
// Months with the -ban/-ben ending ("októberben", "augusztusban").
export const MONTHS_IN = [
  'januárban',
  'februárban',
  'márciusban',
  'áprilisban',
  'májusban',
  'júniusban',
  'júliusban',
  'augusztusban',
  'szeptemberben',
  'októberben',
  'novemberben',
  'decemberben',
];
// Number words for the title-number ranking ("a kettő és a három").

// The season named as an object ("megelőzi a telet").
export const SEASON_ACCUSATIVE: Record<string, string> = {
  tél: 'telet',
  tavasz: 'tavaszt',
  nyár: 'nyarat',
  ősz: 'őszt',
};
/** "a" or "az" before a word or a number, by its first sound ("az 1940-es", "a 2000-es"). */
export const article = (word: string | number) => {
  const text = String(word).trim();
  if (/^\d/.test(text)) return /^[15]/.test(text) ? 'az' : 'a';
  return /^[aáeéiíoóöőuúüű]/i.test(text) ? 'az' : 'a';
};
/** The word with its article: "az irodalom", "a képzőművészet". */
export const withArticle = (word: string) => `${article(word)} ${word}`;
// "3-án", "4-én", "1-jén": the suffix follows the day's last digits as read out loud.
const DAY_SUFFIX: Record<number, string> = {
  1: 'jén',
  2: 'án',
  3: 'án',
  4: 'én',
  5: 'én',
  6: 'án',
  7: 'én',
  8: 'án',
  9: 'én',
  0: 'án',
};
export const dayWithSuffix = (day: number) =>
  `${day}-${day === 10 || day === 20 || day === 30 ? (day === 10 ? 'én' : 'án') : DAY_SUFFIX[day % 10]}`;
// Numbers before a noun, in words: "a tíz leggyakoribb", "két lépcső".
const COUNT_WORDS = [
  'nulla',
  'egy',
  'két',
  'három',
  'négy',
  'öt',
  'hat',
  'hét',
  'nyolc',
  'kilenc',
  'tíz',
  'tizenegy',
  'tizenkét',
];
/** A count before a noun, in words up to twelve: "tíz", "tizenkét"; above that in digits. */
export const countWord = (count: number) => COUNT_WORDS[count] ?? String(count);
export const capitalize = (text: string) => `${text[0]?.toUpperCase() ?? ''}${text.slice(1)}`;
// "négy nemzedéken át"

// Evening hours as said out loud ("péntek este hét").
export const EVENING_HOURS: Record<number, string> = {
  17: 'öt',
  18: 'hat',
  19: 'hét',
  20: 'nyolc',
  21: 'kilenc',
};
// Months with the possessive ending ("2025 júniusa óta", "októbere").
export const MONTHS_OF = [
  'januárja',
  'februárja',
  'márciusa',
  'áprilisa',
  'májusa',
  'júniusa',
  'júliusa',
  'augusztusa',
  'szeptembere',
  'októbere',
  'novembere',
  'decembere',
];
export const monthIndex = (month: string) => Number(month.slice(5, 7)) - 1;

/** Use the calendar month written by the API, without a browser-timezone conversion. */
export function dataMonth(
  generatedAt: string | null | undefined,
  form: 'plain' | 'in' | 'until' = 'plain'
): string | null {
  if (typeof generatedAt !== 'string') return null;
  const match = /^(\d{4})-(\d{2})-(\d{2})(?:$|T)/.exec(generatedAt);
  if (!match || !Number.isFinite(Date.parse(generatedAt))) return null;
  const [, year, month, day] = match;
  const date = new Date(Date.UTC(Number(year), Number(month) - 1, Number(day)));
  if (date.toISOString().slice(0, 10) !== match[0].slice(0, 10)) return null;
  const index = Number(month) - 1;
  if (form === 'plain') return `${year}. ${MONTHS[index]}`;
  const stem = MONTHS_OF[index].replace(/a$/, 'á').replace(/e$/, 'é');
  const suffix = form === 'until' ? 'ig' : stem.endsWith('á') ? 'ban' : 'ben';
  return `${year}. ${stem}${suffix}`;
}

/** "2025-06" → "2025. június"; the tick under a column is the short month. */
export const monthLabel = (month: string) => `${month.slice(0, 4)}. ${MONTHS[monthIndex(month)]}`;

/** The short month under a column; January and the first month also name the year. */
export const shortMonthTick = (month: string, firstMonth: string) =>
  month.endsWith('-01') || month === firstMonth
    ? `${MONTHS[Number(month.slice(5)) - 1].slice(0, 3)} '${month.slice(2, 4)}`
    : MONTHS[Number(month.slice(5)) - 1].slice(0, 3);

// The -ban/-ben ending of a year follows its last word read out loud ("1975-ben", "1980-ban", "2000-ben").
const UNIT_BACK = new Set([3, 6, 8]);
const TEN_BACK = new Set([2, 3, 6, 8]);
/** "1975" → "1975-ben", "1980" → "1980-ban". */
export const yearIn = (year: number) => {
  const back =
    year % 10
      ? UNIT_BACK.has(year % 10)
      : year % 100
        ? TEN_BACK.has((year % 100) / 10)
        : year % 1000 !== 0;
  return `${year}-${back ? 'ban' : 'ben'}`;
};
