// strikeX — Core loop diagram (light). Inlined from guidelines/wheel-core-light.card.html
// so the CC/CSP center loop renders inside the Strategy section's Wheel card. Modeled on
// WheelGraph: a fading comet beam runs every edge (6 layered #co-beamPaths dashes), and each
// block is hover-shiny in its product-palette color, swapping its label to the product token
// (e.g. CALL_P) on hover; the T/Cash state nodes shine neutral and keep their label. Glow id
// namespaced (co-glow) so it never collides with the other inline SVGs. Exposes window.CoreLoop.

// Block (keyed by its rect's x) → { palette p, token t shown on hover }. The two combined
// legs run warm (CALL_P) and cold (PUT_P); the T/Cash state circles + arrows shine neutral.
const CO_NODES = {
  "414": { p: "flame",  t: "call_p" }, // T − CALL_S0
  "793": { p: "cobalt", t: "put_p"  }, // Cash − PUT_S0
};

const CORE_LOOP_SVG = `
<svg viewBox="120 470 1200 300" role="img" aria-label="Core loop, light">
  <defs>
    <filter id="co-glow" x="-40%" y="-40%" width="180%" height="180%">
      <feGaussianBlur stdDeviation="2.2" result="b"></feGaussianBlur>
      <feMerge><feMergeNode in="b"></feMergeNode><feMergeNode in="SourceGraphic"></feMergeNode></feMerge>
    </filter>
    <g id="co-beamPaths" fill="none">
      <path pathLength="100" d="M255,517 L414,517"></path>
      <path pathLength="100" d="M620,517 L816,517"></path>
      <path pathLength="100" d="M520,550 L520,658"></path>
      <path pathLength="100" d="M900,668 L900,558"></path>
      <path pathLength="100" d="M793,701 L606,701"></path>
      <path pathLength="100" d="M1150,701 L1011,701"></path>
      <path pathLength="100" d="M978,517 L1228,517 L1228,679"></path>
      <path pathLength="100" d="M172,497 L172,701 L436,701"></path>
    </g>
  </defs>
  <path class="edge" d="M255,517 L414,517"></path>
  <path class="edge" d="M620,517 L816,517"></path>
  <path class="edge" d="M520,550 L520,658"></path>
  <path class="edge" d="M900,668 L900,558"></path>
  <path class="edge" d="M793,701 L606,701"></path>
  <path class="edge" d="M1150,701 L1011,701"></path>
  <path class="edge" d="M978,517 L1228,517 L1228,679"></path>
  <path class="edge" d="M172,497 L172,701 L436,701"></path>
  <g class="beams" filter="url(#co-glow)">
    <g stroke-opacity="0.4" stroke-dasharray="28 72"><animate attributeName="stroke-dashoffset" values="28;-72" dur="2.6s" repeatCount="indefinite"></animate><use href="#co-beamPaths"></use></g>
    <g stroke-opacity="0.4" stroke-dasharray="21 79"><animate attributeName="stroke-dashoffset" values="21;-79" dur="2.6s" repeatCount="indefinite"></animate><use href="#co-beamPaths"></use></g>
    <g stroke-opacity="0.4" stroke-dasharray="15 85"><animate attributeName="stroke-dashoffset" values="15;-85" dur="2.6s" repeatCount="indefinite"></animate><use href="#co-beamPaths"></use></g>
    <g stroke-opacity="0.4" stroke-dasharray="10 90"><animate attributeName="stroke-dashoffset" values="10;-90" dur="2.6s" repeatCount="indefinite"></animate><use href="#co-beamPaths"></use></g>
    <g stroke-opacity="0.4" stroke-dasharray="6 94"><animate attributeName="stroke-dashoffset" values="6;-94" dur="2.6s" repeatCount="indefinite"></animate><use href="#co-beamPaths"></use></g>
    <g stroke-opacity="0.4" stroke-dasharray="3 97"><animate attributeName="stroke-dashoffset" values="3;-97" dur="2.6s" repeatCount="indefinite"></animate><use href="#co-beamPaths"></use></g>
  </g>
  <polygon class="red" points="132,497 212,497 212,477 256,517 212,557 212,537 132,537" stroke-linejoin="round"></polygon>
  <text class="state" x="170" y="525" text-anchor="middle">T</text>
  <polygon class="red" points="1274,679 1182,679 1182,659 1136,701 1182,743 1182,723 1274,723" stroke-linejoin="round"></polygon>
  <text class="state" x="1230" y="709" text-anchor="middle">Cash</text>
  <rect class="box" x="414" y="485" width="206" height="66" rx="4"></rect>
  <text class="lbl" x="520" y="523" text-anchor="middle">T − CALL<tspan class="sub">S0</tspan></text>
  <ellipse class="box" cx="900" cy="517" rx="80" ry="41"></ellipse>
  <text class="state" x="900" y="525" text-anchor="middle">Cash</text>
  <ellipse class="box" cx="520" cy="701" rx="86" ry="43"></ellipse>
  <text class="state" x="520" y="709" text-anchor="middle">T</text>
  <rect class="box" x="793" y="668" width="214" height="66" rx="4"></rect>
  <text class="lbl" x="900" y="707" text-anchor="middle">Cash − PUT<tspan class="sub">S0</tspan></text>
</svg>`;

function CoreLoop({ style }) {
  const ref = React.useRef(null);

  // Tag each block with its palette class (CSS :hover shine) and, on hover, crossfade its
  // label to the product token — reverting on mouse-leave. Mirrors WheelGraph.
  React.useLayoutEffect(() => {
    const root = ref.current && ref.current.querySelector("svg");
    if (!root) return;
    const texts = Array.from(root.querySelectorAll("text"));
    const labelInside = (r) => {
      const x = +r.getAttribute("x"), y = +r.getAttribute("y");
      const w = +r.getAttribute("width"), h = +r.getAttribute("height");
      return texts.find((t) => {
        const tx = +t.getAttribute("x"), ty = +t.getAttribute("y");
        return tx >= x && tx <= x + w && ty >= y && ty <= y + h;
      });
    };
    const cleanups = [];
    root.querySelectorAll("rect").forEach((r) => {
      const node = CO_NODES[r.getAttribute("x")];
      if (!node) return;
      r.classList.add("hv-node", "hv-" + node.p);
      const label = labelInside(r);
      if (!label) return;
      const original = label.innerHTML;
      label.style.transition = "opacity .14s ease";
      let timer;
      const swap = (apply) => {
        clearTimeout(timer);
        label.style.opacity = "0";
        timer = setTimeout(() => { apply(); label.style.opacity = "1"; }, 140);
      };
      const enter = () => swap(() => { label.textContent = node.t.toUpperCase(); });
      const leave = () => swap(() => { label.innerHTML = original; });
      r.addEventListener("mouseenter", enter);
      r.addEventListener("mouseleave", leave);
      cleanups.push(() => { clearTimeout(timer); r.removeEventListener("mouseenter", enter); r.removeEventListener("mouseleave", leave); });
    });
    // T & Cash state nodes (circles + entry arrows) shine neutral; their labels stay.
    root.querySelectorAll("ellipse, polygon").forEach((el) => el.classList.add("hv-node", "hv-neutral"));
    return () => cleanups.forEach((fn) => fn());
  }, []);

  return (
    <div ref={ref} className="core-loop" style={{ width: "100%", maxWidth: 800, margin: "0 auto", padding: 14, boxSizing: "border-box", ...style }}>
      <style>{`
        .core-loop { animation: co-enter 640ms cubic-bezier(0.22, 1, 0.36, 1) both; }
        @keyframes co-enter { from { opacity: 0; transform: translateY(14px) scale(0.97); } to { opacity: 1; transform: none; } }
        .core-loop svg { width: 100%; height: auto; display: block; animation: co-fade 760ms ease-out 160ms both; }
        @keyframes co-fade { from { opacity: 0; } to { opacity: 1; } }
        /* Labels overlap the blocks — let pointer events fall through so hovering a label
           still triggers its block's shine. */
        .core-loop svg text { pointer-events: none; }
        .core-loop .box  { fill: #FFFFFF; stroke: var(--gray-300); stroke-width: 3; }
        .core-loop .red  { fill: #FFFFFF; stroke: var(--gray-300); stroke-width: 3; }
        .core-loop .edge { fill: none; stroke: var(--gray-300); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
        /* Comet beam: a fading head built from 6 layered dashes sharing one moving point. */
        .core-loop .beams { fill: none; stroke: var(--blue-500); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
        @media (prefers-reduced-motion: reduce) { .core-loop, .core-loop svg { animation: none; } }
        .core-loop .lbl   { font-family: var(--font-mono); font-size: 21px; letter-spacing: .01em; fill: var(--gray-900); }
        .core-loop .state { font-family: var(--font-mono); font-size: 22px; letter-spacing: .01em; fill: var(--gray-900); }
        .core-loop .sub   { font-size: 0.66em; baseline-shift: sub; }
        /* Per-block hover shine — each block lights up in its product-palette color. */
        .core-loop .hv-node { cursor: pointer; transition: stroke .35s ease, fill .35s ease, filter .35s ease; }
        .core-loop .hv-flame:hover   { stroke: var(--flame-400);  fill: var(--flame-50);  filter: drop-shadow(0 0 12px var(--flame-300)); }
        .core-loop .hv-cobalt:hover  { stroke: var(--cobalt-400); fill: var(--cobalt-50); filter: drop-shadow(0 0 12px var(--cobalt-300)); }
        .core-loop .hv-neutral:hover { stroke: var(--gray-500);   fill: var(--gray-100);  filter: drop-shadow(0 0 12px var(--gray-300)); }
      `}</style>
      <div dangerouslySetInnerHTML={{ __html: CORE_LOOP_SVG }} />
    </div>
  );
}

window.CoreLoop = CoreLoop;
