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 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); }
/**
 * 「All」全览视图布局：把所有启用画布按各自原始尺寸（同一比例尺）紧凑装箱，
 * 产出整体宽高 + 每个画布区域在大画布坐标系下的位置与图元，供 PisellFloorMapLayout
 * 在主 TransformWrapper 内复用渲染（共享 ViewControls / 视口锁定 / 全屏 / fitBounds）。
 *
 * 不依赖 React，仅数据计算，便于单测与缓存。
 */
import { mergeCanvasItems } from "./floorMapConfigMerge";
import { computeFloorMapContentStage, packCanvasesIntoShelves } from "./floorMapCanvasStage";
import { gapToPx } from "./layoutMeasurements";
import { DEFAULT_BOUNDING_PADDING } from "../hooks/floorMapViewportPersistence";
import { mergeCanvasesForTabs } from "./mergeCanvasesForTabs";
import { FLOOR_MAP_OVERVIEW_TITLE_BAND_HEIGHT } from "../floorMapLayoutConstants";

/** 单个画布在 All 视图中的「区域」描述 */

/**
 * 构造 All 视图布局。
 *
 * - 顺序：按 `canvas.order` 升序，与顶部 Tab 顺序一致；
 * - 尺寸：每个画布按 {@link computeFloorMapContentStage}（含其自己的 boundingPadding）
 *   推导内容尺寸，所有画布共享同一比例尺，不缩放；
 * - 装箱：{@link packCanvasesIntoShelves} 行装填，目标行宽 ≈ √总面积。
 */
export function buildFloorMapOverviewLayout(params) {
  var config = params.config,
    globalMapLayer = params.globalMapLayer,
    cellSize = params.cellSize,
    itemUnit = params.itemUnit,
    areaGap = params.areaGap;
  var enriched = mergeCanvasesForTabs(config);
  var enabledOrdered = enriched.filter(function (c) {
    return c.enabled;
  }).sort(function (a, b) {
    return a.order - b.order;
  });
  var stageList = enabledOrdered.map(function (canvas) {
    var _effectiveMapLayer$bo;
    var items = mergeCanvasItems(canvas.id, config, cellSize);
    var effectiveMapLayer = canvas.mapLayer ? _objectSpread(_objectSpread({}, globalMapLayer !== null && globalMapLayer !== void 0 ? globalMapLayer : {}), canvas.mapLayer) : globalMapLayer;
    var boundingPadding = (_effectiveMapLayer$bo = effectiveMapLayer === null || effectiveMapLayer === void 0 ? void 0 : effectiveMapLayer.boundingPadding) !== null && _effectiveMapLayer$bo !== void 0 ? _effectiveMapLayer$bo : DEFAULT_BOUNDING_PADDING;
    var itemGap = gapToPx(effectiveMapLayer === null || effectiveMapLayer === void 0 ? void 0 : effectiveMapLayer.gap);
    var stage = computeFloorMapContentStage({
      items: items,
      cellSize: cellSize,
      itemGap: itemGap,
      itemUnit: itemUnit,
      boundingPadding: boundingPadding,
      preferPixelBoundingBox: true
    });
    return {
      canvasId: canvas.id,
      items: items,
      contentWidth: stage.contentWidth,
      contentHeight: stage.contentHeight,
      contentOffsetX: stage.contentOffsetX,
      contentOffsetY: stage.contentOffsetY,
      cellSize: cellSize
    };
  });
  var titleBandHeight = FLOOR_MAP_OVERVIEW_TITLE_BAND_HEIGHT;
  var pack = packCanvasesIntoShelves(stageList.map(function (s) {
    return {
      width: s.contentWidth,
      /** 装箱高度 = 标题带 + 画布内容，使相邻区块 gap 为标题留出空间 */
      height: s.contentHeight + titleBandHeight
    };
  }), areaGap);
  var areas = stageList.map(function (s, i) {
    var _pack$positions$i$x, _pack$positions$i, _pack$positions$i$y, _pack$positions$i2;
    return _objectSpread(_objectSpread({}, s), {}, {
      x: (_pack$positions$i$x = (_pack$positions$i = pack.positions[i]) === null || _pack$positions$i === void 0 ? void 0 : _pack$positions$i.x) !== null && _pack$positions$i$x !== void 0 ? _pack$positions$i$x : 0,
      y: (_pack$positions$i$y = (_pack$positions$i2 = pack.positions[i]) === null || _pack$positions$i2 === void 0 ? void 0 : _pack$positions$i2.y) !== null && _pack$positions$i$y !== void 0 ? _pack$positions$i$y : 0,
      titleBandHeight: titleBandHeight
    });
  });
  return {
    areas: areas,
    totalWidth: pack.totalWidth,
    totalHeight: pack.totalHeight
  };
}