function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useEffect, useRef, useState } from 'react';

/**
 * 防抖搜索 Hook
 */

/**
 * 防抖搜索 Hook
 * 用于实时搜索模式的防抖处理
 */
export var useDebounceSearch = function useDebounceSearch(options) {
  var keyword = options.keyword,
    debounceTime = options.debounceTime,
    onSearch = options.onSearch,
    _options$enabled = options.enabled,
    enabled = _options$enabled === void 0 ? true : _options$enabled,
    lastSearchKeyword = options.lastSearchKeyword;
  var _useState = useState(keyword),
    _useState2 = _slicedToArray(_useState, 2),
    debouncedKeyword = _useState2[0],
    setDebouncedKeyword = _useState2[1];
  var timerRef = useRef(null);

  /**
   * 防抖处理
   */
  useEffect(function () {
    if (!enabled) {
      // 不启用防抖时，直接更新
      setDebouncedKeyword(keyword);
      return;
    }

    // 清除之前的定时器
    if (timerRef.current) {
      clearTimeout(timerRef.current);
    }

    // 设置新的定时器
    timerRef.current = setTimeout(function () {
      setDebouncedKeyword(keyword);
    }, debounceTime);

    // 清理函数
    return function () {
      if (timerRef.current) {
        clearTimeout(timerRef.current);
      }
    };
  }, [keyword, debounceTime, enabled]);

  /**
   * 触发搜索回调
   */
  useEffect(function () {
    // 只有在启用防抖时才触发搜索（包括空关键词，用于恢复数据）
    if (enabled && onSearch && debouncedKeyword !== undefined) {
      // 如果和上次搜索的关键词相同，跳过（避免重复触发）
      if (lastSearchKeyword !== undefined && debouncedKeyword === lastSearchKeyword) {
        return;
      }
      try {
        onSearch(debouncedKeyword);
      } catch (error) {
        console.error('Failed to trigger search:', error);
      }
    }
  }, [debouncedKeyword, onSearch, enabled, lastSearchKeyword]);
  return {
    debouncedKeyword: debouncedKeyword
  };
};