"use client";
import { useState, useRef, useCallback, useEffect } from "react";
const defaultStillnessOptions = {
  duration: 25,
  maxVelocity: 10
};
function useHover({ delay = 0, earlyTriggerOnStill = false, onHoverChange } = {}) {
  const [isHovered, setIsHovered] = useState(false);
  const delayOnMouseEnter = typeof delay === "number" ? delay : delay[0];
  const delayOnMouseLeave = typeof delay === "number" ? delay : delay[1];
  const delayTimeoutRef = useRef();
  const stillTimeoutRef = useRef();
  const lastPositionRef = useRef();
  const stillnessOptions = (() => {
    if (earlyTriggerOnStill === true) return defaultStillnessOptions;
    if (typeof earlyTriggerOnStill === "object") return { ...defaultStillnessOptions, ...earlyTriggerOnStill };
    return null;
  })();
  const clearTimers = () => {
    [delayTimeoutRef, stillTimeoutRef].forEach((ref) => {
      if (ref.current) {
        window.clearTimeout(ref.current);
        ref.current = void 0;
      }
    });
  };
  const setHovered = useCallback(
    (hovered) => {
      setIsHovered(hovered);
      onHoverChange == null ? void 0 : onHoverChange(hovered);
    },
    [onHoverChange]
  );
  const handleMouseEnter = useCallback(
    (event) => {
      clearTimers();
      lastPositionRef.current = { x: event.clientX, y: event.clientY, time: Date.now() };
      if (stillnessOptions == null ? void 0 : stillnessOptions.duration) {
        stillTimeoutRef.current = window.setTimeout(() => setHovered(true), stillnessOptions.duration);
      }
      if (delayOnMouseEnter > 0) {
        delayTimeoutRef.current = window.setTimeout(() => setHovered(true), delayOnMouseEnter);
      } else {
        setHovered(true);
      }
    },
    [stillnessOptions == null ? void 0 : stillnessOptions.duration, delayOnMouseEnter, setHovered]
  );
  const handleMouseMove = useCallback(
    (event) => {
      if (!(stillnessOptions == null ? void 0 : stillnessOptions.duration)) return;
      const now = Date.now();
      const last = lastPositionRef.current;
      if (last && stillnessOptions.maxVelocity) {
        const dx = event.clientX - last.x;
        const dy = event.clientY - last.y;
        const dt = now - last.time;
        const velocity = Math.sqrt(dx * dx + dy * dy) / (dt / 1e3);
        if (velocity <= stillnessOptions.maxVelocity) {
          return;
        }
      }
      lastPositionRef.current = { x: event.clientX, y: event.clientY, time: now };
      if (stillTimeoutRef.current) {
        window.clearTimeout(stillTimeoutRef.current);
        stillTimeoutRef.current = window.setTimeout(() => setHovered(true), stillnessOptions.duration);
      }
    },
    [stillnessOptions, setHovered]
  );
  const handleMouseLeave = useCallback(() => {
    clearTimers();
    lastPositionRef.current = void 0;
    if (delayOnMouseLeave > 0) {
      delayTimeoutRef.current = window.setTimeout(() => setHovered(false), delayOnMouseLeave);
    } else {
      setHovered(false);
    }
  }, [delayOnMouseLeave, setHovered]);
  useEffect(() => {
    return clearTimers;
  }, []);
  return {
    isHovered,
    props: {
      onMouseEnter: handleMouseEnter,
      onMouseMove: handleMouseMove,
      onMouseLeave: handleMouseLeave
    }
  };
}
export {
  useHover
};
