function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useEffect, useLayoutEffect, useMemo, useRef } from 'react';
import classNames from 'classnames';
import { PisellShellFrame } from "../../pisellShellFrame";
import { PisellFloorMapLayout, FloorMapLayoutProvider, EMPTY_FLOOR_MAP_LAYOUT_CONTEXT } from "../../pisellFloorMapLayout";
import { useRecordBoardContext } from "../hooks/useRecordBoardContext";
import { partitionShellFrameChildren } from "../utils/partitionShellFrameChildren";
import RecordBoardToolBar from "./ToolBar";
import RecordBoardBatchActionBar from "./BatchActionBar";
import RecordBoardPagination from "./Pagination";
import RecordBoardSearch from "./Search";
import "./shellFrameFloorMap.less";
import ToolBarReset from "./ToolBarReset";
import ToolBarQuickFilter from "./ToolBarQuickFilter";
import RecordBoardFloorMap from "./FloorMap";
import RecordBoardCalendar from "./Calendar/RecordBoardCalendar";
import { PisellRecordBoardCalendarView } from "./Calendar/PisellRecordBoardCalendarView";
import RecordBoardResourceWall from "./ResourceWall/RecordBoardResourceWall";
import { PisellRecordBoardResourceWallView } from "./ResourceWall/PisellRecordBoardResourceWallView";
import { RecordBoardShellBodyMetaContext } from "./ShellBodyMetaContext";

/**
 * 槽位 props 与根级合并：**子组件显式传入**的键覆盖根级；子级为 `undefined` 的键不覆盖（避免吞掉根级回调）。
 * 子槽无 props（`{}`）时直接沿用根级对象引用。
 */
function mergeRecordBoardSlotProps(root, child) {
  if (!child || Object.keys(child).length === 0) {
    return root;
  }
  var base = _objectSpread({}, root !== null && root !== void 0 ? root : {});
  for (var _i = 0, _Object$entries = Object.entries(child); _i < _Object$entries.length; _i++) {
    var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
      key = _Object$entries$_i[0],
      val = _Object$entries$_i[1];
    if (val !== undefined) {
      base[key] = val;
    }
  }
  return base;
}

/** fillHeight 时内容区可滚动、分页器贴底；默认表头 sticky；grid.scroll.autoCalc 时需测量高度供 scroll.y */
var FILL_HEIGHT_SCROLL_STYLE = {
  flex: 1,
  minHeight: 0,
  overflow: 'auto'
};

/** 测量滚动区高度并写入 context，供 Grid 在 scroll.autoCalc 时计算 scroll.y（对齐 table useGenScroll）。autoCalc 时 overflow 为 hidden，仅 Table 内部滚动。 */
function ScrollAreaMeasurer(_ref) {
  var children = _ref.children,
    onHeight = _ref.onHeight,
    autoCalc = _ref.autoCalc;
  var ref = useRef(null);
  useEffect(function () {
    var el = ref.current;
    if (!el) return;
    var ro = new ResizeObserver(function (entries) {
      var entry = entries[0];
      if (entry) onHeight(entry.contentRect.height);
    });
    ro.observe(el);
    onHeight(el.getBoundingClientRect().height);
    return function () {
      ro.disconnect();
      onHeight(null);
    };
  }, [onHeight]);
  return /*#__PURE__*/React.createElement("div", {
    ref: ref,
    className: "pisell-record-board-scroll-body",
    style: _objectSpread(_objectSpread({}, FILL_HEIGHT_SCROLL_STYLE), {}, {
      overflow: autoCalc ? 'hidden' : 'auto'
    })
  }, children);
}
var 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` 与子槽合并时**子组件优先**。
 */
var RecordBoardShellFrame = function RecordBoardShellFrame(_ref2) {
  var _ctx$floorMapLayoutRe, _effectiveFloorMapPro, _effectiveFloorMapPro2, _effectiveFloorMapPro3, _ctx$setScrollAreaHei, _ctx$childComponentPr;
  var children = _ref2.children,
    className = _ref2.className,
    style = _ref2.style,
    belowToolbar = _ref2.belowToolbar;
  var ctx = useRecordBoardContext();
  var fillHeight = (ctx === null || ctx === void 0 ? void 0 : ctx.fillHeight) === true;
  var _useMemo = useMemo(function () {
      return partitionShellFrameChildren(children);
    }, [children]),
    gridChildren = _useMemo.gridChildren,
    floorMapPropsFromChild = _useMemo.floorMapPropsFromChild,
    calendarPropsFromChild = _useMemo.calendarPropsFromChild,
    resourceWallPropsFromChild = _useMemo.resourceWallPropsFromChild,
    hasGridContent = _useMemo.hasGridContent,
    hasFloorMapChild = _useMemo.hasFloorMapChild,
    hasCalendarChild = _useMemo.hasCalendarChild,
    hasResourceWallChild = _useMemo.hasResourceWallChild;

  /** 子组件 FloorMap 与根级 context.floorMapProps 合并，子 props 覆盖根级 */
  var effectiveFloorMapProps = useMemo(function () {
    if (hasFloorMapChild) {
      return mergeRecordBoardSlotProps(ctx.floorMapProps, floorMapPropsFromChild);
    }
    return ctx.floorMapProps;
  }, [hasFloorMapChild, floorMapPropsFromChild, ctx.floorMapProps]);

  /** 子组件 Calendar 与根级 context.calendarProps 合并，子 props 覆盖根级 */
  var effectiveCalendarProps = useMemo(function () {
    if (hasCalendarChild) {
      return mergeRecordBoardSlotProps(ctx.calendarProps, calendarPropsFromChild);
    }
    return ctx.calendarProps;
  }, [hasCalendarChild, calendarPropsFromChild, ctx.calendarProps]);
  var effectiveResourceWallProps = useMemo(function () {
    if (hasResourceWallChild) {
      return mergeRecordBoardSlotProps(ctx.resourceWallProps, resourceWallPropsFromChild);
    }
    return ctx.resourceWallProps;
  }, [hasResourceWallChild, resourceWallPropsFromChild, ctx.resourceWallProps]);

  /**
   * 平面图使用整份多 key：根级 Context.dataSources 与 FloorMap props 合并（后者覆盖同 key）。
   * 表格分页切片只影响 ctx.data，不得缩短此对象。
   */
  var mergedFloorMapDataSources = useMemo(function () {
    var root = ctx.dataSources;
    var fromFloor = effectiveFloorMapProps === null || effectiveFloorMapProps === void 0 ? void 0 : effectiveFloorMapProps.dataSources;
    if (root == null && fromFloor == null) return undefined;
    return _objectSpread(_objectSpread({}, root !== null && root !== void 0 ? root : {}), fromFloor !== null && fromFloor !== void 0 ? fromFloor : {});
  }, [ctx.dataSources, effectiveFloorMapProps === null || effectiveFloorMapProps === void 0 ? void 0 : effectiveFloorMapProps.dataSources]);
  var hasFloorMapSlot = hasFloorMapChild || ctx.floorMapProps != null;
  var hasCalendarSlot = hasCalendarChild || ctx.calendarProps != null;
  var hasResourceWallSlot = hasResourceWallChild || ctx.resourceWallProps != null;
  var shellBodyMeta = useMemo(function () {
    return {
      effectiveFloorMapProps: effectiveFloorMapProps,
      effectiveCalendarProps: effectiveCalendarProps,
      effectiveResourceWallProps: effectiveResourceWallProps,
      hasGridContent: hasGridContent,
      hasFloorMapSlot: hasFloorMapSlot,
      hasCalendarSlot: hasCalendarSlot,
      hasResourceWallSlot: hasResourceWallSlot
    };
  }, [effectiveFloorMapProps, effectiveCalendarProps, effectiveResourceWallProps, hasGridContent, hasFloorMapSlot, hasCalendarSlot, hasResourceWallSlot]);
  var showGrid = useMemo(function () {
    return hasGridContent && ctx.bodyView === 'grid';
  }, [hasGridContent, ctx.bodyView]);

  /** 无限滚动时不展示底部分页（含 paginationRender 挂载点） */
  var showGridPagination = showGrid && ctx.gridInfiniteScroll !== true;
  var showFloorMap = useMemo(function () {
    return hasFloorMapSlot && effectiveFloorMapProps != null && ctx.bodyView === 'floorMap';
  }, [hasFloorMapSlot, effectiveFloorMapProps, ctx.bodyView]);
  var showCalendar = useMemo(function () {
    return hasCalendarSlot && ctx.bodyView === 'calendar';
  }, [hasCalendarSlot, ctx.bodyView]);
  var showResourceWall = useMemo(function () {
    return hasResourceWallSlot && ctx.bodyView === 'resourceWall';
  }, [hasResourceWallSlot, ctx.bodyView]);

  /** 仅单一主区时同步 bodyView，避免切换组合后状态不一致 */
  useLayoutEffect(function () {
    if (!ctx.setBodyView) return;
    var hasG = hasGridContent;
    var hasF = hasFloorMapSlot;
    var hasC = hasCalendarSlot;
    var hasR = hasResourceWallSlot;
    var n = [hasG, hasF, hasC, hasR].filter(Boolean).length;
    if (n !== 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]);
  var floorMapLayoutProviderValue = useMemo(function () {
    var _ctx$floorMapLayoutCo;
    return (_ctx$floorMapLayoutCo = ctx.floorMapLayoutContext) !== null && _ctx$floorMapLayoutCo !== void 0 ? _ctx$floorMapLayoutCo : EMPTY_FLOOR_MAP_LAYOUT_CONTEXT;
  }, [ctx.floorMapLayoutContext]);
  var 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, _extends({
    ref: (_ctx$floorMapLayoutRe = ctx.floorMapLayoutRef) !== null && _ctx$floorMapLayoutRe !== void 0 ? _ctx$floorMapLayoutRe : undefined
  }, effectiveFloorMapProps, mergedFloorMapDataSources !== undefined ? {
    dataSources: mergedFloorMapDataSources
  } : {}, {
    items: (_effectiveFloorMapPro = effectiveFloorMapProps.items) !== null && _effectiveFloorMapPro !== void 0 ? _effectiveFloorMapPro : [],
    renderItem: (_effectiveFloorMapPro2 = effectiveFloorMapProps.renderItem) !== null && _effectiveFloorMapPro2 !== void 0 ? _effectiveFloorMapPro2 : function () {
      return null;
    },
    style: _objectSpread({
      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;
  var 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;
  var 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;
  var 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 : function () {},
    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);
  var shellStyle = fillHeight && style ? _objectSpread(_objectSpread({}, style), FILL_HEIGHT_WRAPPER_STYLE) : fillHeight ? FILL_HEIGHT_WRAPPER_STYLE : style;
  var wrapper = /*#__PURE__*/React.createElement(RecordBoardShellBodyMetaContext.Provider, {
    value: shellBodyMeta
  }, /*#__PURE__*/React.createElement(PisellShellFrame, {
    className: classNames(className, {
      /** 非表格主区：工具栏与内容区无缝（与 PisellShellFrame.less body-subview 配对） */
      'pisell-shell-frame--body-subview': !showGrid,
      'pisell-shell-frame--grid-view': 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: 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';
export default RecordBoardShellFrame;
export { RecordBoardToolBar, RecordBoardBatchActionBar, RecordBoardPagination, RecordBoardSearch, ToolBarReset, ToolBarQuickFilter, RecordBoardFloorMap, RecordBoardCalendar, RecordBoardResourceWall };
export { useRecordBoardShellBodyMeta } from "./ShellBodyMetaContext";