// ============================================================
// bedside/antiemetics.js
// ANTIEMETICS — drugs.json-backed.
// ============================================================

import { S } from './shared.js';

// Fallback inline data — used if /data/drugs.json hasn't loaded yet.
// Must match drugs.json shape for the antiemetics section.
var EMET_FALLBACK = [
  { name: 'Ondansetron',      dose_mg_per_kg: 0.15, max_mg: 8,  unit: 'mg', route: 'PO / ODT / IV',   notes: 'First-line. Max single 8 mg. Repeat q8h. QT prolongation — avoid with other QT drugs. &lt;6 mo: limited data.', weight_band_dose: { under_15kg: '2 mg', '15_to_30kg': '4 mg', '30kg_or_more': '8 mg' } },
  { name: 'Metoclopramide',   dose_mg_per_kg: 0.15, max_mg: 10, unit: 'mg', route: 'IV / IM / PO',    notes: '0.1-0.15 mg/kg (max 10 mg). Give with diphenhydramine to prevent EPS / dystonia.' },
  { name: 'Dimenhydrinate',   dose_mg_per_kg: 1.25, max_mg: 50, unit: 'mg', route: 'PO / IV / IM / PR', notes: '1.25 mg/kg (max 50 mg) q6h. ≥2 yr.' },
  { name: 'Diphenhydramine',  dose_mg_per_kg: 1,    max_mg: 50, unit: 'mg', route: 'PO / IV / IM',    notes: '1 mg/kg (max 50 mg) q6h. Adjunct, sedating.' },
  { name: 'Promethazine',     dose_mg_per_kg: 0.25, max_mg: 25, unit: 'mg', route: 'PO / IV / IM',    notes: '0.25-1 mg/kg. <strong>CONTRAINDICATED &lt;2 yr</strong> (resp depression). Tissue injury if IV extrav.' },
  { name: 'Dexamethasone',    dose_mg_per_kg: 0.15, max_mg: 10, unit: 'mg', route: 'IV / PO',         notes: 'Adjunct, esp. chemo-induced. 0.15 mg/kg (max 10 mg).' },
  { name: 'Scopolamine patch', dose_mg_per_kg: null, max_mg: null, unit: 'mg', dose_display: '1.5 mg patch', route: 'Transdermal', notes: '≥12 yr. Motion sickness. Apply 4h before exposure.' }
];

// Prefer JSON-backed data if loaded; fall back to the inline list above.
function emetDrugs() {
  var s = window._DRUGS && window._DRUGS.sections && window._DRUGS.sections.antiemetics;
  return (s && s.drugs && s.drugs.length) ? s.drugs : EMET_FALLBACK;
}

// Iterate the drug list to produce S.drugRow(...) output, handling the
// special cases (Ondansetron weight-band prefix, Scopolamine fixed dose).
function emetRows(wt, drugs) {
  return drugs.map(function(dg) {
    var dose;
    if (dg.name === 'Ondansetron' && dg.weight_band_dose) {
      var bd = dg.weight_band_dose;
      var band = wt < 15 ? bd.under_15kg : wt < 30 ? bd['15_to_30kg'] : bd['30kg_or_more'];
      dose = band + ' (weight band) OR ' + S.dStr(wt, dg.dose_mg_per_kg, dg.max_mg, ' ' + dg.unit) + ' (' + dg.dose_mg_per_kg + ' ' + dg.unit + '/kg)';
    } else if (dg.dose_display) {
      dose = dg.dose_display;
    } else {
      dose = S.dStr(wt, dg.dose_mg_per_kg, dg.max_mg, ' ' + dg.unit);
    }
    return S.drugRow(dg.name, dose, dg.route, dg.notes);
  }).join('');
}

function calcEmet() {
  var wt = parseFloat(document.getElementById('emet-weight').value);
  var el = document.getElementById('emet-result');
  if (!wt) { el.innerHTML = '<p style="color:var(--red);font-size:13px;">Enter weight (kg).</p>'; return; }
  var html = '<div style="padding:10px 14px;border-radius:8px;background:var(--blue-light);border:1.5px solid var(--blue);margin-bottom:10px;"><strong style="color:var(--blue-dark);">Antiemetic doses — ' + wt + ' kg</strong></div>';
  html += S.drugTable(emetRows(wt, emetDrugs()));
  html += '<div style="padding:10px;background:#fef3c7;border-radius:6px;font-size:12px;color:#92400e;margin-top:10px;"><strong>Pearls:</strong> Ondansetron is first-line in ED for acute gastroenteritis vomiting — single PO/ODT dose increases oral rehydration success. Avoid anticholinergics + opioid combinations. Check QTc if stacking ondansetron + other QT drugs.</div>';
  html += S.ref('AAP gastroenteritis guidance; WHO essential medicines; Lexicomp.');
  el.innerHTML = html;
}

export function init() {
  document.addEventListener('click', function(e) {
    if (e.target.id === 'btn-emet-calc' || e.target.closest('#btn-emet-calc')) calcEmet();
  });
}
