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/floorMapCanvasStage.ts
var floorMapCanvasStage_exports = {};
__export(floorMapCanvasStage_exports, {
  computeFloorMapContentStage: () => computeFloorMapContentStage,
  getOverviewGridDims: () => getOverviewGridDims,
  packCanvasesIntoShelves: () => packCanvasesIntoShelves
});
module.exports = __toCommonJS(floorMapCanvasStage_exports);
var import_layoutMeasurements = require("./layoutMeasurements");
function computeFloorMapContentStage(params) {
  const {
    items,
    cellSize,
    itemGap,
    itemUnit,
    boundingPadding,
    preferPixelBoundingBox = true
  } = params;
  const padSingle = boundingPadding;
  const pad = padSingle * 2;
  const useBoundingBox = preferPixelBoundingBox && itemUnit === "pixel" && items.length > 0;
  const box = useBoundingBox ? (0, import_layoutMeasurements.getPixelBoundingBox)(items, cellSize) : null;
  if (box) {
    return {
      contentWidth: box.maxRight - box.minLeft + pad,
      contentHeight: box.maxBottom - box.minTop + pad,
      contentOffsetX: padSingle - box.minLeft,
      contentOffsetY: padSingle - box.minTop,
      alignPadSingle: padSingle
    };
  }
  const fromItems = (0, import_layoutMeasurements.getContentSize)(items, cellSize, itemGap, itemUnit);
  return {
    contentWidth: fromItems.contentWidth + pad,
    contentHeight: fromItems.contentHeight + pad,
    contentOffsetX: 0,
    contentOffsetY: 0,
    alignPadSingle: null
  };
}
function getOverviewGridDims(count) {
  if (count <= 0) return { cols: 1, rows: 1 };
  if (count <= 3) return { cols: count, rows: 1 };
  const cols = Math.ceil(Math.sqrt(count));
  const rows = Math.ceil(count / cols);
  return { cols, rows };
}
function packCanvasesIntoShelves(boxes, gap, aspectBias = 2.5) {
  if (boxes.length === 0) {
    return { positions: [], totalWidth: 0, totalHeight: 0 };
  }
  const safeGap = Math.max(0, gap);
  const safeBias = Number.isFinite(aspectBias) && aspectBias > 0 ? aspectBias : 1;
  const totalArea = boxes.reduce(
    (acc, b) => acc + Math.max(0, b.width) * Math.max(0, b.height),
    0
  );
  const widestBox = boxes.reduce((max, b) => Math.max(max, b.width), 0);
  const targetWidth = Math.max(widestBox, Math.sqrt(totalArea * safeBias));
  const placed = [];
  const positions = new Array(boxes.length);
  let totalWidth = 0;
  let totalHeight = 0;
  const conflicts = (x, y, w, h, p) => {
    const xClash = x + w + safeGap > p.x && p.x + p.w + safeGap > x;
    const yClash = y + h + safeGap > p.y && p.y + p.h + safeGap > y;
    return xClash && yClash;
  };
  boxes.forEach((box, i) => {
    const candidates = [{ x: 0, y: 0 }];
    for (const p of placed) {
      candidates.push({ x: p.x + p.w + safeGap, y: p.y });
      candidates.push({ x: p.x, y: p.y + p.h + safeGap });
    }
    let bestX = Number.POSITIVE_INFINITY;
    let bestY = Number.POSITIVE_INFINITY;
    for (const cand of candidates) {
      const { x, y } = cand;
      if (x + box.width > targetWidth && placed.length > 0 && box.width <= targetWidth) {
        continue;
      }
      let clash = false;
      for (const p of placed) {
        if (conflicts(x, y, box.width, box.height, p)) {
          clash = true;
          break;
        }
      }
      if (clash) continue;
      if (y < bestY || y === bestY && x < bestX) {
        bestX = x;
        bestY = y;
      }
    }
    if (!Number.isFinite(bestX)) {
      bestX = 0;
      bestY = placed.reduce((m, p) => Math.max(m, p.y + p.h + safeGap), 0);
    }
    placed.push({ x: bestX, y: bestY, w: box.width, h: box.height });
    positions[i] = { x: bestX, y: bestY };
    if (bestX + box.width > totalWidth) totalWidth = bestX + box.width;
    if (bestY + box.height > totalHeight) totalHeight = bestY + box.height;
  });
  return { positions, totalWidth, totalHeight };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  computeFloorMapContentStage,
  getOverviewGridDims,
  packCanvasesIntoShelves
});
