import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useCallback, useEffect, useReducer } from "react";
//#region src/components/list/hooks/useListState.ts
function listStateReducer(state, action) {
	switch (action.type) {
		case "SET_ACTIVE_TAB": return _objectSpread2(_objectSpread2({}, state), {}, { activeTab: action.payload });
		case "SET_CURRENT_PAGE": return _objectSpread2(_objectSpread2({}, state), {}, { currentPage: action.payload });
		case "SET_STICKY_ACTIVE": return _objectSpread2(_objectSpread2({}, state), {}, { isStickyActive: action.payload });
		case "RESET_PAGE": return _objectSpread2(_objectSpread2({}, state), {}, { currentPage: 1 });
		default: return state;
	}
}
const useListState = ({ externalCurrentPage, tabItems, defaultPageSize, onLoadData, tabStyle }) => {
	const [state, dispatch] = useReducer(listStateReducer, {
		activeTab: tabItems.length > 0 ? tabItems[0].key : "all",
		currentPage: 1,
		isStickyActive: false
	});
	useEffect(() => {
		if (tabStyle === "switch" && tabItems.length > 0) {
			var _tabItems$;
			const defaultTab = [tabItems[0].key];
			if (((_tabItems$ = tabItems[0]) === null || _tabItems$ === void 0 || (_tabItems$ = _tabItems$.children) === null || _tabItems$ === void 0 ? void 0 : _tabItems$.length) > 0) defaultTab.push("0");
			setActiveTab(defaultTab);
		}
	}, [tabItems]);
	const actualCurrentPage = externalCurrentPage || state.currentPage;
	const setActiveTab = useCallback((tab) => {
		dispatch({
			type: "SET_ACTIVE_TAB",
			payload: tab
		});
	}, []);
	const setCurrentPage = useCallback((page) => {
		dispatch({
			type: "SET_CURRENT_PAGE",
			payload: page
		});
	}, []);
	const setStickyActive = useCallback((active) => {
		dispatch({
			type: "SET_STICKY_ACTIVE",
			payload: active
		});
	}, []);
	const resetPage = useCallback(() => {
		dispatch({ type: "RESET_PAGE" });
	}, []);
	const loadData = useCallback((params) => {
		onLoadData === null || onLoadData === void 0 || onLoadData(params);
	}, [onLoadData]);
	return {
		state,
		setActiveTab,
		setCurrentPage,
		setStickyActive,
		resetPage,
		handleTabChange: useCallback((key) => {
			setActiveTab(key);
			resetPage();
			loadData({
				trigger: "tabChange",
				currentPage: 1,
				pageSize: defaultPageSize,
				activeTab: key
			});
		}, [
			setActiveTab,
			resetPage,
			loadData,
			defaultPageSize
		]),
		handlePageChange: 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: 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: useCallback(() => {
			const nextPage = actualCurrentPage + 1;
			setCurrentPage(nextPage);
			loadData({
				trigger: "loadMore",
				currentPage: nextPage,
				pageSize: defaultPageSize,
				activeTab: state.activeTab
			});
		}, [
			actualCurrentPage,
			setCurrentPage,
			loadData,
			defaultPageSize,
			state.activeTab
		]),
		handleViewLess: useCallback(() => {
			resetPage();
			loadData({
				trigger: "pagination",
				currentPage: 1,
				pageSize: defaultPageSize,
				activeTab: state.activeTab,
				previousPage: actualCurrentPage
			});
		}, [
			resetPage,
			loadData,
			defaultPageSize,
			state.activeTab,
			actualCurrentPage
		]),
		handleScrollLoad: 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
export { useListState };
