import { useRef, useState } from "react";
//#region src/components/PisellTabbar/hooks/useActiveKey.ts
/**
* 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);
* ```
*/
const useActiveKey = (props) => {
	const { initialActiveKey = [], onChange } = props;
	const [activeKey, _setActiveKey] = useState(initialActiveKey);
	const cacheActiveKey = useRef([]);
	const setCacheActiveKey = (val) => {
		cacheActiveKey.current = [...val];
	};
	const resetActiveKey = () => {
		let val = cacheActiveKey.current;
		_setActiveKey(val);
		onChange === null || onChange === void 0 || onChange(val);
		cacheActiveKey.current = [];
	};
	/**
	* 设置指定层级的激活 key
	* 
	* @param level - 层级索引（0-based）
	* @param key - 要设置的 key 值
	* @param cKey - 可选的子级 key（如果有子级需要同时激活）
	* 
	* @description
	* 设置时会自动清除该层级之后的所有层级 key，保证层级逻辑的一致性
	*/
	const setActiveKey = (level, key, cKey) => {
		const newActiveKey = [...activeKey];
		newActiveKey.splice(level + 1);
		newActiveKey[level] = String(key);
		if (cKey !== void 0) newActiveKey[level + 1] = String(cKey);
		_setActiveKey(newActiveKey);
		onChange === null || onChange === void 0 || onChange(newActiveKey);
	};
	/**
	* 获取指定层级的激活 key
	* 
	* @param level - 层级索引（0-based）
	* @returns 该层级的激活 key
	*/
	const getActiveKey = (level) => {
		return activeKey === null || activeKey === void 0 ? void 0 : activeKey[level];
	};
	return {
		activeKey,
		setActiveKey,
		getActiveKey,
		resetActiveKey,
		getCacheActiveKey: () => cacheActiveKey.current,
		setCacheActiveKey,
		_setActiveKey: (value) => {
			if (JSON.stringify(value) !== JSON.stringify(activeKey)) _setActiveKey(value || initialActiveKey);
		}
	};
};
//#endregion
export { useActiveKey as default };
