// strikeX — Call ladder diagram (light). Inlined from guidelines/wheel-call-light.card.html
// so the BCSR call-rung ladder renders inside the Strategy section's Covered Call card.
// Modeled on WheelGraph: a fading comet beam runs every edge (6 layered #cl-beamPaths
// dashes), and each block is hover-shiny in its product-palette color, swapping its label
// to the product token (e.g. CALL_N) on hover. Glow id namespaced (cl-glow) so it never
// collides with the other inline SVGs. Exposes window.CallLadder.

// Block (keyed by its rect's x) → { palette p, token t shown on hover }. CALL legs run warm.
const CL_NODES = {
  "240":  { p: "clay", t: "call_np" }, "976":  { p: "clay", t: "call_np" }, // CALL−CALL spreads
  "289":  { p: "sun",  t: "call_n"  }, "551":  { p: "sun",  t: "call_n"  }, // +CALL legs
  "1048": { p: "sun",  t: "call_n"  },
};

const CALL_LADDER_SVG = `
<svg viewBox="130 100 1200 300" role="img" aria-label="Call ladder, light">
  <defs>
    <filter id="cl-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="cl-beamPaths" fill="none">
      <path pathLength="100" d="M380,305 L380,184"></path>
      <path pathLength="100" d="M467,334 L551,334"></path>
      <path pathLength="100" d="M640,305 L640,243 L748,243"></path>
      <path pathLength="100" d="M822,243 L960,243"></path>
    </g>
  </defs>
  <path class="edge" d="M380,305 L380,184"></path>
  <path class="edge" d="M467,334 L551,334"></path>
  <path class="edge" d="M640,305 L640,243 L748,243"></path>
  <path class="edge dash" d="M822,243 L960,243"></path>
  <g class="beams" filter="url(#cl-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="#cl-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="#cl-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="#cl-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="#cl-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="#cl-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="#cl-beamPaths"></use></g>
  </g>
  <text class="dots" x="788" y="243" text-anchor="middle">···</text>
  <text class="plus" x="1140" y="246" text-anchor="middle">+</text>
  <rect class="box" x="240" y="118" width="280" height="66" rx="4"></rect>
  <text class="lbl" x="380" y="157" text-anchor="middle">CALL<tspan class="sub">S0</tspan> − CALL<tspan class="sub">S1</tspan></text>
  <rect class="box" x="289" y="305" width="182" height="58" rx="4"></rect>
  <text class="lbl" x="380" y="340" text-anchor="middle">+CALL<tspan class="sub">S0</tspan></text>
  <rect class="box" x="551" y="305" width="182" height="58" rx="4"></rect>
  <text class="lbl" x="642" y="340" text-anchor="middle">+CALL<tspan class="sub">S1</tspan></text>
  <rect class="box" x="976" y="118" width="328" height="66" rx="4"></rect>
  <text class="lbl" x="1140" y="157" text-anchor="middle">CALL<tspan class="sub">Si−1</tspan> − CALL<tspan class="sub">Si</tspan></text>
  <rect class="box" x="1048" y="305" width="184" height="58" rx="4"></rect>
  <text class="lbl" x="1140" y="340" text-anchor="middle">+CALL<tspan class="sub">Si</tspan></text>
</svg>`;

function CallLadder({ 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 = CL_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); });
    });
    return () => cleanups.forEach((fn) => fn());
  }, []);

  return (
    <div ref={ref} className="call-ladder" style={{ width: "100%", maxWidth: 1280, margin: "0 auto", padding: 14, boxSizing: "border-box", ...style }}>
      <style>{`
        .call-ladder { animation: cl-enter 640ms cubic-bezier(0.22, 1, 0.36, 1) both; }
        @keyframes cl-enter { from { opacity: 0; transform: translateY(14px) scale(0.97); } to { opacity: 1; transform: none; } }
        .call-ladder svg { width: 100%; height: auto; display: block; animation: cl-fade 760ms ease-out 160ms both; }
        @keyframes cl-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. */
        .call-ladder svg text { pointer-events: none; }
        .call-ladder .box  { fill: #FFFFFF; stroke: var(--gray-300); stroke-width: 3; }
        .call-ladder .edge { fill: none; stroke: var(--gray-300); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
        .call-ladder .edge.dash { stroke-dasharray: 1.5 9; }
        /* Comet beam: a fading head built from 6 layered dashes sharing one moving point. */
        .call-ladder .beams { fill: none; stroke: var(--blue-500); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
        @media (prefers-reduced-motion: reduce) { .call-ladder, .call-ladder svg { animation: none; } }
        .call-ladder .lbl  { font-family: var(--font-mono); font-size: 21px; letter-spacing: .01em; fill: var(--gray-900); }
        .call-ladder .dots { font-family: var(--font-mono); font-size: 24px; letter-spacing: .14em; fill: var(--gray-400); }
        .call-ladder .plus { font-family: var(--font-mono); font-size: 28px; fill: var(--gray-500); }
        .call-ladder .sub  { font-size: 0.66em; baseline-shift: sub; }
        /* Per-block hover shine — each block lights up in its product-palette color. */
        .call-ladder .hv-node { cursor: pointer; transition: stroke .35s ease, fill .35s ease, filter .35s ease; }
        .call-ladder .hv-sun:hover  { stroke: var(--sun-400);  fill: var(--sun-50);  filter: drop-shadow(0 0 12px var(--sun-300)); }
        .call-ladder .hv-clay:hover { stroke: var(--clay-400); fill: var(--clay-50); filter: drop-shadow(0 0 12px var(--clay-300)); }
      `}</style>
      <div dangerouslySetInnerHTML={{ __html: CALL_LADDER_SVG }} />
    </div>
  );
}

window.CallLadder = CallLadder;
