const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_bookingToReservationTables = require("../data/bookingToReservationTables.js");
const require_reservationCards = require("../floorMap/reservationCards.js");
const require_buildReservationMobileTableConfig = require("../mobileTable/buildReservationMobileTableConfig.js");
const require_hudDrawerLocalListQuery = require("../data/hudDrawerLocalListQuery.js");
const require_ReservationFloorMapHud = require("../floorMap/ReservationFloorMapHud.js");
const require_ReservationHudMapItemsSync = require("../floorMap/ReservationHudMapItemsSync.js");
const require_reservationHudTableRows = require("../floorMap/reservationHudTableRows.js");
const require_reservationHudResourceRootBuckets = require("../floorMap/reservationHudResourceRootBuckets.js");
const require_ReservationHudDrawerQuickFilters = require("../components/ReservationHudDrawerQuickFilters.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/plus/pisellReservation/hooks/useReservationHudDrawer.tsx
/**
* 平面图 HUD 抽屉：视口 items 同步、本地列表分页与筛参、展开/收起、ESC、内嵌 RecordBoard toolbar 覆写。
*/
function useReservationHudDrawer({ floorMapHudTableDrawer, dataSources, gridDataSourceKey, isInternalListData, floorDayBookingData, searchParamsProp, dataProp, loadingProp, bookingChildProps, resourceBoardData, phoneLayout, hideViewportHudStats = false }) {
	const hudItemsRef = (0, react.useRef)([]);
	const [hudMapItems, setHudMapItems] = (0, react.useState)([]);
	const [hudDrawerOpen, setHudDrawerOpen] = (0, react.useState)(false);
	const hudDrawerRef = (0, react.useRef)(null);
	const [hudDrawerResourceIds, setHudDrawerResourceIds] = (0, react.useState)(null);
	const [hudDrawerLocalSearchParams, setHudDrawerLocalSearchParams] = (0, react.useState)({});
	const hudDrawerSearchParamsOpenSnapshotRef = (0, react.useRef)({});
	const [hudDrawerListPagination, setHudDrawerListPagination] = (0, react.useState)({
		pageNumber: 1,
		pageSize: 50
	});
	const showVenueHudDock = (0, react.useMemo)(() => floorMapHudTableDrawer && (hudDrawerOpen || require_ReservationFloorMapHud.reservationFloorMapHudSeatStatsTotal(hudMapItems, dataSources) > 0), [
		floorMapHudTableDrawer,
		hudDrawerOpen,
		hudMapItems,
		dataSources
	]);
	const handleOpenHudTableDrawer = (0, react.useCallback)(() => {
		const boundTableRows = require_reservationHudTableRows.filterReservationRowsBoundOnCanvas(hudItemsRef.current, dataSources, gridDataSourceKey);
		const resourceIdSet = new Set(boundTableRows.map((r) => String(r.id)).filter(Boolean));
		const parentSnapshot = require_hudDrawerLocalListQuery.cloneHudDrawerSearchParams(isInternalListData ? floorDayBookingData.searchParams : searchParamsProp !== null && searchParamsProp !== void 0 ? searchParamsProp : {});
		require_hudDrawerLocalListQuery.stripHudDrawerBookingDateQuickFilter(parentSnapshot);
		require_hudDrawerLocalListQuery.stripHudDrawerBucketFilterDefaults(parentSnapshot);
		hudDrawerSearchParamsOpenSnapshotRef.current = parentSnapshot;
		setHudDrawerLocalSearchParams(parentSnapshot);
		setHudDrawerResourceIds(resourceIdSet);
		setHudDrawerListPagination({
			pageNumber: 1,
			pageSize: 50
		});
		setHudDrawerOpen(true);
	}, [
		dataSources,
		gridDataSourceKey,
		isInternalListData,
		floorDayBookingData.searchParams,
		searchParamsProp
	]);
	const closeHudTableDrawer = (0, react.useCallback)(() => {
		setHudDrawerOpen(false);
		setHudDrawerResourceIds(null);
		setHudDrawerLocalSearchParams({});
		setHudDrawerListPagination({
			pageNumber: 1,
			pageSize: 50
		});
	}, []);
	(0, 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, react.useMemo)(() => require_reservationHudResourceRootBuckets.buildResourceRootBuckets(resourceBoardData.data), [resourceBoardData.data]);
	const hudDrawerDisplayRows = (0, react.useMemo)(() => {
		if (!floorMapHudTableDrawer || !hudDrawerOpen || !hudDrawerResourceIds || hudDrawerResourceIds.size === 0) return [];
		return require_hudDrawerLocalListQuery.applyHudDrawerLocalListQuery(require_bookingToReservationTables.filterBookingsByResourceIds(isInternalListData ? floorDayBookingData.data : dataProp !== null && dataProp !== void 0 ? dataProp : [], hudDrawerResourceIds), 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, react.useEffect)(() => {
		if (hudDrawerListPagination.pageNumber <= hudDrawerMaxPage) return;
		setHudDrawerListPagination((p) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, p), {}, { pageNumber: hudDrawerMaxPage }));
	}, [hudDrawerMaxPage, hudDrawerListPagination.pageNumber]);
	/** 与 RecordBoard `grid.infiniteScroll` 一致：按页数累积展示行（触底增加 pageNumber 后多展一段） */
	const hudDrawerPagedRows = (0, 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, react.useMemo)(() => {
		const raw = bookingChildProps.toolBar;
		return require_buildReservationMobileTableConfig.buildReservationHudDrawerToolbarConfig({
			baseToolBar: raw != null && typeof raw === "object" && !Array.isArray(raw) ? raw : {},
			searchGetContainer: () => {
				var _hudDrawerRef$current;
				return (_hudDrawerRef$current = hudDrawerRef.current) !== null && _hudDrawerRef$current !== void 0 ? _hudDrawerRef$current : document.body;
			},
			quickFilterComponent: require_ReservationHudDrawerQuickFilters.default
		});
	}, [bookingChildProps.toolBar]);
	const hudDrawerLoading = isInternalListData ? floorDayBookingData.loading : loadingProp !== null && loadingProp !== void 0 ? loadingProp : false;
	const hudDrawerBookingPagination = (0, react.useMemo)(() => ({
		pageNumber: hudDrawerListPagination.pageNumber,
		pageSize: hudDrawerListPagination.pageSize
	}), [hudDrawerListPagination.pageNumber, hudDrawerListPagination.pageSize]);
	const hudDrawerOnSearch = (0, react.useCallback)((params) => {
		setHudDrawerListPagination((p) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, p), {}, { pageNumber: 1 }));
		setHudDrawerLocalSearchParams((prev) => require_hudDrawerLocalListQuery.mergeHudDrawerSearchParams(prev, params));
	}, []);
	const handleHudDrawerFormRecordIdsChange = (0, react.useCallback)((formRecordIds) => {
		hudDrawerOnSearch({ filter: { values: { form_record_ids: formRecordIds } } });
	}, [hudDrawerOnSearch]);
	const hudDrawerOnReset = (0, react.useCallback)(() => {
		setHudDrawerListPagination((p) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, p), {}, { pageNumber: 1 }));
		setHudDrawerLocalSearchParams(require_hudDrawerLocalListQuery.cloneHudDrawerSearchParams(hudDrawerSearchParamsOpenSnapshotRef.current));
	}, []);
	const hudDrawerOnPageChange = (0, react.useCallback)((pageNumber, pageSize) => {
		setHudDrawerListPagination({
			pageNumber,
			pageSize
		});
	}, []);
	const renderItemByKind = (0, react.useMemo)(() => require_reservationCards.getReservationRenderItemByKind(dataSources), [dataSources]);
	return {
		hudItemsRef,
		hudMapItems,
		setHudMapItems,
		hudDrawerRef,
		hudDrawerOpen,
		showVenueHudDock,
		handleOpenHudTableDrawer,
		closeHudTableDrawer,
		hudDrawerResourceRootBuckets,
		hudDrawerPagedRows,
		hudDrawerFilteredTotal,
		hudDrawerLoading,
		hudDrawerBookingPagination,
		hudDrawerOnSearch,
		hudDrawerOnReset,
		hudDrawerOnPageChange,
		hudDrawerToolBar,
		hudDrawerLocalSearchParams,
		handleHudDrawerFormRecordIdsChange,
		reservationFloorMapViewportOverlay: (0, react.useCallback)((args) => {
			const items = args.items;
			hudItemsRef.current = items;
			if (floorMapHudTableDrawer) return /* @__PURE__ */ react.default.createElement(require_ReservationHudMapItemsSync.ReservationHudMapItemsSync, {
				items,
				setItems: setHudMapItems
			});
			if (hideViewportHudStats) return null;
			return /* @__PURE__ */ react.default.createElement(require_ReservationFloorMapHud.ReservationFloorMapHud, {
				items,
				dataSources: args.dataSources,
				interactive: false,
				phoneLayout
			});
		}, [
			floorMapHudTableDrawer,
			phoneLayout,
			hideViewportHudStats
		]),
		renderItemByKind
	};
}
//#endregion
exports.useReservationHudDrawer = useReservationHudDrawer;
