// Formats fr-CA : prix CAD, pi²/m², dates, pourcentages. const cad0 = new Intl.NumberFormat("fr-CA", { style: "currency", currency: "CAD", maximumFractionDigits: 0 }); const int = new Intl.NumberFormat("fr-CA", { maximumFractionDigits: 0 }); const dec1 = new Intl.NumberFormat("fr-CA", { maximumFractionDigits: 1, minimumFractionDigits: 0 }); export function fmtPrice(v: number | null | undefined, opts?: { rent?: boolean; compact?: boolean }): string { if (v == null || !Number.isFinite(v)) return "—"; if (opts?.compact) { if (v >= 1_000_000) return `${dec1.format(v / 1_000_000).replace(",0", "")} M$`; if (v >= 1_000) return `${int.format(Math.round(v / 1000))} k$`; } const s = cad0.format(v); return opts?.rent ? `${s}/mois` : s; } export function fmtInt(v: number | null | undefined): string { return v == null || !Number.isFinite(v) ? "—" : int.format(v); } export function fmtPct(v: number | null | undefined, digits = 1, signed = true): string { if (v == null || !Number.isFinite(v)) return "—"; const s = new Intl.NumberFormat("fr-CA", { maximumFractionDigits: digits, minimumFractionDigits: 0 }).format(Math.abs(v) * 100); return `${signed ? (v > 0 ? "+" : v < 0 ? "−" : "") : ""}${s} %`; } export function fmtArea(sqft: number | null | undefined, unit: "sqft" | "m2" = "sqft"): string { if (sqft == null || !Number.isFinite(sqft) || sqft <= 0) return "—"; return unit === "m2" ? `${int.format(Math.round(sqft * 0.09290304))} m²` : `${int.format(Math.round(sqft))} pi²`; } export function fmtDate(iso: string | null | undefined, style: "short" | "long" = "short"): string { if (!iso) return "—"; const d = new Date(iso); if (Number.isNaN(d.getTime())) return "—"; return d.toLocaleDateString("fr-CA", style === "long" ? { day: "numeric", month: "long", year: "numeric" } : { day: "numeric", month: "short", year: "numeric" }); } export function daysSince(iso: string | null | undefined): number | null { if (!iso) return null; const t = Date.parse(iso); return Number.isNaN(t) ? null : Math.max(0, Math.floor((Date.now() - t) / 86400000)); } export function fmtRooms(bed: number | null | undefined, bath: number | null | undefined): string { const p: string[] = []; if (bed != null) p.push(`${dec1.format(bed)} ch.`); if (bath != null) p.push(`${dec1.format(bath)} sdb`); return p.join(" · "); } export function slugify(s: string): string { return s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""); } export function pluralize(n: number, one: string, many: string): string { return `${fmtInt(n)} ${n > 1 ? many : one}`; }