var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellReservation/hooks/useReservationHudDrawer.tsx
var useReservationHudDrawer_exports = {};
__export(useReservationHudDrawer_exports, {
  useReservationHudDrawer: () => useReservationHudDrawer
});
module.exports = __toCommonJS(useReservationHudDrawer_exports);
var import_react = __toESM(require("react"));
var import_bookingToReservationTables = require("../data/bookingToReservationTables");
var import_hudDrawerLocalListQuery = require("../data/hudDrawerLocalListQuery");
var import_ReservationFloorMapHud = require("../floorMap/ReservationFloorMapHud");
var import_buildReservationMobileTableConfig = require("../mobileTable/buildReservationMobileTableConfig");
var import_ReservationHudMapItemsSync = require("../floorMap/ReservationHudMapItemsSync");
var import_reservationHudTableRows = require("../floorMap/reservationHudTableRows");
var import_reservationHudResourceRootBuckets = require("../floorMap/reservationHudResourceRootBuckets");
var import_ReservationHudDrawerQuickFilters = require("../components/ReservationHudDrawerQuickFilters");
var import_reservationConstants = require("../reservationConstants");
var import_reservationCards = require("../floorMap/reservationCards");
function useReservationHudDrawer({
  floorMapHudTableDrawer,
  dataSources,
  gridDataSourceKey,
  isInternalListData,
  floorDayBookingData,
  searchParamsProp,
  dataProp,
  loadingProp,
  bookingChildProps,
  resourceBoardData,
  phoneLayout,
  hideViewportHudStats = false
}) {
  const hudItemsRef = (0, import_react.useRef)([]);
  const [hudMapItems, setHudMapItems] = (0, import_react.useState)([]);
  const [hudDrawerOpen, setHudDrawerOpen] = (0, import_react.useState)(false);
  const hudDrawerRef = (0, import_react.useRef)(null);
  const [hudDrawerResourceIds, setHudDrawerResourceIds] = (0, import_react.useState)(null);
  const [hudDrawerLocalSearchParams, setHudDrawerLocalSearchParams] = (0, import_react.useState)({});
  const hudDrawerSearchParamsOpenSnapshotRef = (0, import_react.useRef)(
    {}
  );
  const [hudDrawerListPagination, setHudDrawerListPagination] = (0, import_react.useState)({
    pageNumber: 1,
    pageSize: import_reservationConstants.PISELL_RESERVATION_HUD_LOCAL_PAGE_SIZE
  });
  const showVenueHudDock = (0, import_react.useMemo)(
    () => floorMapHudTableDrawer && (hudDrawerOpen || (0, import_ReservationFloorMapHud.reservationFloorMapHudSeatStatsTotal)(hudMapItems, dataSources) > 0),
    [floorMapHudTableDrawer, hudDrawerOpen, hudMapItems, dataSources]
  );
  const handleOpenHudTableDrawer = (0, import_react.useCallback)(() => {
    const boundTableRows = (0, import_reservationHudTableRows.filterReservationRowsBoundOnCanvas)(
      hudItemsRef.current,
      dataSources,
      gridDataSourceKey
    );
    const resourceIdSet = new Set(
      boundTableRows.map((r) => String(r.id)).filter(Boolean)
    );
    const parentSnapshot = (0, import_hudDrawerLocalListQuery.cloneHudDrawerSearchParams)(
      isInternalListData ? floorDayBookingData.searchParams : searchParamsProp ?? {}
    );
    (0, import_hudDrawerLocalListQuery.stripHudDrawerBookingDateQuickFilter)(parentSnapshot);
    (0, import_hudDrawerLocalListQuery.stripHudDrawerBucketFilterDefaults)(parentSnapshot);
    hudDrawerSearchParamsOpenSnapshotRef.current = parentSnapshot;
    setHudDrawerLocalSearchParams(parentSnapshot);
    setHudDrawerResourceIds(resourceIdSet);
    setHudDrawerListPagination({
      pageNumber: 1,
      pageSize: import_reservationConstants.PISELL_RESERVATION_HUD_LOCAL_PAGE_SIZE
    });
    setHudDrawerOpen(true);
  }, [
    dataSources,
    gridDataSourceKey,
    isInternalListData,
    floorDayBookingData.searchParams,
    searchParamsProp
  ]);
  const closeHudTableDrawer = (0, import_react.useCallback)(() => {
    setHudDrawerOpen(false);
    setHudDrawerResourceIds(null);
    setHudDrawerLocalSearchParams({});
    setHudDrawerListPagination({
      pageNumber: 1,
      pageSize: import_reservationConstants.PISELL_RESERVATION_HUD_LOCAL_PAGE_SIZE
    });
  }, []);
  (0, import_react.useEffect)(() => {
    if (!floorMapHudTableDrawer || !hudDrawerOpen) return;
    const onKeyDown = (e) => {
      if (e.key === "Escape") closeHudTableDrawer();
    };
    window.addEventListener("keydown", onKeyDown);
    return () => window.removeEventListener("keydown", onKeyDown);
  }, [floorMapHudTableDrawer, hudDrawerOpen, closeHudTableDrawer]);
  const hudDrawerResourceRootBuckets = (0, import_react.useMemo)(
    () => (0, import_reservationHudResourceRootBuckets.buildResourceRootBuckets)(resourceBoardData.data),
    [resourceBoardData.data]
  );
  const hudDrawerDisplayRows = (0, import_react.useMemo)(() => {
    if (!floorMapHudTableDrawer || !hudDrawerOpen || !hudDrawerResourceIds || hudDrawerResourceIds.size === 0) {
      return [];
    }
    const source = isInternalListData ? floorDayBookingData.data : dataProp ?? [];
    const baseRows = (0, import_bookingToReservationTables.filterBookingsByResourceIds)(source, hudDrawerResourceIds);
    return (0, import_hudDrawerLocalListQuery.applyHudDrawerLocalListQuery)(baseRows, hudDrawerLocalSearchParams);
  }, [
    floorMapHudTableDrawer,
    hudDrawerOpen,
    hudDrawerResourceIds,
    isInternalListData,
    floorDayBookingData.data,
    dataProp,
    hudDrawerLocalSearchParams
  ]);
  const hudDrawerFilteredTotal = hudDrawerDisplayRows.length;
  const hudDrawerMaxPage = Math.max(
    1,
    Math.ceil(hudDrawerFilteredTotal / hudDrawerListPagination.pageSize) || 1
  );
  (0, import_react.useEffect)(() => {
    if (hudDrawerListPagination.pageNumber <= hudDrawerMaxPage) return;
    setHudDrawerListPagination((p) => ({ ...p, pageNumber: hudDrawerMaxPage }));
  }, [hudDrawerMaxPage, hudDrawerListPagination.pageNumber]);
  const hudDrawerPagedRows = (0, import_react.useMemo)(() => {
    const { pageNumber, pageSize } = hudDrawerListPagination;
    const safeSize = Math.max(1, pageSize);
    const end = Math.min(
      hudDrawerDisplayRows.length,
      pageNumber * safeSize
    );
    return hudDrawerDisplayRows.slice(0, end);
  }, [hudDrawerDisplayRows, hudDrawerListPagination]);
  const hudDrawerToolBar = (0, import_react.useMemo)(() => {
    const raw = bookingChildProps.toolBar;
    const base = raw != null && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
    return (0, import_buildReservationMobileTableConfig.buildReservationHudDrawerToolbarConfig)({
      baseToolBar: base,
      searchGetContainer: () => hudDrawerRef.current ?? document.body,
      quickFilterComponent: import_ReservationHudDrawerQuickFilters.ReservationHudDrawerQuickFilters
    });
  }, [bookingChildProps.toolBar]);
  const hudDrawerLoading = isInternalListData ? floorDayBookingData.loading : loadingProp ?? false;
  const hudDrawerBookingPagination = (0, import_react.useMemo)(
    () => ({
      pageNumber: hudDrawerListPagination.pageNumber,
      pageSize: hudDrawerListPagination.pageSize
    }),
    [hudDrawerListPagination.pageNumber, hudDrawerListPagination.pageSize]
  );
  const hudDrawerOnSearch = (0, import_react.useCallback)((params) => {
    setHudDrawerListPagination((p) => ({ ...p, pageNumber: 1 }));
    setHudDrawerLocalSearchParams(
      (prev) => (0, import_hudDrawerLocalListQuery.mergeHudDrawerSearchParams)(prev, params)
    );
  }, []);
  const handleHudDrawerFormRecordIdsChange = (0, import_react.useCallback)(
    (formRecordIds) => {
      hudDrawerOnSearch({
        filter: {
          values: {
            form_record_ids: formRecordIds
          }
        }
      });
    },
    [hudDrawerOnSearch]
  );
  const hudDrawerOnReset = (0, import_react.useCallback)(() => {
    setHudDrawerListPagination((p) => ({ ...p, pageNumber: 1 }));
    setHudDrawerLocalSearchParams(
      (0, import_hudDrawerLocalListQuery.cloneHudDrawerSearchParams)(hudDrawerSearchParamsOpenSnapshotRef.current)
    );
  }, []);
  const hudDrawerOnPageChange = (0, import_react.useCallback)(
    (pageNumber, pageSize) => {
      setHudDrawerListPagination({ pageNumber, pageSize });
    },
    []
  );
  const renderItemByKind = (0, import_react.useMemo)(
    () => (0, import_reservationCards.getReservationRenderItemByKind)(dataSources),
    [dataSources]
  );
  const reservationFloorMapViewportOverlay = (0, import_react.useCallback)(
    (args) => {
      const items = args.items;
      hudItemsRef.current = items;
      if (floorMapHudTableDrawer) {
        return /* @__PURE__ */ import_react.default.createElement(import_ReservationHudMapItemsSync.ReservationHudMapItemsSync, { items, setItems: setHudMapItems });
      }
      if (hideViewportHudStats) {
        return null;
      }
      return /* @__PURE__ */ import_react.default.createElement(
        import_ReservationFloorMapHud.ReservationFloorMapHud,
        {
          items,
          dataSources: args.dataSources,
          interactive: false,
          phoneLayout
        }
      );
    },
    [floorMapHudTableDrawer, phoneLayout, hideViewportHudStats]
  );
  return {
    hudItemsRef,
    hudMapItems,
    setHudMapItems,
    hudDrawerRef,
    hudDrawerOpen,
    showVenueHudDock,
    handleOpenHudTableDrawer,
    closeHudTableDrawer,
    hudDrawerResourceRootBuckets,
    hudDrawerPagedRows,
    hudDrawerFilteredTotal,
    hudDrawerLoading,
    hudDrawerBookingPagination,
    hudDrawerOnSearch,
    hudDrawerOnReset,
    hudDrawerOnPageChange,
    hudDrawerToolBar,
    hudDrawerLocalSearchParams,
    handleHudDrawerFormRecordIdsChange,
    reservationFloorMapViewportOverlay,
    renderItemByKind
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationHudDrawer
});
