/* global React */
// =========================================================
// Unit conversion — pouze pro vítr (jediná uživatelsky volitelná jednotka).
// Teplota / tlak / srážky / čas se vždy zobrazují v defaultní jednotce,
// helpery jsou ponechány jako passthrough, aby konzumenti nemuseli řešit
// dvě cesty.
// =========================================================

window.UnitPrefs = window.UnitPrefs || { wind: 'mps' };

const WIND = {
  mps: { factor: 1,         label: 'm/s' },
  kmh: { factor: 3.6,       label: 'km/h' },
  mph: { factor: 2.23694,   label: 'mph' },
  kn:  { factor: 1.94384,   label: 'kn' },
};

window.Units = {
  temp(c)        { return (c == null || isNaN(c)) ? null : c; },
  tempLabel()    { return '°C'; },

  wind(mps) {
    if (mps == null || isNaN(mps)) return null;
    const u = window.UnitPrefs.wind || 'mps';
    return mps * (WIND[u] || WIND.mps).factor;
  },
  windLabel()    { return WIND[window.UnitPrefs.wind]?.label || 'm/s'; },

  pressure(hpa)  { return (hpa == null || isNaN(hpa)) ? null : hpa; },
  pressureLabel(){ return 'hPa'; },

  rain(mm)       { return (mm == null || isNaN(mm)) ? null : mm; },
  rainLabel()    { return 'mm'; },

  time(date) {
    if (!date) return '—';
    return date.toLocaleTimeString('cs-CZ', { hour: '2-digit', minute: '2-digit', hour12: false });
  },
};
