// core/data.jsx — vulnerabilitycore runtime data container.
// No static/mock data: every collection below is populated at runtime from the
// backend API (see core/api.js + app.jsx). Only fixed taxonomy/helpers live here.

// Status workflow (triage). "remediated" is persisted in the DB (the remediated
// column, set via PUT /api/vulnerabilities/{id}/remediated); the other intermediate
// statuses remain a client-side overlay in localStorage.
const STATUS = {
  new:          { label: "Open",         order: 0 },
  triaged:      { label: "Triaged",      order: 1 },
  "in-progress":{ label: "In progress",  order: 2 },
  remediated:   { label: "Remediated",   order: 3 },
  "accepted":   { label: "Accepted risk",order: 4 },
};

function severityRank(s){ return { Critical:0, High:1, Medium:2, Low:3 }[s]; }

// Live collections — filled by the API after login. Empty until then.
const VULNS = [];          // GET /api/ledger (lazy, via ensureCatalog) — backs SBOM correlation
const TREND = [];          // GET /api/dashboard -> trend
const TOP_ECUS = [];       // GET /api/dashboard -> topEcus
const COVERAGE = {         // GET /api/dashboard -> coverage
  vsNvd: 0, sources: 0, lastSync: "—", classified: 0, freshnessHrs: 0,
};
const SUPPLIERS = [];      // GET /api/meta -> suppliers (Tier-1 companies)
const BRANDS = [];         // GET /api/meta -> brands (OEM companies)

// Lazily load the full ledger catalog ONCE and cache it on window.VC.VULNS.
// Each page has its own endpoint (dashboard/ledger/browse/detail/components); this
// shared cache only backs the client-side SBOM correlation engine used by the
// Components page + wizard (which need the whole catalog to match libraries).
let _catalogPromise = null;
function ensureCatalog() {
  if (_catalogPromise) return _catalogPromise;
  // Only while it is still the read in hand: resetCatalog() at sign-out drops it, and a late
  // answer must not land in the next user's session.
  const p = window.VCApi.ledger()
    .then((cat) => {
      const items = (cat && cat.items) || [];
      if (_catalogPromise === p) window.VC.VULNS = items;
      return items;
    })
    .catch((e) => { if (_catalogPromise === p) _catalogPromise = null; throw e; });
  _catalogPromise = p;
  return p;
}
function resetCatalog() { _catalogPromise = null; window.VC.VULNS = []; }

window.VC = {
  VULNS, TREND, TOP_ECUS, COVERAGE, SUPPLIERS, BRANDS, STATUS,
  severityRank, ensureCatalog, resetCatalog,
};
