export const fmtDateTime = (d) => {
  if (!d) return "";
  try {
    const dt = new Date(d);
    const datePart = dt.toLocaleDateString("sl-SI", {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
    });
    const timePart = dt.toLocaleTimeString("sl-SI", {
      hour: "2-digit",
      minute: "2-digit",
      hour12: false,
    });
    return `${datePart} ${timePart}`;
  } catch (e) {
    return String(d);
  }
};

export function fmtCurrency(value) {
  const n = Number(value ?? 0);
  try {
    return new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
      n
    );
  } catch (e) {
    return `${n.toFixed(2)} €`;
  }
}

export function fmtDateDMY(value) {
  if (!value) return "-";
  const d = new Date(value);
  if (isNaN(d)) return "-";
  const parts = new Intl.DateTimeFormat("en-GB", {
    timeZone: "Europe/Ljubljana",
    day: "2-digit",
    month: "2-digit",
    year: "numeric",
  }).formatToParts(d);
  const map = Object.fromEntries(parts.map((p) => [p.type, p.value]));
  return `${map.day}.${map.month}.${map.year}`;
}