// Blob cursor — morphs shape + color on hover over interactive elements
const { useEffect, useRef, useState } = React;

function BlobCursor({ palette }) {
  const blobRef = useRef(null);
  const dotRef = useRef(null);
  const pos = useRef({ x: -100, y: -100 });
  const target = useRef({ x: -100, y: -100 });
  const [mode, setMode] = useState("default"); // default | hover | text

  useEffect(() => {
    const onMove = (e) => {
      target.current.x = e.clientX;
      target.current.y = e.clientY;
      if (dotRef.current) {
        dotRef.current.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`;
      }
    };
    const onOver = (e) => {
      const el = e.target;
      if (!el.closest) return;
      if (el.closest('a, button, [role="button"], .tiu-hit')) setMode("hover");
      else if (el.closest('input, textarea, [contenteditable="true"]')) setMode("text");
      else setMode("default");
    };
    window.addEventListener("mousemove", onMove, { passive: true });
    window.addEventListener("mouseover", onOver, { passive: true });

    let raf;
    const tick = () => {
      pos.current.x += (target.current.x - pos.current.x) * 0.18;
      pos.current.y += (target.current.y - pos.current.y) * 0.18;
      if (blobRef.current) {
        blobRef.current.style.transform = `translate(${pos.current.x}px, ${pos.current.y}px)`;
      }
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);

    return () => {
      window.removeEventListener("mousemove", onMove);
      window.removeEventListener("mouseover", onOver);
      cancelAnimationFrame(raf);
    };
  }, []);

  // Scale based on mode
  const scale = mode === "hover" ? 1.9 : mode === "text" ? 0.45 : 1;
  const dotOpacity = mode === "default" ? 1 : 0;

  return (
    <>
      {/* Big blobby follower */}
      <div
        ref={blobRef}
        aria-hidden="true"
        style={{
          position: "fixed",
          top: 0, left: 0,
          width: 60, height: 60,
          marginLeft: -30, marginTop: -30,
          pointerEvents: "none",
          zIndex: 9998,
          mixBlendMode: "multiply",
          willChange: "transform",
        }}
      >
        <div
          style={{
            width: "100%", height: "100%",
            transform: `scale(${scale})`,
            transition: "transform 260ms cubic-bezier(.2,.9,.25,1.2)",
            willChange: "transform",
          }}
        >
          <svg viewBox="0 0 100 100" width="100%" height="100%">
            <defs>
              <radialGradient id="blobGrad" cx="35%" cy="35%" r="70%">
                <stop offset="0%"  stopColor={palette.blush} stopOpacity="0.95" />
                <stop offset="55%" stopColor={palette.sky}   stopOpacity="0.8"  />
                <stop offset="100%" stopColor={palette.sage} stopOpacity="0.65" />
              </radialGradient>
              <filter id="blobBlur">
                <feGaussianBlur stdDeviation="1.5" />
              </filter>
            </defs>
            <path
              filter="url(#blobBlur)"
              fill="url(#blobGrad)"
              d="M50 8 C 70 8, 92 22, 92 46 C 92 70, 76 92, 52 92 C 26 92, 8 74, 8 50 C 8 28, 30 8, 50 8 Z"
            >
              <animate
                attributeName="d"
                dur="7s"
                repeatCount="indefinite"
                values="
M50 8 C 70 8, 92 22, 92 46 C 92 70, 76 92, 52 92 C 26 92, 8 74, 8 50 C 8 28, 30 8, 50 8 Z;
M52 6 C 78 10, 94 28, 90 52 C 86 74, 70 94, 46 92 C 22 90, 6 70, 10 46 C 14 24, 30 4, 52 6 Z;
M48 10 C 68 6, 92 18, 94 44 C 96 68, 78 92, 54 92 C 28 92, 6 72, 10 48 C 14 26, 28 12, 48 10 Z;
M50 8 C 70 8, 92 22, 92 46 C 92 70, 76 92, 52 92 C 26 92, 8 74, 8 50 C 8 28, 30 8, 50 8 Z
                "
              />
            </path>
          </svg>
        </div>
      </div>

      {/* Inner precise dot */}
      <div
        ref={dotRef}
        aria-hidden="true"
        style={{
          position: "fixed",
          top: 0, left: 0,
          width: 6, height: 6,
          marginLeft: -3, marginTop: -3,
          borderRadius: "999px",
          background: palette.ink,
          opacity: dotOpacity,
          transition: "opacity 180ms",
          pointerEvents: "none",
          zIndex: 9999,
        }}
      />
    </>
  );
}

window.BlobCursor = BlobCursor;
