import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { EMPTY_FLOOR_MAP_LAYOUT_CONTEXT, FloorMapLayoutProvider } from "../../pisellFloorMapLayout/context/FloorMapLayoutContext.js";
import PisellFloorMapLayout from "../../pisellFloorMapLayout/PisellFloorMapLayout.js";
import { useRecordBoardContext } from "../hooks/useRecordBoardContext.js";
import PisellShellFrame from "../../pisellShellFrame/PisellShellFrame.js";
import "./FloorMap.js";
import "./Calendar/RecordBoardCalendar.js";
import "./ResourceWall/RecordBoardResourceWall.js";
import { partitionShellFrameChildren } from "../utils/partitionShellFrameChildren.js";
import "./ToolBarReset/index.js";
import "./ToolBarQuickFilter/index.js";
import { RecordBoardShellBodyMetaContext } from "./ShellBodyMetaContext.js";
import RecordBoardToolBar from "./ToolBar/index.js";
import RecordBoardBatchActionBar from "./BatchActionBar/index.js";
import RecordBoardPagination from "./Pagination/index.js";
import "./Search/index.js";
import { PisellRecordBoardCalendarView } from "./Calendar/PisellRecordBoardCalendarView.js";
import { PisellRecordBoardResourceWallView } from "./ResourceWall/PisellRecordBoardResourceWallView.js";
import React, { useEffect, useLayoutEffect, useMemo, useRef } from "react";
import classNames from "classnames";
import "./shellFrameFloorMap.less";
//#region src/components/pisellRecordBoard/shellFrame/index.tsx
/**
* 槽位 props 与根级合并：**子组件显式传入**的键覆盖根级；子级为 `undefined` 的键不覆盖（避免吞掉根级回调）。
* 子槽无 props（`{}`）时直接沿用根级对象引用。
*/
function mergeRecordBoardSlotProps(root, child) {
	if (!child || Object.keys(child).length === 0) return root;
	const base = _objectSpread2({}, root !== null && root !== void 0 ? root : {});
	for (const [key, val] of Object.entries(child)) if (val !== void 0) base[key] = val;
	return base;
}
/** fillHeight 时内容区可滚动、分页器贴底；默认表头 sticky；grid.scroll.autoCalc 时需测量高度供 scroll.y */
const FILL_HEIGHT_SCROLL_STYLE = {
	flex: 1,
	minHeight: 0,
	overflow: "auto"
};
/** 测量滚动区高度并写入 context，供 Grid 在 scroll.autoCalc 时计算 scroll.y（对齐 table useGenScroll）。autoCalc 时 overflow 为 hidden，仅 Table 内部滚动。 */
function ScrollAreaMeasurer({ children, onHeight, autoCalc }) {
	const ref = useRef(null);
	useEffect(() => {
		const el = ref.current;
		if (!el) return;
		const ro = new ResizeObserver((entries) => {
			const entry = entries[0];
			if (entry) onHeight(entry.contentRect.height);
		});
		ro.observe(el);
		onHeight(el.getBoundingClientRect().height);
		return () => {
			ro.disconnect();
			onHeight(null);
		};
	}, [onHeight]);
	return /* @__PURE__ */ React.createElement("div", {
		ref,
		className: "pisell-record-board-scroll-body",
		style: _objectSpread2(_objectSpread2({}, FILL_HEIGHT_SCROLL_STYLE), {}, { overflow: autoCalc ? "hidden" : "auto" })
	}, children);
}
const FILL_HEIGHT_WRAPPER_STYLE = {
	flex: 1,
	minHeight: 0,
	display: "flex",
	flexDirection: "column"
};
/**
* Shell Frame：基于 PisellShellFrame 的 RecordBoard 壳层。
* 解析子节点：**GridLayout / CardLayout 等** 与可选 **FloorMap / Calendar / ResourceWall** 槽；
* 子视图组合与 Segmented 切换由子节点与根级 props 决定；根级 `floorMap` / `calendar` / `resourceWall` 与子槽合并时**子组件优先**。
*/
const RecordBoardShellFrame = ({ children, className, style, belowToolbar }) => {
	var _ctx$floorMapLayoutRe, _effectiveFloorMapPro, _effectiveFloorMapPro2, _effectiveFloorMapPro3, _ctx$setScrollAreaHei, _ctx$childComponentPr;
	const ctx = useRecordBoardContext();
	const fillHeight = (ctx === null || ctx === void 0 ? void 0 : ctx.fillHeight) === true;
	const { gridChildren, floorMapPropsFromChild, calendarPropsFromChild, resourceWallPropsFromChild, hasGridContent, hasFloorMapChild, hasCalendarChild, hasResourceWallChild } = useMemo(() => partitionShellFrameChildren(children), [children]);
	/** 子组件 FloorMap 与根级 context.floorMapProps 合并，子 props 覆盖根级 */
	const effectiveFloorMapProps = useMemo(() => {
		if (hasFloorMapChild) return mergeRecordBoardSlotProps(ctx.floorMapProps, floorMapPropsFromChild);
		return ctx.floorMapProps;
	}, [
		hasFloorMapChild,
		floorMapPropsFromChild,
		ctx.floorMapProps
	]);
	/** 子组件 Calendar 与根级 context.calendarProps 合并，子 props 覆盖根级 */
	const effectiveCalendarProps = useMemo(() => {
		if (hasCalendarChild) return mergeRecordBoardSlotProps(ctx.calendarProps, calendarPropsFromChild);
		return ctx.calendarProps;
	}, [
		hasCalendarChild,
		calendarPropsFromChild,
		ctx.calendarProps
	]);
	const effectiveResourceWallProps = useMemo(() => {
		if (hasResourceWallChild) return mergeRecordBoardSlotProps(ctx.resourceWallProps, resourceWallPropsFromChild);
		return ctx.resourceWallProps;
	}, [
		hasResourceWallChild,
		resourceWallPropsFromChild,
		ctx.resourceWallProps
	]);
	/**
	* 平面图使用整份多 key：根级 Context.dataSources 与 FloorMap props 合并（后者覆盖同 key）。
	* 表格分页切片只影响 ctx.data，不得缩短此对象。
	*/
	const mergedFloorMapDataSources = useMemo(() => {
		const root = ctx.dataSources;
		const fromFloor = effectiveFloorMapProps === null || effectiveFloorMapProps === void 0 ? void 0 : effectiveFloorMapProps.dataSources;
		if (root == null && fromFloor == null) return void 0;
		return _objectSpread2(_objectSpread2({}, root !== null && root !== void 0 ? root : {}), fromFloor !== null && fromFloor !== void 0 ? fromFloor : {});
	}, [ctx.dataSources, effectiveFloorMapProps === null || effectiveFloorMapProps === void 0 ? void 0 : effectiveFloorMapProps.dataSources]);
	const hasFloorMapSlot = hasFloorMapChild || ctx.floorMapProps != null;
	const hasCalendarSlot = hasCalendarChild || ctx.calendarProps != null;
	const hasResourceWallSlot = hasResourceWallChild || ctx.resourceWallProps != null;
	const shellBodyMeta = useMemo(() => ({
		effectiveFloorMapProps,
		effectiveCalendarProps,
		effectiveResourceWallProps,
		hasGridContent,
		hasFloorMapSlot,
		hasCalendarSlot,
		hasResourceWallSlot
	}), [
		effectiveFloorMapProps,
		effectiveCalendarProps,
		effectiveResourceWallProps,
		hasGridContent,
		hasFloorMapSlot,
		hasCalendarSlot,
		hasResourceWallSlot
	]);
	const showGrid = useMemo(() => hasGridContent && ctx.bodyView === "grid", [hasGridContent, ctx.bodyView]);
	/** 无限滚动时不展示底部分页（含 paginationRender 挂载点） */
	const showGridPagination = showGrid && ctx.gridInfiniteScroll !== true;
	const showFloorMap = useMemo(() => hasFloorMapSlot && effectiveFloorMapProps != null && ctx.bodyView === "floorMap", [
		hasFloorMapSlot,
		effectiveFloorMapProps,
		ctx.bodyView
	]);
	const showCalendar = useMemo(() => hasCalendarSlot && ctx.bodyView === "calendar", [hasCalendarSlot, ctx.bodyView]);
	const showResourceWall = useMemo(() => hasResourceWallSlot && ctx.bodyView === "resourceWall", [hasResourceWallSlot, ctx.bodyView]);
	/** 仅单一主区时同步 bodyView，避免切换组合后状态不一致 */
	useLayoutEffect(() => {
		if (!ctx.setBodyView) return;
		const hasG = hasGridContent;
		const hasF = hasFloorMapSlot;
		const hasC = hasCalendarSlot;
		const hasR = hasResourceWallSlot;
		if ([
			hasG,
			hasF,
			hasC,
			hasR
		].filter(Boolean).length !== 1) return;
		if (hasF && !hasG && !hasC && !hasR) {
			ctx.setBodyView("floorMap");
			return;
		}
		if (hasC && !hasG && !hasF && !hasR) {
			ctx.setBodyView("calendar");
			return;
		}
		if (hasR && !hasG && !hasF && !hasC) {
			ctx.setBodyView("resourceWall");
			return;
		}
		if (hasG && !hasF && !hasC && !hasR) ctx.setBodyView("grid");
	}, [
		hasFloorMapSlot,
		hasGridContent,
		hasCalendarSlot,
		hasResourceWallSlot,
		ctx.setBodyView
	]);
	const floorMapLayoutProviderValue = useMemo(() => {
		var _ctx$floorMapLayoutCo;
		return (_ctx$floorMapLayoutCo = ctx.floorMapLayoutContext) !== null && _ctx$floorMapLayoutCo !== void 0 ? _ctx$floorMapLayoutCo : EMPTY_FLOOR_MAP_LAYOUT_CONTEXT;
	}, [ctx.floorMapLayoutContext]);
	const floorMapNode = showFloorMap && effectiveFloorMapProps ? /* @__PURE__ */ React.createElement("div", {
		className: "pisell-record-board-floor-map-viewport",
		style: {
			flex: 1,
			minHeight: 0,
			display: "flex",
			flexDirection: "column",
			overflow: "hidden",
			position: "relative"
		}
	}, /* @__PURE__ */ React.createElement(FloorMapLayoutProvider, { value: floorMapLayoutProviderValue }, /* @__PURE__ */ React.createElement(PisellFloorMapLayout, _objectSpread2(_objectSpread2(_objectSpread2({ ref: (_ctx$floorMapLayoutRe = ctx.floorMapLayoutRef) !== null && _ctx$floorMapLayoutRe !== void 0 ? _ctx$floorMapLayoutRe : void 0 }, effectiveFloorMapProps), mergedFloorMapDataSources !== void 0 ? { dataSources: mergedFloorMapDataSources } : {}), {}, {
		items: (_effectiveFloorMapPro = effectiveFloorMapProps.items) !== null && _effectiveFloorMapPro !== void 0 ? _effectiveFloorMapPro : [],
		renderItem: (_effectiveFloorMapPro2 = effectiveFloorMapProps.renderItem) !== null && _effectiveFloorMapPro2 !== void 0 ? _effectiveFloorMapPro2 : (() => null),
		style: _objectSpread2({
			flex: 1,
			minHeight: 0
		}, (_effectiveFloorMapPro3 = effectiveFloorMapProps.style) !== null && _effectiveFloorMapPro3 !== void 0 ? _effectiveFloorMapPro3 : {})
	}))), ctx.floorMapFooter ? /* @__PURE__ */ React.createElement("div", { className: "pisell-record-board-floor-map-floating-slot" }, ctx.floorMapFooter) : null) : null;
	const calendarNode = showCalendar && effectiveCalendarProps ? /* @__PURE__ */ React.createElement("div", {
		className: "pisell-record-board-calendar-viewport",
		style: {
			flex: 1,
			minHeight: 0,
			display: "flex",
			flexDirection: "column",
			overflow: "hidden",
			position: "relative"
		}
	}, /* @__PURE__ */ React.createElement(PisellRecordBoardCalendarView, effectiveCalendarProps)) : null;
	const resourceWallNode = showResourceWall && effectiveResourceWallProps ? /* @__PURE__ */ React.createElement("div", {
		className: "pisell-record-board-resource-wall-viewport",
		style: {
			flex: 1,
			minHeight: 0,
			display: "flex",
			flexDirection: "column",
			overflow: "hidden",
			position: "relative"
		}
	}, /* @__PURE__ */ React.createElement(PisellRecordBoardResourceWallView, effectiveResourceWallProps)) : null;
	const bodyContent = fillHeight ? /* @__PURE__ */ React.createElement(React.Fragment, null, showGrid ? /* @__PURE__ */ React.createElement(ScrollAreaMeasurer, {
		onHeight: (_ctx$setScrollAreaHei = ctx === null || ctx === void 0 ? void 0 : ctx.setScrollAreaHeight) !== null && _ctx$setScrollAreaHei !== void 0 ? _ctx$setScrollAreaHei : (() => {}),
		autoCalc: ctx === null || ctx === void 0 || (_ctx$childComponentPr = ctx.childComponentProps) === null || _ctx$childComponentPr === void 0 || (_ctx$childComponentPr = _ctx$childComponentPr.grid) === null || _ctx$childComponentPr === void 0 || (_ctx$childComponentPr = _ctx$childComponentPr.scroll) === null || _ctx$childComponentPr === void 0 ? void 0 : _ctx$childComponentPr.autoCalc
	}, gridChildren) : showResourceWall ? resourceWallNode : showCalendar ? calendarNode : floorMapNode, showGridPagination ? /* @__PURE__ */ React.createElement(RecordBoardPagination, null) : null) : /* @__PURE__ */ React.createElement(React.Fragment, null, showGrid ? gridChildren : showResourceWall ? resourceWallNode : showCalendar ? calendarNode : floorMapNode, showGridPagination ? /* @__PURE__ */ React.createElement(RecordBoardPagination, null) : null);
	const shellStyle = fillHeight && style ? _objectSpread2(_objectSpread2({}, style), FILL_HEIGHT_WRAPPER_STYLE) : fillHeight ? FILL_HEIGHT_WRAPPER_STYLE : style;
	const wrapper = /* @__PURE__ */ React.createElement(RecordBoardShellBodyMetaContext.Provider, { value: shellBodyMeta }, /* @__PURE__ */ React.createElement(PisellShellFrame, {
		className: classNames(className, {
			"pisell-shell-frame--body-subview": !showGrid,
			"pisell-shell-frame--floor-map-view": showFloorMap,
			"pisell-shell-frame--calendar-view": showCalendar,
			"pisell-shell-frame--resource-wall-view": showResourceWall
		}),
		style: shellStyle,
		config: {
			showToolbar: true,
			showBatchActionBar: true
		},
		toolbar: /* @__PURE__ */ React.createElement(RecordBoardToolBar, null),
		belowToolbar,
		batchActionBar: /* @__PURE__ */ React.createElement(RecordBoardBatchActionBar, null)
	}, bodyContent));
	if (fillHeight) return /* @__PURE__ */ React.createElement("div", { style: FILL_HEIGHT_WRAPPER_STYLE }, wrapper);
	return wrapper;
};
RecordBoardShellFrame.displayName = "RecordBoard.ShellFrame";
//#endregion
export { RecordBoardShellFrame as default };
