import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { I18N_RESOURCE_WALL_RESOURCES } from "../reservationConstants.js";
import { PisellReservationScheduleEndActions, PisellReservationToolbarTabs, ReservationDefaultScheduleStart, ReservationGridTopRight, ReservationMobileToolbarBodyViewSelect } from "../components/PisellReservationSubcomponents.js";
import { computeReservationDensityBuckets, fitTimelineHighlightsToSlotCount, reservationTimelineAxisSlotCount } from "../data/reservationDensity.js";
import { ReservationResourceWallResourceSelect } from "../components/ReservationResourceWallResourceSelect.js";
import { adaptFilterConfigForMobile, adaptQuickFiltersForMobileToolbar, buildReservationMobileTableToolbarConfig, buildReservationPhoneNarrowStackToolbarConfig } from "../mobileTable/buildReservationMobileTableConfig.js";
import React, { useCallback, useMemo } from "react";
import { locales } from "@pisell/utils";
import { DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY } from "@pisell/materials";
//#region src/plus/pisellReservation/hooks/useReservationToolBarMerged.tsx
/**
* RecordBoard 顶栏工具栏：合并 booking 透视 toolBar、资源墙快筛注入、日程带与密度加载。
*/
const _excluded = [
	"tabs",
	"topLeft",
	"topRight",
	"bottomLeft",
	"quickFilter"
];
function useReservationToolBarMerged(p) {
	const { toolBarProp, bookingToolBar, isInternalListData, tables, schedule, handleScheduleChange, scheduleStartSlotProp, scheduleEndSlotProp, schedulePropsProp, mergedTimeNavigatorProps, followWallClock, onFollowWallClockToggle, onTimeNavigatorUserInteraction, calendarGranularity, setCalendarGranularity, onNewReservation, salesTimelineHighlights, floorDayBookingData, data, effectiveTimelineRange, bodyView, phoneLayout } = p;
	const defaultScheduleEnd = useMemo(() => /* @__PURE__ */ React.createElement(PisellReservationScheduleEndActions, {
		onNewReservation,
		calendarGranularity,
		onCalendarGranularityChange: setCalendarGranularity,
		phoneLayout
	}), [
		onNewReservation,
		calendarGranularity,
		setCalendarGranularity,
		phoneLayout
	]);
	const effectiveScheduleEndSlot = useMemo(() => {
		const slotNode = scheduleEndSlotProp !== null && scheduleEndSlotProp !== void 0 ? scheduleEndSlotProp : defaultScheduleEnd;
		return /* @__PURE__ */ React.createElement("div", { onClick: (event) => {
			const target = event.target;
			if (!(target === null || target === void 0 ? void 0 : target.closest(".pisell-reservation-schedule-end-slots"))) return;
			event.stopPropagation();
		} }, slotNode);
	}, [scheduleEndSlotProp, defaultScheduleEnd]);
	const resourceWallFilterSelectOptions = useMemo(() => {
		return tables.map((row) => {
			const id = String(row.id);
			const r = row;
			const code = r.code != null && String(r.code).trim() !== "" ? String(r.code) : "";
			const name = r.name != null && String(r.name).trim() !== "" ? String(r.name) : "";
			return {
				value: id,
				label: code || name || id
			};
		});
	}, [tables]);
	const bookingQuickFilterWithResourceWall = useMemo(() => {
		const base = bookingToolBar === null || bookingToolBar === void 0 ? void 0 : bookingToolBar.quickFilter;
		if (base === false || base == null) return base;
		if (!isInternalListData) return base;
		const rawList = base.filterList;
		const filterList = Array.isArray(rawList) ? [...rawList] : [];
		if (filterList.some((i) => {
			var _ref, _key;
			return String((_ref = (_key = i.key) !== null && _key !== void 0 ? _key : i.name) !== null && _ref !== void 0 ? _ref : "") === DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY;
		})) return base;
		return _objectSpread2(_objectSpread2({}, typeof base === "object" && base !== null ? base : {}), {}, { filterList: [...filterList, {
			type: "custom",
			key: DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY,
			name: DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY,
			label: locales.getText(I18N_RESOURCE_WALL_RESOURCES),
			other: {
				component: ReservationResourceWallResourceSelect,
				options: resourceWallFilterSelectOptions
			}
		}] });
	}, [
		bookingToolBar,
		isInternalListData,
		resourceWallFilterSelectOptions
	]);
	const loadReservationDensity = useCallback(function() {
		var _ref2 = _asyncToGenerator(function* ({ referenceDate, stepMinutes }) {
			const slotCount = reservationTimelineAxisSlotCount(referenceDate, stepMinutes, effectiveTimelineRange);
			if (salesTimelineHighlights != null && salesTimelineHighlights.length > 0) return fitTimelineHighlightsToSlotCount(salesTimelineHighlights, slotCount);
			return computeReservationDensityBuckets(isInternalListData ? floorDayBookingData : data, referenceDate, stepMinutes);
		});
		return function(_x) {
			return _ref2.apply(this, arguments);
		};
	}(), [
		isInternalListData,
		floorDayBookingData,
		data,
		salesTimelineHighlights,
		effectiveTimelineRange.start,
		effectiveTimelineRange.end
	]);
	return { toolBarMerged: useMemo(() => {
		const _ref3 = toolBarProp !== null && toolBarProp !== void 0 ? toolBarProp : {}, { tabs: extraTabs, topLeft: topLeftProp, topRight: topRightProp, bottomLeft: bottomLeftProp, quickFilter: toolBarQuickFilterOverride } = _ref3, restToolbar = _objectWithoutProperties(_ref3, _excluded);
		const booking = bookingToolBar !== null && bookingToolBar !== void 0 ? bookingToolBar : {};
		const effectiveQuickFilter = toolBarQuickFilterOverride !== void 0 ? toolBarQuickFilterOverride : bookingQuickFilterWithResourceWall;
		const baseToolbar = _objectSpread2(_objectSpread2({}, booking), restToolbar);
		const mobileToolbar = phoneLayout ? bodyView === "grid" || bodyView === "resourceWall" ? buildReservationMobileTableToolbarConfig({
			baseToolBar: baseToolbar,
			quickFilterFilterList: effectiveQuickFilter && typeof effectiveQuickFilter === "object" && Array.isArray(effectiveQuickFilter.filterList) ? effectiveQuickFilter.filterList : void 0
		}) : buildReservationPhoneNarrowStackToolbarConfig(baseToolbar) : baseToolbar;
		const phoneQuickFilter = phoneLayout && effectiveQuickFilter && typeof effectiveQuickFilter === "object" && Array.isArray(effectiveQuickFilter.filterList) ? _objectSpread2(_objectSpread2({}, effectiveQuickFilter), {}, {
			size: "small",
			filterList: adaptQuickFiltersForMobileToolbar(effectiveQuickFilter.filterList)
		}) : effectiveQuickFilter;
		const mergedFilter = mobileToolbar.filter;
		const phoneFilter = phoneLayout && mergedFilter && typeof mergedFilter === "object" ? adaptFilterConfigForMobile(mergedFilter) : mergedFilter;
		return _objectSpread2(_objectSpread2(_objectSpread2({}, mobileToolbar), phoneFilter ? { filter: phoneFilter } : {}), {}, {
			quickFilter: phoneQuickFilter,
			hideBodyViewToggle: phoneLayout && (bodyView === "grid" || bodyView === "resourceWall"),
			tabs: phoneLayout && (bodyView === "grid" || bodyView === "resourceWall") ? null : /* @__PURE__ */ React.createElement(PisellReservationToolbarTabs, {
				extraTabs,
				schedule,
				onScheduleChange: handleScheduleChange,
				scheduleProps: schedulePropsProp,
				timeNavigatorProps: mergedTimeNavigatorProps,
				scheduleStartSlot: scheduleStartSlotProp !== null && scheduleStartSlotProp !== void 0 ? scheduleStartSlotProp : phoneLayout ? null : /* @__PURE__ */ React.createElement(ReservationDefaultScheduleStart, null),
				scheduleEndSlot: effectiveScheduleEndSlot,
				followWallClock,
				onFollowWallClockToggle,
				onTimeNavigatorUserInteraction,
				loadDensity: loadReservationDensity,
				calendarGranularity,
				phoneLayout
			}),
			topLeft: /* @__PURE__ */ React.createElement(React.Fragment, null, phoneLayout ? /* @__PURE__ */ React.createElement(ReservationMobileToolbarBodyViewSelect, null) : null, topLeftProp),
			topRight: /* @__PURE__ */ React.createElement(ReservationGridTopRight, { extra: topRightProp }),
			bottomLeft: bottomLeftProp !== null && bottomLeftProp !== void 0 ? bottomLeftProp : null
		});
	}, [
		bookingToolBar,
		bookingQuickFilterWithResourceWall,
		toolBarProp,
		schedule,
		handleScheduleChange,
		scheduleStartSlotProp,
		effectiveScheduleEndSlot,
		followWallClock,
		onFollowWallClockToggle,
		onTimeNavigatorUserInteraction,
		mergedTimeNavigatorProps,
		schedulePropsProp,
		loadReservationDensity,
		calendarGranularity,
		phoneLayout,
		bodyView
	]) };
}
//#endregion
export { useReservationToolBarMerged };
