import React from 'react';
import RecordBoardFloorMap from "../shellFrame/FloorMap";

/** 与 shellFrame/FloorMap 上定义的 displayName 一致，用于从 ShellFrame children 中识别平面图槽 */
export var RECORD_BOARD_FLOOR_MAP_DISPLAY_NAME = 'RecordBoard.FloorMap';

/** displayName 在部分构建下可能被摇掉；与 RecordBoardFloorMap 引用一致时仍可识别，工具栏才出「表格/平面图」切换 */
function isRecordBoardFloorMapElementType(type) {
  if (type === RecordBoardFloorMap) return true;
  var dn = type === null || type === void 0 ? void 0 : type.displayName;
  return dn === RECORD_BOARD_FLOOR_MAP_DISPLAY_NAME;
}

/**
 * 将 ShellFrame 的 children 拆成：供表格区渲染的子树 + 可选的 FloorMap 透传 props。
 * 支持 Fragment 嵌套；多个 FloorMap 时以最后一个为准。
 */
export function partitionShellFrameChildren(children) {
  var foundFloorMap = false;
  var floorProps;
  var gridChunks = [];
  var walk = function walk(node) {
    React.Children.forEach(node, function (child) {
      if (child == null || child === false) return;
      if (! /*#__PURE__*/React.isValidElement(child)) {
        gridChunks.push(child);
        return;
      }
      if (child.type === React.Fragment) {
        walk(child.props.children);
        return;
      }
      if (isRecordBoardFloorMapElementType(child.type)) {
        foundFloorMap = true;
        floorProps = child.props;
        return;
      }
      gridChunks.push(child);
    });
  };
  walk(children);
  var hasGridContent = gridChunks.length > 0;
  var gridChildren;
  if (!hasGridContent) {
    gridChildren = null;
  } else if (gridChunks.length === 1) {
    gridChildren = gridChunks[0];
  } else {
    gridChildren = /*#__PURE__*/React.createElement(React.Fragment, null, gridChunks);
  }
  return {
    gridChildren: gridChildren,
    hasFloorMapChild: foundFloorMap,
    floorMapPropsFromChild: floorProps,
    hasGridContent: hasGridContent
  };
}