/* NY taxi cursor with contextual labels */
function Cursor({ enabled }) {
  const ringRef = React.useRef(null);
  const labelRef = React.useRef(null);
  const [state, setState] = React.useState("default");
  const facingRef = React.useRef(1); // 1 right, -1 left — kept in a ref so direction flips don't tear down the RAF loop
  const lastXRef = React.useRef(0);

  React.useEffect(() => {
    if (!enabled) return;
    let x = window.innerWidth / 2, y = window.innerHeight / 2;
    let tx = x, ty = y, raf;
    let lastState = "default";

    const onMove = (e) => {
      if (e.clientX > lastXRef.current) facingRef.current = 1;
      else if (e.clientX < lastXRef.current) facingRef.current = -1;
      lastXRef.current = e.clientX;
      tx = e.clientX; ty = e.clientY;
    };
    const tick = () => {
      x += (tx - x) * 0.55;
      y += (ty - y) * 0.55;
      if (ringRef.current)  ringRef.current.style.transform  = `translate3d(${x}px, ${y}px, 0) translate(-50%, -50%) scaleX(${facingRef.current})`;
      if (labelRef.current) labelRef.current.style.transform = `translate3d(${x}px, ${y}px, 0)`;
      raf = requestAnimationFrame(tick);
    };
    tick();
    window.addEventListener("mousemove", onMove, { passive: true });

    const onOver = (e) => {
      let next = "default";
      const t = e.target.closest && e.target.closest("[data-cursor]");
      if (t) {
        next = t.getAttribute("data-cursor") || "hover";
      } else if (e.target.closest && e.target.closest("a, button, .pill, .tweak-opt, [role='button']")) {
        next = "hover";
      } else if (e.target.matches && e.target.matches("input, textarea")) {
        next = "text";
      }
      if (next !== lastState) {
        lastState = next;
        setState(next);
      }
    };
    window.addEventListener("mouseover", onOver);

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

  if (!enabled) return null;
  return (
    <div ref={ringRef} className="cursor" data-state={state}>
      <TaxiSVG />
    </div>
  );
}

function TaxiSVG() {
  return (
    <svg viewBox="0 0 60 32" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      {/* body */}
      <path d="M4 22 L8 14 L14 11 L26 10 L40 11 L50 14 L56 18 L56 24 L4 24 Z" fill="#F6C445" stroke="#0A0A0A" strokeWidth="1.2"/>
      {/* roof sign */}
      <rect x="22" y="6" width="14" height="4" fill="#F6C445" stroke="#0A0A0A" strokeWidth="1"/>
      <text x="29" y="9.5" textAnchor="middle" fontFamily="monospace" fontSize="3" fill="#0A0A0A" fontWeight="700">TAXI</text>
      {/* windows */}
      <path d="M14 13 L20 12 L22 17 L14 17 Z" fill="#0A0A0A" opacity="0.85"/>
      <path d="M24 12 L34 12 L34 17 L24 17 Z" fill="#0A0A0A" opacity="0.85"/>
      <path d="M36 12 L44 14 L46 17 L36 17 Z" fill="#0A0A0A" opacity="0.85"/>
      {/* checker stripe */}
      <g>
        <rect x="10" y="19" width="4" height="2" fill="#0A0A0A"/>
        <rect x="18" y="19" width="4" height="2" fill="#0A0A0A"/>
        <rect x="26" y="19" width="4" height="2" fill="#0A0A0A"/>
        <rect x="34" y="19" width="4" height="2" fill="#0A0A0A"/>
        <rect x="42" y="19" width="4" height="2" fill="#0A0A0A"/>
      </g>
      {/* headlight */}
      <circle className="headlight" cx="54" cy="20" r="1.4" fill="#FFF8D6" opacity="0.9"/>
      {/* wheels */}
      <g>
        <circle cx="14" cy="25" r="3.2" fill="#0A0A0A"/>
        <circle cx="14" cy="25" r="1.2" fill="#F6C445"/>
        <circle cx="44" cy="25" r="3.2" fill="#0A0A0A"/>
        <circle cx="44" cy="25" r="1.2" fill="#F6C445"/>
      </g>
    </svg>
  );
}

window.Cursor = Cursor;
