var fentonCache = null;

export function loadFentonLmsData() {
  if (fentonCache) return Promise.resolve(fentonCache);
  return fetch('/data/calculators/fenton-lms.json', { credentials: 'same-origin' })
    .then(function(response) {
      if (!response.ok) throw new Error('Failed to load Fenton LMS data');
      return response.json();
    })
    .then(function(data) {
      fentonCache = data.weightLms || {};
      return fentonCache;
    });
}

export function interpolateLMS(table, val) {
  var keys = Object.keys(table).map(Number).sort(function(a,b){return a-b;});
  if (val <= keys[0]) return table[keys[0]];
  if (val >= keys[keys.length-1]) return table[keys[keys.length-1]];
  for (var i = 0; i < keys.length - 1; i++) {
    if (val >= keys[i] && val <= keys[i+1]) {
      var t = (val - keys[i]) / (keys[i+1] - keys[i]);
      var a = table[keys[i]], b = table[keys[i+1]];
      return { L: a.L + t*(b.L-a.L), M: a.M + t*(b.M-a.M), S: a.S + t*(b.S-a.S) };
    }
  }
  return table[keys[0]];
}

export function calcZ(value, L, M, S) {
  if (Math.abs(L) < 0.001) return Math.log(value / M) / S;
  return (Math.pow(value / M, L) - 1) / (L * S);
}

export function zToPercentile(z) {
  var a1=0.254829592, a2=-0.284496736, a3=1.421413741, a4=-1.453152027, a5=1.061405429, p=0.3275911;
  var sign = z < 0 ? -1 : 1;
  var x = Math.abs(z) / Math.sqrt(2);
  var t = 1 / (1 + p * x);
  var y = 1 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t * Math.exp(-x*x);
  return Math.round(((1 + sign * y) / 2) * 1000) / 10;
}

export function classifyGA(weeks, days) {
  var total = weeks + (days || 0) / 7;
  if (total < 28) return { label: 'Extremely Preterm', color: '#dc2626', icon: 'fa-triangle-exclamation' };
  if (total < 32) return { label: 'Very Preterm', color: '#ea580c', icon: 'fa-triangle-exclamation' };
  if (total < 34) return { label: 'Moderate Preterm', color: '#d97706', icon: 'fa-circle-exclamation' };
  if (total < 37) return { label: 'Late Preterm', color: '#ca8a04', icon: 'fa-circle-info' };
  if (total < 39) return { label: 'Early Term', color: '#2563eb', icon: 'fa-circle-info' };
  if (total < 41) return { label: 'Full Term', color: '#16a34a', icon: 'fa-circle-check' };
  if (total < 42) return { label: 'Late Term', color: '#d97706', icon: 'fa-circle-info' };
  return { label: 'Post Term', color: '#dc2626', icon: 'fa-triangle-exclamation' };
}

export function classifyWeight(percentile) {
  if (percentile < 3) return { label: 'Severely SGA', color: '#dc2626', detail: '<3rd percentile' };
  if (percentile < 10) return { label: 'SGA', color: '#ea580c', detail: '<10th percentile' };
  if (percentile > 97) return { label: 'Severely LGA', color: '#dc2626', detail: '>97th percentile' };
  if (percentile > 90) return { label: 'LGA', color: '#ea580c', detail: '>90th percentile' };
  return { label: 'AGA', color: '#16a34a', detail: '10th-90th percentile' };
}

export function classifyBirthWeight(grams) {
  if (grams < 1000) return { label: 'Extremely Low Birth Weight (ELBW)', color: '#dc2626' };
  if (grams < 1500) return { label: 'Very Low Birth Weight (VLBW)', color: '#ea580c' };
  if (grams < 2500) return { label: 'Low Birth Weight (LBW)', color: '#d97706' };
  if (grams <= 4000) return { label: 'Normal Birth Weight', color: '#16a34a' };
  return { label: 'Macrosomia (>4000g)', color: '#ea580c' };
}
