/**
 * ProtocolsTab — browse the framework's protocol map (KV.2, ported from VS Code).
 *
 * Primitives (the building blocks) + 36 domain families (what the framework is
 * FOR — emergency, automotive, biotech, manufacturing, finance, space…). A
 * discovery surface so users see the breadth and jump into compute. Static map,
 * mirrors zeq-vs-code-sdk/src/data/protocols.json.
 *
 * @version 1.287.24
 * @license CC BY 4.0 — Zeq. H / Zeq 1.287 Hz Limited
 */
const ProtocolsTab = (() => {
  "use strict";
  let _cid = null, _q = "", _open = {};
  const DATA = {"primitives":["ZFK","ZeqPulse","ZeqProof","ZeqLattice","ZeqShift"],"families":{"emergency":["911","triage","ambulance","disaster","search-rescue"],"automotive":["adas","battery","v2x","crash","autonomous"],"education":["adaptive-learning","assessment","tutoring","simulation","analytics"],"biotech":["biosignals","pharma","diagnostics","genomics"],"manufacturing":["cnc","quality","supply-chain","digital-twin","predictive"],"semiconductor":["chip-design","fpga","thermal","power","testing"],"forensics":["digital","dna","fingerprint","document"],"pharmacology":["drug-interaction","clinical-trial","dosing","formulation","pharmacovigilance"],"security":["encryption","channels","proofs","firewall"],"aviation":["flight-dynamics","atc","engine","structural","navigation"],"weather":["forecast","severe","marine","agricultural","solar"],"robotics":["kinematics","slam","manipulation","swarm","control"],"optics":["lens","laser","fiber","spectroscopy","holography"],"nutrition":["meal-planning","macros","allergy","supplement","metabolic"],"climate":["modeling","atmospheric","ocean"],"maritime":["navigation","hull","cargo","ballast","port"],"solver":["ode-solver","master-equation"],"math":["operators","constants","equations"],"space":["orbital","trajectory","radiation","comms"],"blockchain":["physics-blockchain","pohc"],"gamedev":["physics-engine","procgen","netcode","ai","audio"],"energy":["power-grid","solar","fusion"],"audio":["processing","music","speech","acoustic","codec"],"biology":["protein","ecosystems","neural","genetics"],"water":["purification","desalination","flood","groundwater","wastewater"],"nuclear":["reactor","shielding","dosimetry","waste","fusion"],"finance":["risk","pricing","audit"],"comms":["sms","voip","email","mesh"],"geoscience":["seismic","volcano","hydrology","mining","surveying"],"agriculture":["soil","irrigation","crop-disease","livestock","yield"],"rf":["spectrum","modulation","radio"],"materials":["stress","composites","nano","corrosion","3d-print"],"account":["usage","keys","status"],"infrastructure":["water","traffic","bridge","power-grid","smart-city"],"identity":["zeq-auth"],"misc":["zeqtext","globe","zeqcompress","hulyaforensics"]}};
  const PRIM_DESC = { ZFK: "Zeq Field Kernel — the field substrate", ZeqPulse: "the 1.287 Hz system clock", ZeqProof: "signed compute receipts", ZeqLattice: "multi-node compute", ZeqShift: "KO42 metric re-alignment" };

  function esc(s) { return String(s == null ? "" : s).replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[c])); }

  const STYLE = "<style>" +
    ".zp{padding:12px;overflow-y:auto;height:100%}" +
    ".zp h3{font-size:13px;margin:0 0 3px;color:var(--text-primary)}" +
    ".zp .lead{font-size:11px;color:var(--text-muted);line-height:1.5;margin:0 0 10px}" +
    ".zp input{width:100%;padding:7px 9px;background:rgba(0,0,0,.45);border:1px solid var(--border);border-radius:7px;color:var(--text-primary);font-size:12px;outline:none;margin-bottom:10px}" +
    ".zp input:focus{border-color:var(--accent-mint)}" +
    ".zp .h{font-size:10px;letter-spacing:.08em;text-transform:uppercase;color:var(--text-muted);margin:4px 0 6px}" +
    ".zp .prims{display:flex;flex-wrap:wrap;gap:5px;margin-bottom:12px}" +
    ".zp .prim{font-size:11px;padding:5px 9px;border-radius:7px;border:1px solid var(--accent-mint);background:rgba(0,255,136,.06);color:var(--accent-mint);font-family:'JetBrains Mono',monospace}" +
    ".zp .prim small{color:var(--text-muted);font-family:inherit;margin-left:5px}" +
    ".zp .fam{border:1px solid var(--border);border-radius:8px;margin-bottom:6px;overflow:hidden;background:var(--bg-secondary)}" +
    ".zp .famh{display:flex;justify-content:space-between;align-items:center;padding:8px 10px;cursor:pointer;font-size:12px;font-weight:600;color:var(--text-primary);text-transform:capitalize}" +
    ".zp .famh .n{font-size:10px;color:var(--text-muted);font-weight:400}" +
    ".zp .subs{display:flex;flex-wrap:wrap;gap:4px;padding:0 10px 9px}" +
    ".zp .sub{font-size:10px;padding:3px 7px;border-radius:5px;border:1px solid var(--border);color:var(--text-secondary);font-family:'JetBrains Mono',monospace}" +
    ".zp .empty{font-size:11px;color:var(--text-muted)}" +
    "</style>";

  function render() {
    const c = document.getElementById(_cid);
    if (!c) return;
    const q = _q.toLowerCase();
    const fams = Object.keys(DATA.families).filter((f) => !q || f.includes(q) || DATA.families[f].some((s) => s.includes(q)));
    const prims = '<div class="prims">' + DATA.primitives.map((p) => '<span class="prim">' + esc(p) + '<small>' + esc(PRIM_DESC[p] || "") + "</small></span>").join("") + "</div>";
    const famHtml = fams.length ? fams.map((f) => {
      const open = _open[f] || q;
      const subs = DATA.families[f];
      return '<div class="fam"><div class="famh" data-fam="' + esc(f) + '"><span>' + esc(f) + '</span><span class="n">' + subs.length + (open ? " ▾" : " ▸") + "</span></div>" +
        (open ? '<div class="subs">' + subs.map((s) => '<span class="sub">' + esc(s) + "</span>").join("") + "</div>" : "") + "</div>";
    }).join("") : '<div class="empty">No protocols match.</div>';
    c.innerHTML = STYLE + '<div class="zp"><h3>Protocols</h3>' +
      '<p class="lead">The building blocks (primitives) + 36 domain families the framework is built for. Browse the breadth, then compute against any of it.</p>' +
      '<input id="zp-q" placeholder="search protocols (e.g. battery, dna, orbital)" value="' + esc(_q) + '">' +
      '<div class="h">Primitives</div>' + prims +
      '<div class="h">Domain families</div>' + famHtml + "</div>";
    const qi = document.getElementById("zp-q");
    if (qi) { qi.addEventListener("input", () => { _q = qi.value; render(); const n = document.getElementById("zp-q"); if (n) { n.focus(); n.setSelectionRange(_q.length, _q.length); } }); }
    c.querySelectorAll(".famh[data-fam]").forEach((b) => b.addEventListener("click", () => { const f = b.getAttribute("data-fam"); _open[f] = !_open[f]; render(); }));
  }

  function init(cid) { _cid = cid; render(); }
  function stop() {}
  return { init, stop };
})();
if (typeof module !== "undefined" && module.exports) { module.exports = ProtocolsTab; }
