var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/list/hooks/useListState.ts
var useListState_exports = {};
__export(useListState_exports, {
  useListState: () => useListState
});
module.exports = __toCommonJS(useListState_exports);
var import_react = require("react");
function listStateReducer(state, action) {
  switch (action.type) {
    case "SET_ACTIVE_TAB":
      return { ...state, activeTab: action.payload };
    case "SET_CURRENT_PAGE":
      return { ...state, currentPage: action.payload };
    case "SET_STICKY_ACTIVE":
      return { ...state, isStickyActive: action.payload };
    case "RESET_PAGE":
      return { ...state, currentPage: 1 };
    default:
      return state;
  }
}
var useListState = ({
  externalCurrentPage,
  tabItems,
  defaultPageSize,
  onLoadData
}) => {
  const initialState = {
    activeTab: tabItems.length > 0 ? tabItems[0].key : "all",
    currentPage: 1,
    isStickyActive: false
  };
  const [state, dispatch] = (0, import_react.useReducer)(listStateReducer, initialState);
  const actualCurrentPage = externalCurrentPage || state.currentPage;
  const setActiveTab = (0, import_react.useCallback)((tab) => {
    dispatch({ type: "SET_ACTIVE_TAB", payload: tab });
  }, []);
  const setCurrentPage = (0, import_react.useCallback)((page) => {
    dispatch({ type: "SET_CURRENT_PAGE", payload: page });
  }, []);
  const setStickyActive = (0, import_react.useCallback)((active) => {
    dispatch({ type: "SET_STICKY_ACTIVE", payload: active });
  }, []);
  const resetPage = (0, import_react.useCallback)(() => {
    dispatch({ type: "RESET_PAGE" });
  }, []);
  const loadData = (0, import_react.useCallback)((params) => {
    onLoadData == null ? void 0 : onLoadData(params);
  }, [onLoadData]);
  const handleTabChange = (0, import_react.useCallback)((key) => {
    setActiveTab(key);
    resetPage();
    loadData({
      trigger: "tabChange",
      currentPage: 1,
      pageSize: defaultPageSize,
      activeTab: key
    });
  }, [setActiveTab, resetPage, loadData, defaultPageSize]);
  const handlePageChange = (0, import_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]);
  const handleShowSizeChange = (0, import_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]);
  const handleLoadMore = (0, import_react.useCallback)(() => {
    const nextPage = actualCurrentPage + 1;
    setCurrentPage(nextPage);
    loadData({
      trigger: "loadMore",
      currentPage: nextPage,
      pageSize: defaultPageSize,
      activeTab: state.activeTab
    });
  }, [actualCurrentPage, setCurrentPage, loadData, defaultPageSize, state.activeTab]);
  const handleViewLess = (0, import_react.useCallback)(() => {
    resetPage();
    loadData({
      trigger: "pagination",
      currentPage: 1,
      pageSize: defaultPageSize,
      activeTab: state.activeTab,
      previousPage: actualCurrentPage
    });
  }, [resetPage, loadData, defaultPageSize, state.activeTab, actualCurrentPage]);
  const handleScrollLoad = (0, import_react.useCallback)(() => {
    const nextPage = actualCurrentPage + 1;
    setCurrentPage(nextPage);
    loadData({
      trigger: "scroll",
      currentPage: nextPage,
      pageSize: defaultPageSize,
      activeTab: state.activeTab
    });
  }, [actualCurrentPage, setCurrentPage, loadData, defaultPageSize, state.activeTab]);
  return {
    state,
    setActiveTab,
    setCurrentPage,
    setStickyActive,
    resetPage,
    handleTabChange,
    handlePageChange,
    handleShowSizeChange,
    handleLoadMore,
    handleViewLess,
    handleScrollLoad,
    actualCurrentPage
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useListState
});
