// data.jsx — VulnerabilityCore runtime data container.
// No static/mock data: every collection below is populated at runtime from the
// backend API (see 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: "New",          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;
  _catalogPromise = window.VCApi.ledger()
    .then((cat) => { window.VC.VULNS = (cat && cat.items) || []; return window.VC.VULNS; })
    .catch((e) => { _catalogPromise = null; throw e; });
  return _catalogPromise;
}
function resetCatalog() { _catalogPromise = null; window.VC.VULNS = []; }

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