import { useCallback, useRef } from 'react';

/**
 * Hover 交互 Hook 的配置选项
 */

/**
 * Hover 交互处理 Hook
 * @param options - 配置选项
 * @returns 鼠标事件处理函数
 */
export var useMultilevelCardHover = function useMultilevelCardHover(options) {
  var _delay$enter, _delay$leave;
  var enabled = options.enabled,
    delay = options.delay,
    onExpandChange = options.onExpandChange;

  // 定时器引用
  var enterTimerRef = useRef(null);
  var leaveTimerRef = useRef(null);

  // 默认延迟时间
  var enterDelay = (_delay$enter = delay === null || delay === void 0 ? void 0 : delay.enter) !== null && _delay$enter !== void 0 ? _delay$enter : 100;
  var leaveDelay = (_delay$leave = delay === null || delay === void 0 ? void 0 : delay.leave) !== null && _delay$leave !== void 0 ? _delay$leave : 200;

  /**
   * 清除所有定时器
   */
  var clearTimers = useCallback(function () {
    if (enterTimerRef.current) {
      clearTimeout(enterTimerRef.current);
      enterTimerRef.current = null;
    }
    if (leaveTimerRef.current) {
      clearTimeout(leaveTimerRef.current);
      leaveTimerRef.current = null;
    }
  }, []);

  /**
   * 鼠标进入事件
   */
  var handleMouseEnter = useCallback(function () {
    if (!enabled) return;

    // 清除离开定时器
    if (leaveTimerRef.current) {
      clearTimeout(leaveTimerRef.current);
      leaveTimerRef.current = null;
    }

    // 设置进入定时器
    enterTimerRef.current = setTimeout(function () {
      onExpandChange(true);
    }, enterDelay);
  }, [enabled, enterDelay, onExpandChange]);

  /**
   * 鼠标离开事件
   */
  var handleMouseLeave = useCallback(function () {
    if (!enabled) return;

    // 清除进入定时器
    if (enterTimerRef.current) {
      clearTimeout(enterTimerRef.current);
      enterTimerRef.current = null;
    }

    // 设置离开定时器
    leaveTimerRef.current = setTimeout(function () {
      onExpandChange(false);
    }, leaveDelay);
  }, [enabled, leaveDelay, onExpandChange]);
  return {
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    clearTimers: clearTimers
  };
};