var vitalsCache = null;

export function loadVitalsData() {
  if (vitalsCache) return Promise.resolve(vitalsCache);
  return fetch('/data/calculators/vitals.json', { credentials: 'same-origin' })
    .then(function(response) {
      if (!response.ok) throw new Error('Failed to load vitals data');
      return response.json();
    })
    .then(function(data) {
      vitalsCache = data.ageGroups || {};
      return vitalsCache;
    });
}

export function renderVitalsResult(v) {
  return ''
    + '<div class="calc-result-header" style="background:#eff6ff;border-color:#2563eb;">'
    +   '<div style="font-size:18px;font-weight:700;color:#1d4ed8;">' + v.label + '</div>'
    + '</div>'
    + '<div class="calc-result-grid" style="margin-top:12px;">'
    +   '<div class="calc-result-item"><div class="calc-result-label">Heart Rate (awake)</div><div class="calc-result-value" style="font-size:18px;color:#ef4444;">' + v.hr.awake + '</div><div class="calc-result-sub">bpm</div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">Heart Rate (sleep)</div><div class="calc-result-value" style="font-size:18px;color:#8b5cf6;">' + v.hr.sleeping + '</div><div class="calc-result-sub">bpm</div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">Respiratory Rate</div><div class="calc-result-value" style="font-size:18px;color:#0ea5e9;">' + v.rr + '</div><div class="calc-result-sub">breaths/min</div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">Systolic BP</div><div class="calc-result-value" style="font-size:18px;color:#f97316;">' + v.sbp + '</div><div class="calc-result-sub">mmHg</div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">Diastolic BP</div><div class="calc-result-value" style="font-size:18px;color:#f59e0b;">' + v.dbp + '</div><div class="calc-result-sub">mmHg</div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">Temperature</div><div class="calc-result-value" style="font-size:18px;color:#10b981;">' + v.temp + '</div><div class="calc-result-sub">&deg;C</div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">SpO2</div><div class="calc-result-value" style="font-size:18px;color:#06b6d4;">' + v.spo2 + '</div><div class="calc-result-sub"></div></div>'
    +   '<div class="calc-result-item"><div class="calc-result-label">Weight Range</div><div class="calc-result-value" style="font-size:18px;color:#6366f1;">' + v.weight + '</div><div class="calc-result-sub"></div></div>'
    + '</div>'
    + '<div style="margin-top:14px;padding:12px;background:var(--g50);border-radius:8px;">'
    +   '<div style="font-size:12px;font-weight:600;color:var(--g600);margin-bottom:6px;">Clinical Notes</div>'
    +   '<ul style="margin:0;padding-left:16px;font-size:12px;color:var(--g700);line-height:1.7;">'
    +     v.notes.map(function(n) { return '<li>' + n + '</li>'; }).join('')
    +   '</ul>'
    + '</div>';
}
