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 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 { RecordBoardShellBodyMetaContext } from "./ShellBodyMetaContext";

/** 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 等** 与可选 **\<PisellRecordBoard.FloorMap /\>**；
 * 仅表格侧、仅平面图侧、或双视图（出现 Segmented 切换）由子节点组合决定。根级 `floorMap` props 仍支持（如低代码透传），与 FloorMap 子组件 props 合并，**子组件优先**。
 */
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;
  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,
    hasGridContent = _useMemo.hasGridContent,
    hasFloorMapChild = _useMemo.hasFloorMapChild;

  /** 子组件 FloorMap 与根级 context.floorMapProps 合并，子 props 覆盖根级 */
  var effectiveFloorMapProps = useMemo(function () {
    if (hasFloorMapChild) {
      var _ctx$floorMapProps;
      return _objectSpread(_objectSpread({}, (_ctx$floorMapProps = ctx.floorMapProps) !== null && _ctx$floorMapProps !== void 0 ? _ctx$floorMapProps : {}), floorMapPropsFromChild !== null && floorMapPropsFromChild !== void 0 ? floorMapPropsFromChild : {});
    }
    return ctx.floorMapProps;
  }, [hasFloorMapChild, floorMapPropsFromChild, ctx.floorMapProps]);

  /**
   * 平面图使用整份多 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 shellBodyMeta = useMemo(function () {
    return {
      effectiveFloorMapProps: effectiveFloorMapProps,
      hasGridContent: hasGridContent,
      hasFloorMapSlot: hasFloorMapSlot
    };
  }, [effectiveFloorMapProps, hasGridContent, hasFloorMapSlot]);

  /**
   * 仅平面图：强制 floorMap 视图；仅表格：强制 grid；双视图：尊重 ctx.bodyView。
   */
  var showGrid = useMemo(function () {
    if (!hasFloorMapSlot) return true;
    if (!hasGridContent) return false;
    return ctx.bodyView === 'grid';
  }, [hasFloorMapSlot, hasGridContent, ctx.bodyView]);

  /** 仅平面图 / 仅表格时同步 bodyView，避免切换组合后状态不一致 */
  useLayoutEffect(function () {
    if (!ctx.setBodyView) return;
    if (hasFloorMapSlot && !hasGridContent) {
      ctx.setBodyView('floorMap');
      return;
    }
    if (hasGridContent && !hasFloorMapSlot) {
      ctx.setBodyView('grid');
    }
  }, [hasFloorMapSlot, hasGridContent, 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 = effectiveFloorMapProps && !showGrid ? /*#__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 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) : floorMapNode, showGrid ? /*#__PURE__*/React.createElement(RecordBoardPagination, null) : null) : /*#__PURE__*/React.createElement(React.Fragment, null, showGrid ? gridChildren : floorMapNode, showGrid ? /*#__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, {
      'pisell-shell-frame--floor-map-view': !showGrid
    }),
    style: shellStyle,
    config: {
      showToolbar: true,
      showBatchActionBar: true
    },
    toolbar: /*#__PURE__*/React.createElement(RecordBoardToolBar, null),
    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 };
export { useRecordBoardShellBodyMeta } from "./ShellBodyMetaContext";