import { dateFormat, t } from '../i18n' export function uid(): string { if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) { try { return crypto.randomUUID() } catch { /* contexte non sécurisé (http sur réseau local) */ } } return Date.now().toString(36) + Math.random().toString(36).slice(2, 10) } /** Jour local AAAA-MM-JJ (et pas le jour UTC : un épisode diffusé à 1 h du matin reste au bon jour). */ export function localDay(d: Date = new Date()): string { const pad = (n: number) => String(n).padStart(2, '0') return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` } export const todayISO = () => localDay() export function cx(...classes: (string | false | null | undefined)[]): string { return classes.filter(Boolean).join(' ') } /** Note interne (sur 10) → texte affiché en étoiles (sur 5). */ export function formatRating(value: number | undefined): string { if (value == null) return '—' const v = value / 2 return (Number.isInteger(v) ? String(v) : v.toFixed(1)).replace('.', t('common.decimalSep')) } export function formatDuration(minutes: number): string { const total = Math.round(minutes) if (total < 60) return t('common.minutes', { n: total }) const days = Math.floor(total / 1440) const hours = Math.floor((total % 1440) / 60) const mins = total % 60 if (days > 0) return t('common.daysHours', { d: days, h: hours }) return mins ? t('common.hoursMinutes', { h: hours, m: mins }) : t('common.hours', { h: hours }) } export function formatDate(iso?: string): string { if (!iso) return '' const d = new Date(iso.length === 10 ? iso + 'T12:00:00' : iso) if (Number.isNaN(d.getTime())) return iso return dateFormat({ day: 'numeric', month: 'short', year: 'numeric' }).format(d) } /** Recherche insensible à la casse et aux accents. */ export function normalizeText(s: string): string { return s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim() }