function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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 _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 解析、包围盒
 */

/** 默认每格像素 */
export var DEFAULT_CELL_SIZE = 64;

/** 默认 item 间距（像素） */
export var DEFAULT_ITEM_GAP = 8;

/**
 * 将 mapLayer.gap 解析为像素数
 */
export function gapToPx(gap) {
  if (gap === undefined) return DEFAULT_ITEM_GAP;
  if (typeof gap === 'number') return gap;
  var match = String(gap).match(/^(\d+(?:\.\d+)?)\s*px?$/i);
  return match ? Number(match[1]) : DEFAULT_ITEM_GAP;
}

/**
 * 根据 items 计算内容区像素宽高
 * - cell：按格子数 + cellSize + itemGap 计算
 * - pixel：按 item 的 x/y/width/height 像素取边界（仅右/下边界，用于非 floor map 或兼容）
 */
export function getContentSize(items, cellSize, itemGap, itemUnit) {
  if (!items.length) {
    if (itemUnit === 'pixel') {
      return {
        contentWidth: cellSize * 4,
        contentHeight: cellSize * 3
      };
    }
    var _g = itemGap;
    return {
      contentWidth: 4 * (cellSize + _g) + _g,
      contentHeight: 3 * (cellSize + _g) + _g
    };
  }
  if (itemUnit === 'pixel') {
    var maxRight = 0;
    var maxBottom = 0;
    var _iterator = _createForOfIteratorHelper(items),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var _item$width, _item$height;
        var item = _step.value;
        var w = (_item$width = item.width) !== null && _item$width !== void 0 ? _item$width : cellSize;
        var h = (_item$height = item.height) !== null && _item$height !== void 0 ? _item$height : cellSize;
        maxRight = Math.max(maxRight, item.x + w);
        maxBottom = Math.max(maxBottom, item.y + h);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    return {
      contentWidth: maxRight,
      contentHeight: maxBottom
    };
  }
  var maxX = 0;
  var maxY = 0;
  var _iterator2 = _createForOfIteratorHelper(items),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var _item$width2, _item$height2;
      var _item = _step2.value;
      var _w = (_item$width2 = _item.width) !== null && _item$width2 !== void 0 ? _item$width2 : 1;
      var _h = (_item$height2 = _item.height) !== null && _item$height2 !== void 0 ? _item$height2 : 1;
      maxX = Math.max(maxX, _item.x + _w);
      maxY = Math.max(maxY, _item.y + _h);
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  var g = itemGap;
  return {
    contentWidth: maxX * (cellSize + g) + g,
    contentHeight: maxY * (cellSize + g) + g
  };
}

/**
 * pixel 模式下取 items 包围盒（含左/上），用于画布四周扩 padding 时的尺寸与偏移
 */
export function getPixelBoundingBox(items, cellSize) {
  if (!items.length) return null;
  var minLeft = Infinity;
  var minTop = Infinity;
  var maxRight = -Infinity;
  var maxBottom = -Infinity;
  var _iterator3 = _createForOfIteratorHelper(items),
    _step3;
  try {
    for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
      var _item$width3, _item$height3;
      var item = _step3.value;
      var w = (_item$width3 = item.width) !== null && _item$width3 !== void 0 ? _item$width3 : cellSize;
      var h = (_item$height3 = item.height) !== null && _item$height3 !== void 0 ? _item$height3 : cellSize;
      minLeft = Math.min(minLeft, item.x);
      minTop = Math.min(minTop, item.y);
      maxRight = Math.max(maxRight, item.x + w);
      maxBottom = Math.max(maxBottom, item.y + h);
    }
  } catch (err) {
    _iterator3.e(err);
  } finally {
    _iterator3.f();
  }
  return {
    minLeft: minLeft,
    minTop: minTop,
    maxRight: maxRight,
    maxBottom: maxBottom
  };
}