function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
/**
 * 批量新建图元时的落点布局：在画布右侧空白区按近方阵网格排列，项与项之间保留 gap。
 */
import { getOverviewGridDims } from "./floorMapCanvasStage";

/** 批量落点时图元之间的间距（内容坐标 px） */
export var BATCH_PLACE_GAP = 16;
/**
 * 行列数尽量接近正方形（复用全览网格算法）。
 */
export function getSquareishGridDims(count) {
  return getOverviewGridDims(count);
}

/**
 * 计算批量落点左上角坐标列表（行优先：先填满第一行再换行）。
 */
export function computeBatchScenePlacementPositions(sizes, ctx) {
  var _ctx$boundingPadding;
  var count = sizes.length;
  if (count === 0) return [];
  var gap = BATCH_PLACE_GAP;
  var pad = Math.max(0, (_ctx$boundingPadding = ctx.boundingPadding) !== null && _ctx$boundingPadding !== void 0 ? _ctx$boundingPadding : 24);
  var _getSquareishGridDims = getSquareishGridDims(count),
    cols = _getSquareishGridDims.cols,
    rows = _getSquareishGridDims.rows;
  var colWidths = Array.from({
    length: cols
  }, function () {
    return 0;
  });
  var rowHeights = Array.from({
    length: rows
  }, function () {
    return 0;
  });
  for (var i = 0; i < count; i += 1) {
    var col = i % cols;
    var row = Math.floor(i / cols);
    colWidths[col] = Math.max(colWidths[col], sizes[i].width);
    rowHeights[row] = Math.max(rowHeights[row], sizes[i].height);
  }
  var anchorX = pad;
  var anchorY = pad;
  if (ctx.existingElements.length > 0) {
    var maxRight = Math.max.apply(Math, _toConsumableArray(ctx.existingElements.map(function (e) {
      return e.x + e.width;
    })));
    var minTop = Math.min.apply(Math, _toConsumableArray(ctx.existingElements.map(function (e) {
      return e.y;
    })));
    anchorX = maxRight + gap;
    anchorY = Math.max(pad, minTop);
  }
  var colOffsets = [];
  var xCursor = 0;
  for (var c = 0; c < cols; c += 1) {
    colOffsets[c] = xCursor;
    xCursor += colWidths[c] + (c < cols - 1 ? gap : 0);
  }
  var rowOffsets = [];
  var yCursor = 0;
  for (var r = 0; r < rows; r += 1) {
    rowOffsets[r] = yCursor;
    yCursor += rowHeights[r] + (r < rows - 1 ? gap : 0);
  }
  var positions = [];
  for (var _i = 0; _i < count; _i += 1) {
    var _col = _i % cols;
    var _row = Math.floor(_i / cols);
    var cellW = colWidths[_col];
    var cellH = rowHeights[_row];
    var x = anchorX + colOffsets[_col] + Math.round((cellW - sizes[_i].width) / 2);
    var y = anchorY + rowOffsets[_row] + Math.round((cellH - sizes[_i].height) / 2);
    positions.push({
      x: Math.round(x),
      y: Math.round(y)
    });
  }
  return positions;
}
/** 由批量落点坐标与尺寸计算轴对齐包围盒（可加 margin 便于视口聚焦留边） */
export function getPlacementBoundingBox(positions, sizes) {
  var margin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : BATCH_PLACE_GAP;
  if (!positions.length || positions.length !== sizes.length) return null;
  var minLeft = Infinity;
  var minTop = Infinity;
  var maxRight = -Infinity;
  var maxBottom = -Infinity;
  for (var i = 0; i < positions.length; i += 1) {
    var _positions$i = positions[i],
      x = _positions$i.x,
      y = _positions$i.y;
    var w = sizes[i].width;
    var h = sizes[i].height;
    minLeft = Math.min(minLeft, x);
    minTop = Math.min(minTop, y);
    maxRight = Math.max(maxRight, x + w);
    maxBottom = Math.max(maxBottom, y + h);
  }
  var m = Math.max(0, margin);
  return {
    minLeft: minLeft - m,
    minTop: minTop - m,
    maxRight: maxRight + m,
    maxBottom: maxBottom + m
  };
}