const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
const MONTH = 30 * DAY;
const YEAR = 365 * DAY;
const units = [
    { unit: 'year', ms: YEAR },
    { unit: 'month', ms: MONTH },
    { unit: 'week', ms: WEEK },
    { unit: 'day', ms: DAY },
    { unit: 'hour', ms: HOUR },
    { unit: 'minute', ms: MINUTE },
    { unit: 'second', ms: SECOND },
];
/**
 * Formats a date as a localized relative time expression,
 * e.g. "3 days ago" or "in 2 months".
 *
 * Uses the browser's native `Intl.RelativeTimeFormat` API.
 * @param date
 * @param language
 */
export function formatRelativeDate(date, language) {
    var _a;
    const diff = date.getTime() - Date.now();
    const { unit, ms } = (_a = units.find((u) => Math.abs(diff) >= u.ms)) !== null && _a !== void 0 ? _a : units.at(-1);
    return new Intl.RelativeTimeFormat(language, { style: 'long' }).format(Math.round(diff / ms), unit);
}
