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/plus/pisellReservation/hooks/useReservationPhoneGridInfiniteData.ts
var useReservationPhoneGridInfiniteData_exports = {};
__export(useReservationPhoneGridInfiniteData_exports, {
  useReservationPhoneGridInfiniteData: () => useReservationPhoneGridInfiniteData
});
module.exports = __toCommonJS(useReservationPhoneGridInfiniteData_exports);
var import_react = require("react");
var import_phoneGridInfiniteDataUtils = require("./phoneGridInfiniteDataUtils");
function useReservationPhoneGridInfiniteData(options) {
  const {
    enabled,
    isInternalListData,
    rowKey,
    data,
    pagination,
    searchParams
  } = options;
  const [accumulatedRows, setAccumulatedRows] = (0, import_react.useState)([]);
  const lastSearchParamsRef = (0, import_react.useRef)(searchParams);
  const lastAppliedPageRef = (0, import_react.useRef)(0);
  (0, import_react.useEffect)(() => {
    if (!enabled || !isInternalListData) {
      setAccumulatedRows([]);
      lastAppliedPageRef.current = 0;
      return;
    }
    const searchChanged = lastSearchParamsRef.current !== searchParams;
    if (searchChanged) {
      lastSearchParamsRef.current = searchParams;
      lastAppliedPageRef.current = 0;
    }
    const pageNumber = Math.max(1, pagination.pageNumber);
    if (pageNumber <= 1 || searchChanged) {
      setAccumulatedRows(data);
      lastAppliedPageRef.current = 1;
      return;
    }
    if (pageNumber <= lastAppliedPageRef.current) {
      return;
    }
    setAccumulatedRows(
      (prev) => (0, import_phoneGridInfiniteDataUtils.mergePhoneGridRowsByRowKey)(prev, data, rowKey)
    );
    lastAppliedPageRef.current = pageNumber;
  }, [
    enabled,
    isInternalListData,
    data,
    pagination.pageNumber,
    searchParams,
    rowKey
  ]);
  return (0, import_react.useMemo)(() => {
    if (!enabled) return data;
    if (isInternalListData) return accumulatedRows;
    const safeSize = Math.max(1, pagination.pageSize);
    const end = Math.min(data.length, pagination.pageNumber * safeSize);
    return data.slice(0, end);
  }, [
    enabled,
    isInternalListData,
    accumulatedRows,
    data,
    pagination.pageNumber,
    pagination.pageSize
  ]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationPhoneGridInfiniteData
});
