function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 { useRef, useState } from 'react';

/**
 * useActiveKey Hook 参数
 */

/**
 * useActiveKey Hook 返回值
 */

/**
 * Tabbar 激活状态管理 Hook
 * 
 * 用于管理多层级 Tabbar 的激活状态，支持级联选择
 * 
 * @param props - Hook 配置参数
 * @returns 激活状态和操作方法
 * 
 * @example
 * ```tsx
 * const { activeKey, setActiveKey, getActiveKey } = useActiveKey({
 *   initialActiveKey: [1, 10],
 *   onChange: (value) => console.log('当前选中:', value)
 * });
 * 
 * // 设置第 0 层级的 key，并清除后续层级
 * setActiveKey(0, '2');
 * 
 * // 设置第 0 层级的 key，并同时设置第 1 层级
 * setActiveKey(0, '2', '20');
 * 
 * // 获取第 0 层级的 key
 * const level0Key = getActiveKey(0);
 * ```
 */
var useActiveKey = function useActiveKey(props) {
  var _props$initialActiveK = props.initialActiveKey,
    initialActiveKey = _props$initialActiveK === void 0 ? [] : _props$initialActiveK,
    onChange = props.onChange;
  var _useState = useState(initialActiveKey),
    _useState2 = _slicedToArray(_useState, 2),
    activeKey = _useState2[0],
    _setActiveKey2 = _useState2[1];
  var cacheActiveKey = useRef([]);

  // 设置缓存key
  var setCacheActiveKey = function setCacheActiveKey(val) {
    cacheActiveKey.current = _toConsumableArray(val);
  };

  // 重置value为缓存key
  var resetActiveKey = function resetActiveKey() {
    var val = cacheActiveKey.current;
    _setActiveKey2(val);
    onChange === null || onChange === void 0 || onChange(val);
    cacheActiveKey.current = [];
  };

  /**
   * 设置指定层级的激活 key
   * 
   * @param level - 层级索引（0-based）
   * @param key - 要设置的 key 值
   * @param cKey - 可选的子级 key（如果有子级需要同时激活）
   * 
   * @description
   * 设置时会自动清除该层级之后的所有层级 key，保证层级逻辑的一致性
   */
  var setActiveKey = function setActiveKey(level, key, cKey) {
    var newActiveKey = _toConsumableArray(activeKey);
    // 删除该层级之后的所有层级
    newActiveKey.splice(level + 1);
    // 设置当前层级
    newActiveKey[level] = String(key);

    // 如果提供了子级 key，同时设置
    if (cKey !== undefined) {
      newActiveKey[level + 1] = String(cKey);
    }
    _setActiveKey2(newActiveKey);
    onChange === null || onChange === void 0 || onChange(newActiveKey);
  };

  /**
   * 获取指定层级的激活 key
   * 
   * @param level - 层级索引（0-based）
   * @returns 该层级的激活 key
   */
  var getActiveKey = function getActiveKey(level) {
    return activeKey === null || activeKey === void 0 ? void 0 : activeKey[level];
  };
  return {
    activeKey: activeKey,
    setActiveKey: setActiveKey,
    getActiveKey: getActiveKey,
    resetActiveKey: resetActiveKey,
    getCacheActiveKey: function getCacheActiveKey() {
      return cacheActiveKey.current;
    },
    setCacheActiveKey: setCacheActiveKey,
    /**
     * 内部设置方法，用于同步外部传入的 value
     * 会进行深比较，避免不必要的更新
     */
    _setActiveKey: function _setActiveKey(value) {
      if (JSON.stringify(value) !== JSON.stringify(activeKey)) {
        _setActiveKey2(value || initialActiveKey);
      }
    }
  };
};
export default useActiveKey;