// fc-units.jsx — the units engine: the single conversion + rounding contract the
// app needs to be unit-correct in pounds. Weights are STORED in kg (source of
// truth, where the projection math lives) and CONVERTED for display.
//
//   OWUnits.toLb(kg) / toKg(lb)
//   OWUnits.fmtWeight(kg, unit, {limit})  → display string, 1 dp
//        limit:true rounds DOWN, so a "don't exceed" number never reads high
//   OWUnits.unitLabel(unit)               → 'KG' | 'LB'
//   OWUnits.step(unit)                     → input step (kg .1 / lb .2)
//   OWUnits.DIVISIONS_MMA                  → [name, lbLimit] reference table
(function () {
  const KG_TO_LB = 2.20462262;
  const toLb = (kg) => kg * KG_TO_LB;
  const toKg = (lb) => lb / KG_TO_LB;
  const round1 = (x) => Math.round(x * 10) / 10;
  const floor1 = (x) => Math.floor(x * 10) / 10;

  function fmtWeight(kg, unit = 'kg', opts = {}) {
    const v = unit === 'lb' ? toLb(kg) : kg;
    return (opts.limit ? floor1(v) : round1(v)).toFixed(1);
  }
  const unitLabel = (unit) => (unit === 'lb' ? 'LB' : 'KG');
  const step = (unit) => (unit === 'lb' ? 0.2 : 0.1);

  // Standard MMA classes (lb). Same shape extends to boxing / grappling tables.
  const DIVISIONS_MMA = [
    ['Flyweight', 125], ['Bantamweight', 135], ['Featherweight', 145],
    ['Lightweight', 155], ['Welterweight', 170], ['Middleweight', 185],
    ['Light Heavyweight', 205], ['Heavyweight', 265],
  ];

  window.OWUnits = { KG_TO_LB, toLb, toKg, fmtWeight, unitLabel, step, DIVISIONS_MMA };
})();
