require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/components/list/hooks/useListState.ts
function listStateReducer(state, action) {
	switch (action.type) {
		case "SET_ACTIVE_TAB": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state), {}, { activeTab: action.payload });
		case "SET_CURRENT_PAGE": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state), {}, { currentPage: action.payload });
		case "SET_STICKY_ACTIVE": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state), {}, { isStickyActive: action.payload });
		case "RESET_PAGE": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state), {}, { currentPage: 1 });
		default: return state;
	}
}
const useListState = ({ externalCurrentPage, tabItems, defaultPageSize, onLoadData, tabStyle, isTreeTab }) => {
	const initialState = {
		activeTab: tabItems.length > 0 ? tabItems[0].key : "all",
		currentPage: 1,
		isStickyActive: false
	};
	const [state, dispatch] = (0, react.useReducer)(listStateReducer, initialState);
	const activeTabRef = (0, react.useRef)(initialState.activeTab);
	activeTabRef.current = state.activeTab;
	/** 判断当前 activeTab 的首层 key 是否仍存在于 tabItems 中 */
	const isActiveTabValid = (0, ahooks.useMemoizedFn)((items) => {
		const current = activeTabRef.current;
		const key = Array.isArray(current) ? current[0] : current;
		return !!key && items.some((tab) => tab.key == key);
	});
	/** 根据 tabItems 计算默认的 activeTab */
	const getDefaultTab = (0, ahooks.useMemoizedFn)((items, tree) => {
		var _items$;
		if (!tree) return ["0"];
		const defaultTab = [items[0].key];
		if (((_items$ = items[0]) === null || _items$ === void 0 || (_items$ = _items$.children) === null || _items$ === void 0 ? void 0 : _items$.length) > 0) defaultTab.push("0");
		return defaultTab;
	});
	(0, react.useEffect)(() => {
		if (!isTreeTab || tabStyle === "switch" && tabItems.length > 0) {
			if (isActiveTabValid(tabItems)) return;
			setActiveTab(getDefaultTab(tabItems, isTreeTab));
		}
	}, [tabItems, isTreeTab]);
	const actualCurrentPage = externalCurrentPage || state.currentPage;
	const setActiveTab = (0, react.useCallback)((tab) => {
		dispatch({
			type: "SET_ACTIVE_TAB",
			payload: tab
		});
	}, []);
	const setCurrentPage = (0, react.useCallback)((page) => {
		dispatch({
			type: "SET_CURRENT_PAGE",
			payload: page
		});
	}, []);
	const setStickyActive = (0, react.useCallback)((active) => {
		dispatch({
			type: "SET_STICKY_ACTIVE",
			payload: active
		});
	}, []);
	const resetPage = (0, react.useCallback)(() => {
		dispatch({ type: "RESET_PAGE" });
	}, []);
	const loadData = (0, react.useCallback)((params) => {
		onLoadData === null || onLoadData === void 0 || onLoadData(params);
	}, [onLoadData]);
	return {
		state,
		setActiveTab,
		setCurrentPage,
		setStickyActive,
		resetPage,
		handleTabChange: (0, react.useCallback)((key) => {
			setActiveTab(key);
			resetPage();
			loadData({
				trigger: "tabChange",
				currentPage: 1,
				pageSize: defaultPageSize,
				activeTab: key
			});
		}, [
			setActiveTab,
			resetPage,
			loadData,
			defaultPageSize
		]),
		handlePageChange: (0, react.useCallback)((page, size) => {
			const previousPage = actualCurrentPage;
			const pageSize = size || defaultPageSize;
			setCurrentPage(page);
			loadData({
				trigger: "pagination",
				currentPage: page,
				pageSize,
				activeTab: state.activeTab,
				previousPage
			});
		}, [
			actualCurrentPage,
			defaultPageSize,
			setCurrentPage,
			loadData,
			state.activeTab
		]),
		handleShowSizeChange: (0, react.useCallback)((current, size) => {
			const previousPageSize = defaultPageSize;
			setCurrentPage(current);
			loadData({
				trigger: "pageSize",
				currentPage: current,
				pageSize: size,
				activeTab: state.activeTab,
				previousPageSize
			});
		}, [
			defaultPageSize,
			setCurrentPage,
			loadData,
			state.activeTab
		]),
		handleLoadMore: (0, react.useCallback)(() => {
			const nextPage = actualCurrentPage + 1;
			setCurrentPage(nextPage);
			loadData({
				trigger: "loadMore",
				currentPage: nextPage,
				pageSize: defaultPageSize,
				activeTab: state.activeTab
			});
		}, [
			actualCurrentPage,
			setCurrentPage,
			loadData,
			defaultPageSize,
			state.activeTab
		]),
		handleViewLess: (0, react.useCallback)(() => {
			resetPage();
			loadData({
				trigger: "pagination",
				currentPage: 1,
				pageSize: defaultPageSize,
				activeTab: state.activeTab,
				previousPage: actualCurrentPage
			});
		}, [
			resetPage,
			loadData,
			defaultPageSize,
			state.activeTab,
			actualCurrentPage
		]),
		handleScrollLoad: (0, react.useCallback)(() => {
			const nextPage = actualCurrentPage + 1;
			setCurrentPage(nextPage);
			loadData({
				trigger: "scroll",
				currentPage: nextPage,
				pageSize: defaultPageSize,
				activeTab: state.activeTab
			});
		}, [
			actualCurrentPage,
			setCurrentPage,
			loadData,
			defaultPageSize,
			state.activeTab
		]),
		actualCurrentPage
	};
};
//#endregion
exports.useListState = useListState;
