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

// Block (keyed by its rect's x) → { palette p, token t shown on hover }. PUT legs run cold.
const PL_NODES = {
  "638": { p: "azure",  t: "put_n"  }, "888": { p: "azure",  t: "put_n"  }, // +PUT legs
  "268": { p: "azure",  t: "put_n"  },
  "853": { p: "indigo", t: "put_np" }, "233": { p: "indigo", t: "put_np" }, // PUT−PUT spreads
};

const PUT_LADDER_SVG = `
<svg viewBox="200 830 1130 300" role="img" aria-label="Put ladder, light">
  <defs>
    <filter id="pl-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="pl-beamPaths" fill="none">
      <path pathLength="100" d="M888,884 L804,884"></path>
      <path pathLength="100" d="M970,913 L970,1034"></path>
      <path pathLength="100" d="M720,913 L720,975 L648,975"></path>
      <path pathLength="100" d="M576,975 L472,975"></path>
    </g>
  </defs>
  <path class="edge" d="M888,884 L804,884"></path>
  <path class="edge" d="M970,913 L970,1034"></path>
  <path class="edge" d="M720,913 L720,975 L648,975"></path>
  <path class="edge dash" d="M576,975 L472,975"></path>
  <g class="beams" filter="url(#pl-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="#pl-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="#pl-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="#pl-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="#pl-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="#pl-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="#pl-beamPaths"></use></g>
  </g>
  <text class="dots" x="612" y="983" text-anchor="middle">···</text>
  <text class="plus" x="360" y="986" text-anchor="middle">+</text>
  <rect class="box" x="638" y="855" width="164" height="58" rx="4"></rect>
  <text class="lbl" x="720" y="890" text-anchor="middle">+PUT<tspan class="sub">S1</tspan></text>
  <rect class="box" x="888" y="855" width="164" height="58" rx="4"></rect>
  <text class="lbl" x="970" y="890" text-anchor="middle">+PUT<tspan class="sub">S0</tspan></text>
  <rect class="box" x="853" y="1034" width="234" height="66" rx="4"></rect>
  <text class="lbl" x="970" y="1073" text-anchor="middle">PUT<tspan class="sub">S0</tspan> − PUT<tspan class="sub">S1</tspan></text>
  <rect class="box" x="268" y="855" width="184" height="58" rx="4"></rect>
  <text class="lbl" x="360" y="890" text-anchor="middle">+PUT<tspan class="sub">Si</tspan></text>
  <rect class="box" x="233" y="1034" width="254" height="66" rx="4"></rect>
  <text class="lbl" x="360" y="1073" text-anchor="middle">PUT<tspan class="sub">Si−1</tspan> − PUT<tspan class="sub">Si</tspan></text>
</svg>`;

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

window.PutLadder = PutLadder;
