import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { filterBookingsByResourceIds } from "../data/bookingToReservationTables.js";
import { getReservationRenderItemByKind } from "../floorMap/reservationCards.js";
import { buildReservationHudDrawerToolbarConfig } from "../mobileTable/buildReservationMobileTableConfig.js";
import { applyHudDrawerLocalListQuery, cloneHudDrawerSearchParams, mergeHudDrawerSearchParams, stripHudDrawerBookingDateQuickFilter, stripHudDrawerBucketFilterDefaults } from "../data/hudDrawerLocalListQuery.js";
import { ReservationFloorMapHud, reservationFloorMapHudSeatStatsTotal } from "../floorMap/ReservationFloorMapHud.js";
import { ReservationHudMapItemsSync } from "../floorMap/ReservationHudMapItemsSync.js";
import { filterReservationRowsBoundOnCanvas } from "../floorMap/reservationHudTableRows.js";
import { buildResourceRootBuckets } from "../floorMap/reservationHudResourceRootBuckets.js";
import ReservationHudDrawerQuickFilters from "../components/ReservationHudDrawerQuickFilters.js";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "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 = useRef([]);
	const [hudMapItems, setHudMapItems] = useState([]);
	const [hudDrawerOpen, setHudDrawerOpen] = useState(false);
	const hudDrawerRef = useRef(null);
	const [hudDrawerResourceIds, setHudDrawerResourceIds] = useState(null);
	const [hudDrawerLocalSearchParams, setHudDrawerLocalSearchParams] = useState({});
	const hudDrawerSearchParamsOpenSnapshotRef = useRef({});
	const [hudDrawerListPagination, setHudDrawerListPagination] = useState({
		pageNumber: 1,
		pageSize: 50
	});
	const showVenueHudDock = useMemo(() => floorMapHudTableDrawer && (hudDrawerOpen || reservationFloorMapHudSeatStatsTotal(hudMapItems, dataSources) > 0), [
		floorMapHudTableDrawer,
		hudDrawerOpen,
		hudMapItems,
		dataSources
	]);
	const handleOpenHudTableDrawer = useCallback(() => {
		const boundTableRows = filterReservationRowsBoundOnCanvas(hudItemsRef.current, dataSources, gridDataSourceKey);
		const resourceIdSet = new Set(boundTableRows.map((r) => String(r.id)).filter(Boolean));
		const parentSnapshot = cloneHudDrawerSearchParams(isInternalListData ? floorDayBookingData.searchParams : searchParamsProp !== null && searchParamsProp !== void 0 ? searchParamsProp : {});
		stripHudDrawerBookingDateQuickFilter(parentSnapshot);
		stripHudDrawerBucketFilterDefaults(parentSnapshot);
		hudDrawerSearchParamsOpenSnapshotRef.current = parentSnapshot;
		setHudDrawerLocalSearchParams(parentSnapshot);
		setHudDrawerResourceIds(resourceIdSet);
		setHudDrawerListPagination({
			pageNumber: 1,
			pageSize: 50
		});
		setHudDrawerOpen(true);
	}, [
		dataSources,
		gridDataSourceKey,
		isInternalListData,
		floorDayBookingData.searchParams,
		searchParamsProp
	]);
	const closeHudTableDrawer = useCallback(() => {
		setHudDrawerOpen(false);
		setHudDrawerResourceIds(null);
		setHudDrawerLocalSearchParams({});
		setHudDrawerListPagination({
			pageNumber: 1,
			pageSize: 50
		});
	}, []);
	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 = useMemo(() => buildResourceRootBuckets(resourceBoardData.data), [resourceBoardData.data]);
	const hudDrawerDisplayRows = useMemo(() => {
		if (!floorMapHudTableDrawer || !hudDrawerOpen || !hudDrawerResourceIds || hudDrawerResourceIds.size === 0) return [];
		return applyHudDrawerLocalListQuery(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);
	useEffect(() => {
		if (hudDrawerListPagination.pageNumber <= hudDrawerMaxPage) return;
		setHudDrawerListPagination((p) => _objectSpread2(_objectSpread2({}, p), {}, { pageNumber: hudDrawerMaxPage }));
	}, [hudDrawerMaxPage, hudDrawerListPagination.pageNumber]);
	/** 与 RecordBoard `grid.infiniteScroll` 一致：按页数累积展示行（触底增加 pageNumber 后多展一段） */
	const hudDrawerPagedRows = 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 = useMemo(() => {
		const raw = bookingChildProps.toolBar;
		return 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: ReservationHudDrawerQuickFilters
		});
	}, [bookingChildProps.toolBar]);
	const hudDrawerLoading = isInternalListData ? floorDayBookingData.loading : loadingProp !== null && loadingProp !== void 0 ? loadingProp : false;
	const hudDrawerBookingPagination = useMemo(() => ({
		pageNumber: hudDrawerListPagination.pageNumber,
		pageSize: hudDrawerListPagination.pageSize
	}), [hudDrawerListPagination.pageNumber, hudDrawerListPagination.pageSize]);
	const hudDrawerOnSearch = useCallback((params) => {
		setHudDrawerListPagination((p) => _objectSpread2(_objectSpread2({}, p), {}, { pageNumber: 1 }));
		setHudDrawerLocalSearchParams((prev) => mergeHudDrawerSearchParams(prev, params));
	}, []);
	const handleHudDrawerFormRecordIdsChange = useCallback((formRecordIds) => {
		hudDrawerOnSearch({ filter: { values: { form_record_ids: formRecordIds } } });
	}, [hudDrawerOnSearch]);
	const hudDrawerOnReset = useCallback(() => {
		setHudDrawerListPagination((p) => _objectSpread2(_objectSpread2({}, p), {}, { pageNumber: 1 }));
		setHudDrawerLocalSearchParams(cloneHudDrawerSearchParams(hudDrawerSearchParamsOpenSnapshotRef.current));
	}, []);
	const hudDrawerOnPageChange = useCallback((pageNumber, pageSize) => {
		setHudDrawerListPagination({
			pageNumber,
			pageSize
		});
	}, []);
	const renderItemByKind = useMemo(() => getReservationRenderItemByKind(dataSources), [dataSources]);
	return {
		hudItemsRef,
		hudMapItems,
		setHudMapItems,
		hudDrawerRef,
		hudDrawerOpen,
		showVenueHudDock,
		handleOpenHudTableDrawer,
		closeHudTableDrawer,
		hudDrawerResourceRootBuckets,
		hudDrawerPagedRows,
		hudDrawerFilteredTotal,
		hudDrawerLoading,
		hudDrawerBookingPagination,
		hudDrawerOnSearch,
		hudDrawerOnReset,
		hudDrawerOnPageChange,
		hudDrawerToolBar,
		hudDrawerLocalSearchParams,
		handleHudDrawerFormRecordIdsChange,
		reservationFloorMapViewportOverlay: useCallback((args) => {
			const items = args.items;
			hudItemsRef.current = items;
			if (floorMapHudTableDrawer) return /* @__PURE__ */ React.createElement(ReservationHudMapItemsSync, {
				items,
				setItems: setHudMapItems
			});
			if (hideViewportHudStats) return null;
			return /* @__PURE__ */ React.createElement(ReservationFloorMapHud, {
				items,
				dataSources: args.dataSources,
				interactive: false,
				phoneLayout
			});
		}, [
			floorMapHudTableDrawer,
			phoneLayout,
			hideViewportHudStats
		]),
		renderItemByKind
	};
}
//#endregion
export { useReservationHudDrawer };
