function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
 * 平面图 RoomCard：密度档位（布局像素 × viewportScale → visualShortSide）。
 */
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from 'react';
/** `visualShortSide`（px）分档：comfortable 提高后，compact/mini 按比例由原 160/110/76 缩放约 180/160 */
var THRESH_COMFORTABLE = 180;
var THRESH_COMPACT = 124;
var THRESH_MINI = 86;
export var FLOOR_ROOM_CARD_DENSITY_HYSTERESIS_PX = 6;
export function pickDensityFromVisualShortSide(visualShortSide) {
  if (!Number.isFinite(visualShortSide) || visualShortSide <= 0) {
    return 'micro';
  }
  if (visualShortSide >= THRESH_COMFORTABLE) return 'comfortable';
  if (visualShortSide >= THRESH_COMPACT) return 'compact';
  if (visualShortSide >= THRESH_MINI) return 'mini';
  return 'micro';
}
function densityRank(d) {
  switch (d) {
    case 'comfortable':
      return 3;
    case 'compact':
      return 2;
    case 'mini':
      return 1;
    case 'micro':
      return 0;
    default:
      return 0;
  }
}
export function applyDensityHysteresis(visualShortSide, prev) {
  var raw = pickDensityFromVisualShortSide(visualShortSide);
  var h = FLOOR_ROOM_CARD_DENSITY_HYSTERESIS_PX;
  if (prev == null) return raw;
  if (densityRank(raw) === densityRank(prev)) return prev;
  if (densityRank(raw) > densityRank(prev)) {
    if (raw === 'comfortable' && visualShortSide < THRESH_COMFORTABLE + h) {
      return prev;
    }
    if (raw === 'compact' && visualShortSide < THRESH_COMPACT + h) {
      return prev;
    }
    if (raw === 'mini' && visualShortSide < THRESH_MINI + h) {
      return prev;
    }
    return raw;
  }
  if (raw === 'compact' && prev === 'comfortable') {
    if (visualShortSide > THRESH_COMFORTABLE - h) return prev;
  }
  if (raw === 'mini' && prev === 'compact') {
    if (visualShortSide > THRESH_COMPACT - h) return prev;
  }
  if (raw === 'micro' && prev === 'mini') {
    if (visualShortSide > THRESH_MINI - h) return prev;
  }
  return raw;
}
export function computeVisualShortSide(layoutW, layoutH, viewportScale) {
  var s = Number.isFinite(viewportScale) && viewportScale > 0 ? viewportScale : 1;
  var w = Number.isFinite(layoutW) && layoutW > 0 ? layoutW : 0;
  var h = Number.isFinite(layoutH) && layoutH > 0 ? layoutH : 0;
  return Math.min(w, h) * s;
}
export function pickDensityFromLayoutAndViewport(layoutW, layoutH, viewportScale) {
  return pickDensityFromVisualShortSide(computeVisualShortSide(layoutW, layoutH, viewportScale));
}

/**
 * 对外仅两种视觉档位：大卡 vs Tooltip 极简小卡（见 {@link toFloorRoomCardUiDensity}）。
 */

/**
 * 根节点 `data-density`：方桌仅 **micro** → micro；圆桌 **mini / micro** → micro。
 */
export function toFloorRoomCardUiDensity(d) {
  var shellShape = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'rect';
  if (d === 'micro') return 'micro';
  if (d === 'mini') return shellShape === 'circle' ? 'micro' : 'comfortable';
  return 'comfortable';
}

/**
 * Tooltip 极简小卡壳：方桌仅 **micro**；圆桌 **mini** 与 **micro**。
 */
export function isFloorRoomCardTooltipDensity(d) {
  var shellShape = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'rect';
  if (d === 'micro') return true;
  if (d === 'mini') return shellShape === 'circle';
  return false;
}
export var FloorRoomCardDensityContext = /*#__PURE__*/createContext('comfortable');
export function useFloorRoomCardDensity() {
  return useContext(FloorRoomCardDensityContext);
}
export function useResolvedFloorRoomCardDensity(options) {
  var rootRef = options.rootRef,
    density = options.density,
    viewportScale = options.viewportScale;
  var _useState = useState('comfortable'),
    _useState2 = _slicedToArray(_useState, 2),
    resolvedAuto = _useState2[0],
    setResolvedAuto = _useState2[1];
  var prevAutoRef = useRef(null);
  var measureAndApply = useCallback(function () {
    var el = rootRef.current;
    if (!el) return;
    var w = el.offsetWidth;
    var h = el.offsetHeight;
    var vss = computeVisualShortSide(w, h, viewportScale);
    var next = applyDensityHysteresis(vss, prevAutoRef.current);
    prevAutoRef.current = next;
    setResolvedAuto(function (prev) {
      return prev === next ? prev : next;
    });
  }, [rootRef, viewportScale]);
  useLayoutEffect(function () {
    if (density !== 'auto') return;
    measureAndApply();
  }, [density, measureAndApply, viewportScale]);
  useEffect(function () {
    if (density !== 'auto') return;
    var el = rootRef.current;
    if (!el || typeof ResizeObserver === 'undefined') return;
    var ro = new ResizeObserver(function () {
      return measureAndApply();
    });
    ro.observe(el);
    return function () {
      return ro.disconnect();
    };
  }, [density, measureAndApply, rootRef]);
  if (density !== 'auto') {
    return density;
  }
  return resolvedAuto;
}