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); }
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; }
import { FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND } from "../types";
import { isDirectPlacePaletteKind } from "./floorMapElementKindUtils";
import { FLOOR_MAP_BUILTIN_SHAPES } from "./floorMapBuiltinShapes";
import { FLOOR_MAP_TOOLBAR_PALETTE_SIDEBAR_LIMIT } from "../floorMapLayoutConstants";

/** @deprecated 使用 {@link FLOOR_MAP_TOOLBAR_PALETTE_SIDEBAR_LIMIT} */
export var FLOOR_MAP_TOOLBAR_PALETTE_VISIBLE_LIMIT = FLOOR_MAP_TOOLBAR_PALETTE_SIDEBAR_LIMIT;

/** 解析侧栏每类展示上限（至少 1） */
export function resolvePaletteSidebarVisibleLimit(limit) {
  var n = limit !== null && limit !== void 0 ? limit : FLOOR_MAP_TOOLBAR_PALETTE_SIDEBAR_LIMIT;
  return Math.max(1, Math.floor(n));
}

/** 分类下图元数是否应显示「查看全部」 */
export function shouldShowPaletteViewAll(entryCount, limit) {
  return entryCount > resolvePaletteSidebarVisibleLimit(limit);
}

/** 侧栏直接展示的 palette 条目（前 N 项） */
export function getPaletteSidebarVisibleEntries(entries, limit) {
  return entries.slice(0, resolvePaletteSidebarVisibleLimit(limit));
}

/** 批量从弹窗落点时的单项描述（与 {@link FloorMapPlaceMode} scene 子集对齐） */

/**
 * 侧栏 / 弹窗共用的 palette 条目：普通 kind 一项；内置图形 kind 按预设展开多项。
 */

/** 该 palette 条目是否点击即落画布（装饰内置图形单卡、无绑定 kind 等） */
export function isDirectPlacePaletteEntry(entry) {
  if (entry.builtinShapeKey) return true;
  return isDirectPlacePaletteKind(entry.kind);
}
export function buildToolbarPaletteEntries(kinds, resolveBuiltinLabel) {
  var out = [];
  var _iterator = _createForOfIteratorHelper(kinds),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var kind = _step.value;
      if (kind.value === FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND) {
        var _iterator2 = _createForOfIteratorHelper(FLOOR_MAP_BUILTIN_SHAPES),
          _step2;
        try {
          for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
            var _shapeDef = _step2.value;
            out.push({
              key: "builtin:".concat(kind.value, ":").concat(_shapeDef.key),
              kind: kind,
              builtinShapeKey: _shapeDef.key,
              shapeDef: _shapeDef,
              label: resolveBuiltinLabel(_shapeDef)
            });
          }
        } catch (err) {
          _iterator2.e(err);
        } finally {
          _iterator2.f();
        }
        continue;
      }
      out.push({
        key: "kind:".concat(kind.value),
        kind: kind,
        label: kind.label
      });
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return out;
}

/** palette 条目 → 批量落点 payload（每项数量 1） */
export function paletteEntriesToBulkPlaceItems(entries) {
  return expandPaletteEntriesByQuantity(entries, Object.fromEntries(entries.map(function (e) {
    return [e.key, 1];
  })));
}

/**
 * 按弹窗内各条目数量展开为落点列表（数量为 n 则重复 n 次，落点时阶梯偏移）。
 */
export function expandPaletteEntriesByQuantity(entries, quantities) {
  var out = [];
  var _iterator3 = _createForOfIteratorHelper(entries),
    _step3;
  try {
    for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
      var _quantities$e$key;
      var e = _step3.value;
      var n = Math.max(0, Math.floor((_quantities$e$key = quantities[e.key]) !== null && _quantities$e$key !== void 0 ? _quantities$e$key : 0));
      for (var i = 0; i < n; i += 1) {
        out.push(_objectSpread({
          elementKind: e.kind.value
        }, e.builtinShapeKey ? {
          builtinShapeKey: e.builtinShapeKey
        } : {}));
      }
    }
  } catch (err) {
    _iterator3.e(err);
  } finally {
    _iterator3.f();
  }
  return out;
}