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 { useCallback, useState } from 'react';

/**
 * 搜索状态管理 Hook
 */

/**
 * 搜索状态管理 Hook
 * 支持受控和非受控模式
 */
export var useSearchState = function useSearchState(options) {
  var value = options.value,
    _options$defaultValue = options.defaultValue,
    defaultValue = _options$defaultValue === void 0 ? '' : _options$defaultValue,
    visibleProp = options.visible,
    _options$defaultVisib = options.defaultVisible,
    defaultVisible = _options$defaultVisib === void 0 ? false : _options$defaultVisib,
    onChange = options.onChange,
    onVisibleChange = options.onVisibleChange,
    onOpen = options.onOpen,
    onClose = options.onClose;

  // 判断是否为受控模式
  var isControlledKeyword = value !== undefined;
  var isControlledVisible = visibleProp !== undefined;

  // 内部状态（非受控模式使用）
  var _useState = useState(defaultValue),
    _useState2 = _slicedToArray(_useState, 2),
    internalKeyword = _useState2[0],
    setInternalKeyword = _useState2[1];
  var _useState3 = useState(defaultVisible),
    _useState4 = _slicedToArray(_useState3, 2),
    internalVisible = _useState4[0],
    setInternalVisible = _useState4[1];

  // 当前值（受控优先）
  var keyword = isControlledKeyword ? value : internalKeyword;
  var visible = isControlledVisible ? visibleProp : internalVisible;

  /**
   * 设置关键词
   */
  var setKeyword = useCallback(function (newValue) {
    // 非受控模式下更新内部状态
    if (!isControlledKeyword) {
      setInternalKeyword(newValue);
    }

    // 触发回调
    onChange === null || onChange === void 0 || onChange(newValue);
  }, [isControlledKeyword, onChange]);

  /**
   * 设置可见性
   */
  var setVisible = useCallback(function (newVisible) {
    var oldVisible = visible;

    // 非受控模式下更新内部状态
    if (!isControlledVisible) {
      setInternalVisible(newVisible);
    }

    // 触发回调
    onVisibleChange === null || onVisibleChange === void 0 || onVisibleChange(newVisible);

    // 触发打开/关闭回调
    if (newVisible && !oldVisible) {
      onOpen === null || onOpen === void 0 || onOpen();
    } else if (!newVisible && oldVisible) {
      onClose === null || onClose === void 0 || onClose();
    }
  }, [isControlledVisible, visible, onVisibleChange, onOpen, onClose]);

  /**
   * 打开
   */
  var open = useCallback(function () {
    setVisible(true);
  }, [setVisible]);

  /**
   * 关闭
   */
  var close = useCallback(function () {
    setVisible(false);
  }, [setVisible]);

  /**
   * 切换可见性
   */
  var toggle = useCallback(function () {
    setVisible(!visible);
  }, [setVisible, visible]);
  return {
    keyword: keyword,
    setKeyword: setKeyword,
    visible: visible,
    setVisible: setVisible,
    open: open,
    close: close,
    toggle: toggle,
    isControlledKeyword: isControlledKeyword,
    isControlledVisible: isControlledVisible
  };
};