/* ===== Icons + sample data ===== */
const _p = (d, extra) => React.createElement('path', Object.assign({ d }, extra || {}));
const ICONS = {
  chevDown: <polyline points="6 9 12 15 18 9" />,
  chevRight: <polyline points="9 6 15 12 9 18" />,
  calendar: <g><rect x="3" y="4" width="18" height="17" rx="2" /><line x1="3" y1="9" x2="21" y2="9" /><line x1="8" y1="2" x2="8" y2="6" /><line x1="16" y1="2" x2="16" y2="6" /></g>,
  globe: <g><circle cx="12" cy="12" r="9" /><line x1="3" y1="12" x2="21" y2="12" /><path d="M12 3c2.5 2.5 2.5 15 0 18M12 3c-2.5 2.5-2.5 15 0 18" /></g>,
  wrench: <path d="M14.7 6.3a4 4 0 0 0-5.4 5.2L4 16.8 7.2 20l5.3-5.3a4 4 0 0 0 5.2-5.4l-2.5 2.5-2.3-2.3z" />,
  bell: <g><path d="M18 8a6 6 0 1 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" /><path d="M13.7 21a2 2 0 0 1-3.4 0" /></g>,
  user: <g><circle cx="12" cy="8" r="4" /><path d="M4 21a8 8 0 0 1 16 0" /></g>,
  funnel: <polygon points="3 4 21 4 14 12.5 14 20 10 18 10 12.5" />,
  dots: <g><circle cx="12" cy="5" r="1.4" /><circle cx="12" cy="12" r="1.4" /><circle cx="12" cy="19" r="1.4" /></g>,
  crosshair: <g><circle cx="12" cy="12" r="8.5" /><circle cx="12" cy="12" r="2.4" /><line x1="12" y1="1.5" x2="12" y2="5.5" /><line x1="12" y1="18.5" x2="12" y2="22.5" /><line x1="1.5" y1="12" x2="5.5" y2="12" /><line x1="18.5" y1="12" x2="22.5" y2="12" /></g>,
  expand: <g><polyline points="14 4 20 4 20 10" /><polyline points="10 20 4 20 4 14" /><line x1="20" y1="4" x2="13" y2="11" /><line x1="4" y1="20" x2="11" y2="13" /></g>,
  info: <g><circle cx="12" cy="12" r="9" /><line x1="12" y1="11" x2="12" y2="16" /><circle cx="12" cy="7.6" r="0.6" fill="currentColor" /></g>,
  share: <g><circle cx="6" cy="12" r="2.4" /><circle cx="17" cy="6" r="2.4" /><circle cx="17" cy="18" r="2.4" /><line x1="8.1" y1="11" x2="14.9" y2="7" /><line x1="8.1" y1="13" x2="14.9" y2="17" /></g>,
  check: <polyline points="4 12 9 17 20 6" />,
  play: <polygon points="6 4 20 12 6 20" fill="currentColor" stroke="none" />,
  search: <g><circle cx="11" cy="11" r="7" /><line x1="16.5" y1="16.5" x2="21" y2="21" /></g>,
  sort: <g><line x1="4" y1="8" x2="20" y2="8" /><line x1="7" y1="13" x2="17" y2="13" /><line x1="10" y1="18" x2="14" y2="18" /></g>,
};
function Icon({ name, size = 18, sw = 1.8, style, className }) {
  return (
    <span className={"icon" + (className ? " " + className : "")} style={style}>
      <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
        stroke="currentColor" strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
        {ICONS[name]}
      </svg>
    </span>
  );
}

/* ----- Scope hierarchy data (per tab) ----- */
const SCOPE_DATA = {
  portfolio: {
    count: 216,
    cols: ["Grouped", "Name", "Clearing House", "Future Tenor", "Term Code", "Shock Type", "Tenor Dimension"],
    tree: [
      { id: "ir", name: "IR", count: 23600, children: [
        { id: "ir-vol", name: "Vol", count: 12794, leaves: [
          { name: "USD IRVOL 1Y", ch: "LCH", ft: "1Y", tc: "ON", st: "Relative", td: "Standard" },
          { name: "EUR IRVOL 2Y", ch: "Eurex", ft: "2Y", tc: "1M", st: "Absolute", td: "Standard" },
          { name: "GBP IRVOL 5Y", ch: "LCH", ft: "5Y", tc: "3M", st: "Relative", td: "Extended" },
        ]},
        { id: "ir-basis", name: "Basis", count: 8216, leaves: [
          { name: "USD-EUR Basis", ch: "LCH", ft: "3Y", tc: "3M", st: "Absolute", td: "Standard" },
          { name: "USD-JPY Basis", ch: "JSCC", ft: "5Y", tc: "6M", st: "Absolute", td: "Standard" },
        ]},
        { id: "ir-base", name: "Base", count: 2590, leaves: [
          { name: "USD SOFR Curve", ch: "LCH", ft: "10Y", tc: "ON", st: "Relative", td: "Standard" },
        ]},
      ]},
      { id: "fx", name: "FX", count: 4837, children: [
        { id: "fx-spot", name: "Spot", count: 41, leaves: [
          { name: "EUR/USD Spot", ch: "—", ft: "Spot", tc: "ON", st: "Relative", td: "Standard" },
          { name: "USD/JPY Spot", ch: "—", ft: "Spot", tc: "ON", st: "Relative", td: "Standard" },
        ]},
        { id: "fx-vol", name: "Vol", count: 4796, leaves: [
          { name: "EUR/USD Vol 1M", ch: "—", ft: "1M", tc: "1M", st: "Relative", td: "Standard" },
        ]},
      ]},
      { id: "credit", name: "Credit", count: 66, children: [
        { id: "cr-corp", name: "CorpBond", count: 56, leaves: [
          { name: "IG Corp 5Y", ch: "ICE", ft: "5Y", tc: "3M", st: "Absolute", td: "Standard" },
          { name: "HY Corp 3Y", ch: "ICE", ft: "3Y", tc: "3M", st: "Absolute", td: "Standard" },
        ]},
      ]},
      { id: "eq", name: "Equity", count: 1184, children: [
        { id: "eq-spot", name: "Spot", count: 612, leaves: [
          { name: "SPX Index", ch: "—", ft: "Spot", tc: "ON", st: "Relative", td: "Standard" },
        ]},
        { id: "eq-vol", name: "Vol", count: 572, leaves: [] },
      ]},
    ],
  },
  product: {
    count: 216,
    cols: ["Grouped", "Name", "Clearing House", "Future Tenor", "Term Code", "Shock Type", "Tenor Dimension"],
    tree: [
      { id: "lin", name: "Linear", count: 9420, children: [
        { id: "lin-swap", name: "Swaps", count: 5210, leaves: [
          { name: "Vanilla IRS", ch: "LCH", ft: "Various", tc: "—", st: "Relative", td: "Standard" },
          { name: "Basis Swap", ch: "LCH", ft: "Various", tc: "—", st: "Relative", td: "Standard" },
        ]},
        { id: "lin-fut", name: "Futures", count: 4210, leaves: [
          { name: "Eurodollar Fut", ch: "CME", ft: "3M", tc: "3M", st: "Absolute", td: "Standard" },
        ]},
      ]},
      { id: "opt", name: "Options", count: 14012, children: [
        { id: "opt-swpt", name: "Swaptions", count: 9300, leaves: [
          { name: "USD Swaption", ch: "LCH", ft: "5Y", tc: "1Y", st: "Relative", td: "Extended" },
        ]},
        { id: "opt-fxo", name: "FX Options", count: 4712, leaves: [] },
      ]},
      { id: "struct", name: "Structured", count: 880, children: [
        { id: "struct-cln", name: "Credit-Linked", count: 880, leaves: [] },
      ]},
    ],
  },
  riskfactor: {
    count: 180,
    cols: ["Grouped", "Name", "Clearing House", "Future Tenor", "Term Code", "Shock Type", "Tenor Dimension"],
    tree: [
      { id: "rf-ir", name: "IR Curves", count: 92, children: [
        { id: "rf-sofr", name: "SOFR", count: 38, leaves: [
          { name: "SOFR 1Y Node", ch: "LCH", ft: "1Y", tc: "ON", st: "Relative", td: "Standard" },
          { name: "SOFR 5Y Node", ch: "LCH", ft: "5Y", tc: "ON", st: "Relative", td: "Standard" },
        ]},
        { id: "rf-estr", name: "ESTR", count: 30, leaves: [] },
        { id: "rf-sonia", name: "SONIA", count: 24, leaves: [] },
      ]},
      { id: "rf-vol", name: "Vol Surfaces", count: 58, children: [
        { id: "rf-irvol", name: "IR Vol", count: 34, leaves: [
          { name: "USD ATM Vol", ch: "LCH", ft: "ATM", tc: "1Y", st: "Relative", td: "Extended" },
        ]},
        { id: "rf-fxvol", name: "FX Vol", count: 24, leaves: [] },
      ]},
      { id: "rf-cs", name: "Credit Spreads", count: 30, children: [
        { id: "rf-cds", name: "CDS", count: 30, leaves: [
          { name: "CDX IG 5Y", ch: "ICE", ft: "5Y", tc: "3M", st: "Absolute", td: "Standard" },
        ]},
      ]},
    ],
  },
};

/* ----- config option sets ----- */
const OPT = {
  regulator: ["JFSA", "FRB", "NFA", "PRA", "ECB"],
  measure: ["VaR", "SVaR", "RM VaR", "BT VaR"],
  valuation: ["Risk Sens", "Full-Reval"],
  plInterp: ["Percentile", "Expected Shortfall", "Exponential Weighted"],
  holding: ["1-Day", "10-Day"],
  modelVersion: ["Prod", "Test"],
  tsVersion: ["TS_2026_05_PROD", "TS_2026_05_TEST", "TS_2026_04_PROD"],
  percentile: ["90%", "95%", "97.5%", "99%", "99.9%"],
  outVarSvar: ["Spot VaR", "Spot SVaR", "Spot VaR PL Strips", "Spot SVaR PL Strips"],
  outImpact: ["Marginal VaR", "Marginal SVaR", "Contribution VaR", "Contribution SVaR"],
  outAnalysis: ["VaR Analysis", "SVaR Analysis"],
  riskAnalysis: ["SVaR Window Selection", "RNiV", "RNiS", "VaR Benchmarking"],
};

/* ----- output row generator (reflects config) ----- */
function buildOutputRows(cfg) {
  const lvl = cfg.percentile;
  const type = cfg.measure;
  const base = [
    { cob: "2026-06-02", model: "MODEL_001", snap: "EOD", risk: "Market Risk", am: 1250000, es: 1450000 },
    { cob: "2026-06-02", model: "MODEL_002", snap: "Intraday", risk: "Credit Risk", am: 950000, es: 1100000 },
    { cob: "2026-06-02", model: "MODEL_003", snap: "EOD", risk: "Market Risk", am: 2100000, es: 2500000 },
    { cob: "2026-06-01", model: "MODEL_004", snap: "EOD", risk: "Liquidity Risk", am: 780000, es: 920000 },
    { cob: "2026-06-01", model: "MODEL_005", snap: "Intraday", risk: "Operational Risk", am: 450000, es: 600000 },
    { cob: "2026-05-30", model: "MODEL_006", snap: "EOD", risk: "Market Risk", am: 1750000, es: 2000000 },
    { cob: "2026-05-30", model: "MODEL_007", snap: "EOD", risk: "Market Risk", am: 1330000, es: 1580000 },
  ];
  return base.map((r, i) => ({
    ...r,
    type,
    level: lvl,
    date: r.cob,
  }));
}
const fmtNum = (n) => n.toLocaleString("en-US");

Object.assign(window, { Icon, SCOPE_DATA, OPT, buildOutputRows, fmtNum });
