var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellFloorMapLayout/utils/batchScenePlacementLayout.ts
var batchScenePlacementLayout_exports = {};
__export(batchScenePlacementLayout_exports, {
  BATCH_PLACE_GAP: () => BATCH_PLACE_GAP,
  computeBatchScenePlacementPositions: () => computeBatchScenePlacementPositions,
  getPlacementBoundingBox: () => getPlacementBoundingBox,
  getSquareishGridDims: () => getSquareishGridDims
});
module.exports = __toCommonJS(batchScenePlacementLayout_exports);
var import_floorMapCanvasStage = require("./floorMapCanvasStage");
var BATCH_PLACE_GAP = 16;
function getSquareishGridDims(count) {
  return (0, import_floorMapCanvasStage.getOverviewGridDims)(count);
}
function computeBatchScenePlacementPositions(sizes, ctx) {
  const count = sizes.length;
  if (count === 0) return [];
  const gap = BATCH_PLACE_GAP;
  const pad = Math.max(0, ctx.boundingPadding ?? 24);
  const { cols, rows } = getSquareishGridDims(count);
  const colWidths = Array.from({ length: cols }, () => 0);
  const rowHeights = Array.from({ length: rows }, () => 0);
  for (let i = 0; i < count; i += 1) {
    const col = i % cols;
    const row = Math.floor(i / cols);
    colWidths[col] = Math.max(colWidths[col], sizes[i].width);
    rowHeights[row] = Math.max(rowHeights[row], sizes[i].height);
  }
  let anchorX = pad;
  let anchorY = pad;
  if (ctx.existingElements.length > 0) {
    const maxRight = Math.max(
      ...ctx.existingElements.map((e) => e.x + e.width)
    );
    const minTop = Math.min(...ctx.existingElements.map((e) => e.y));
    anchorX = maxRight + gap;
    anchorY = Math.max(pad, minTop);
  }
  const colOffsets = [];
  let xCursor = 0;
  for (let c = 0; c < cols; c += 1) {
    colOffsets[c] = xCursor;
    xCursor += colWidths[c] + (c < cols - 1 ? gap : 0);
  }
  const rowOffsets = [];
  let yCursor = 0;
  for (let r = 0; r < rows; r += 1) {
    rowOffsets[r] = yCursor;
    yCursor += rowHeights[r] + (r < rows - 1 ? gap : 0);
  }
  const positions = [];
  for (let i = 0; i < count; i += 1) {
    const col = i % cols;
    const row = Math.floor(i / cols);
    const cellW = colWidths[col];
    const cellH = rowHeights[row];
    const x = anchorX + colOffsets[col] + Math.round((cellW - sizes[i].width) / 2);
    const y = anchorY + rowOffsets[row] + Math.round((cellH - sizes[i].height) / 2);
    positions.push({ x: Math.round(x), y: Math.round(y) });
  }
  return positions;
}
function getPlacementBoundingBox(positions, sizes, margin = BATCH_PLACE_GAP) {
  if (!positions.length || positions.length !== sizes.length) return null;
  let minLeft = Infinity;
  let minTop = Infinity;
  let maxRight = -Infinity;
  let maxBottom = -Infinity;
  for (let i = 0; i < positions.length; i += 1) {
    const { x, y } = positions[i];
    const w = sizes[i].width;
    const 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);
  }
  const m = Math.max(0, margin);
  return {
    minLeft: minLeft - m,
    minTop: minTop - m,
    maxRight: maxRight + m,
    maxBottom: maxBottom + m
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BATCH_PLACE_GAP,
  computeBatchScenePlacementPositions,
  getPlacementBoundingBox,
  getSquareishGridDims
});
