const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_reservationConstants = require("../reservationConstants.js");
const require_PisellReservationSubcomponents = require("../components/PisellReservationSubcomponents.js");
const require_reservationDensity = require("../data/reservationDensity.js");
const require_ReservationResourceWallResourceSelect = require("../components/ReservationResourceWallResourceSelect.js");
const require_buildReservationMobileTableConfig = require("../mobileTable/buildReservationMobileTableConfig.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@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 = (0, react.useMemo)(() => /* @__PURE__ */ react.default.createElement(require_PisellReservationSubcomponents.PisellReservationScheduleEndActions, {
		onNewReservation,
		calendarGranularity,
		onCalendarGranularityChange: setCalendarGranularity,
		phoneLayout
	}), [
		onNewReservation,
		calendarGranularity,
		setCalendarGranularity,
		phoneLayout
	]);
	const effectiveScheduleEndSlot = (0, react.useMemo)(() => {
		const slotNode = scheduleEndSlotProp !== null && scheduleEndSlotProp !== void 0 ? scheduleEndSlotProp : defaultScheduleEnd;
		return /* @__PURE__ */ react.default.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 = (0, react.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 = (0, react.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 : "") === _pisell_materials.DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY;
		})) return base;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, typeof base === "object" && base !== null ? base : {}), {}, { filterList: [...filterList, {
			type: "custom",
			key: _pisell_materials.DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY,
			name: _pisell_materials.DEFAULT_RESOURCE_WALL_FILTER_FIELD_KEY,
			label: _pisell_utils.locales.getText(require_reservationConstants.I18N_RESOURCE_WALL_RESOURCES),
			other: {
				component: require_ReservationResourceWallResourceSelect.ReservationResourceWallResourceSelect,
				options: resourceWallFilterSelectOptions
			}
		}] });
	}, [
		bookingToolBar,
		isInternalListData,
		resourceWallFilterSelectOptions
	]);
	const loadReservationDensity = (0, react.useCallback)(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* ({ referenceDate, stepMinutes }) {
			const slotCount = require_reservationDensity.reservationTimelineAxisSlotCount(referenceDate, stepMinutes, effectiveTimelineRange);
			if (salesTimelineHighlights != null && salesTimelineHighlights.length > 0) return require_reservationDensity.fitTimelineHighlightsToSlotCount(salesTimelineHighlights, slotCount);
			return require_reservationDensity.computeReservationDensityBuckets(isInternalListData ? floorDayBookingData : data, referenceDate, stepMinutes);
		});
		return function(_x) {
			return _ref2.apply(this, arguments);
		};
	}(), [
		isInternalListData,
		floorDayBookingData,
		data,
		salesTimelineHighlights,
		effectiveTimelineRange.start,
		effectiveTimelineRange.end
	]);
	return { toolBarMerged: (0, react.useMemo)(() => {
		const _ref3 = toolBarProp !== null && toolBarProp !== void 0 ? toolBarProp : {}, { tabs: extraTabs, topLeft: topLeftProp, topRight: topRightProp, bottomLeft: bottomLeftProp, quickFilter: toolBarQuickFilterOverride } = _ref3, restToolbar = require_objectWithoutProperties._objectWithoutProperties(_ref3, _excluded);
		const booking = bookingToolBar !== null && bookingToolBar !== void 0 ? bookingToolBar : {};
		const effectiveQuickFilter = toolBarQuickFilterOverride !== void 0 ? toolBarQuickFilterOverride : bookingQuickFilterWithResourceWall;
		const baseToolbar = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, booking), restToolbar);
		const mobileToolbar = phoneLayout ? bodyView === "grid" || bodyView === "resourceWall" ? require_buildReservationMobileTableConfig.buildReservationMobileTableToolbarConfig({
			baseToolBar: baseToolbar,
			quickFilterFilterList: effectiveQuickFilter && typeof effectiveQuickFilter === "object" && Array.isArray(effectiveQuickFilter.filterList) ? effectiveQuickFilter.filterList : void 0
		}) : require_buildReservationMobileTableConfig.buildReservationPhoneNarrowStackToolbarConfig(baseToolbar) : baseToolbar;
		const phoneQuickFilter = phoneLayout && effectiveQuickFilter && typeof effectiveQuickFilter === "object" && Array.isArray(effectiveQuickFilter.filterList) ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, effectiveQuickFilter), {}, {
			size: "small",
			filterList: require_buildReservationMobileTableConfig.adaptQuickFiltersForMobileToolbar(effectiveQuickFilter.filterList)
		}) : effectiveQuickFilter;
		const mergedFilter = mobileToolbar.filter;
		const phoneFilter = phoneLayout && mergedFilter && typeof mergedFilter === "object" ? require_buildReservationMobileTableConfig.adaptFilterConfigForMobile(mergedFilter) : mergedFilter;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, mobileToolbar), phoneFilter ? { filter: phoneFilter } : {}), {}, {
			quickFilter: phoneQuickFilter,
			hideBodyViewToggle: phoneLayout && (bodyView === "grid" || bodyView === "resourceWall"),
			tabs: phoneLayout && (bodyView === "grid" || bodyView === "resourceWall") ? null : /* @__PURE__ */ react.default.createElement(require_PisellReservationSubcomponents.PisellReservationToolbarTabs, {
				extraTabs,
				schedule,
				onScheduleChange: handleScheduleChange,
				scheduleProps: schedulePropsProp,
				timeNavigatorProps: mergedTimeNavigatorProps,
				scheduleStartSlot: scheduleStartSlotProp !== null && scheduleStartSlotProp !== void 0 ? scheduleStartSlotProp : phoneLayout ? null : /* @__PURE__ */ react.default.createElement(require_PisellReservationSubcomponents.ReservationDefaultScheduleStart, null),
				scheduleEndSlot: effectiveScheduleEndSlot,
				followWallClock,
				onFollowWallClockToggle,
				onTimeNavigatorUserInteraction,
				loadDensity: loadReservationDensity,
				calendarGranularity,
				phoneLayout
			}),
			topLeft: /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, phoneLayout ? /* @__PURE__ */ react.default.createElement(require_PisellReservationSubcomponents.ReservationMobileToolbarBodyViewSelect, null) : null, topLeftProp),
			topRight: /* @__PURE__ */ react.default.createElement(require_PisellReservationSubcomponents.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
exports.useReservationToolBarMerged = useReservationToolBarMerged;
