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/plus/pisellReservation/floorRoomCard/floorRoomCardDensity.ts
var floorRoomCardDensity_exports = {};
__export(floorRoomCardDensity_exports, {
  FLOOR_ROOM_CARD_DENSITY_HYSTERESIS_PX: () => FLOOR_ROOM_CARD_DENSITY_HYSTERESIS_PX,
  FloorRoomCardDensityContext: () => FloorRoomCardDensityContext,
  applyDensityHysteresis: () => applyDensityHysteresis,
  computeVisualShortSide: () => computeVisualShortSide,
  isFloorRoomCardTooltipDensity: () => isFloorRoomCardTooltipDensity,
  pickDensityFromLayoutAndViewport: () => pickDensityFromLayoutAndViewport,
  pickDensityFromVisualShortSide: () => pickDensityFromVisualShortSide,
  toFloorRoomCardUiDensity: () => toFloorRoomCardUiDensity,
  useFloorRoomCardDensity: () => useFloorRoomCardDensity,
  useResolvedFloorRoomCardDensity: () => useResolvedFloorRoomCardDensity
});
module.exports = __toCommonJS(floorRoomCardDensity_exports);
var import_react = require("react");
var THRESH_COMFORTABLE = 180;
var THRESH_COMPACT = 124;
var THRESH_MINI = 86;
var FLOOR_ROOM_CARD_DENSITY_HYSTERESIS_PX = 6;
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;
  }
}
function applyDensityHysteresis(visualShortSide, prev) {
  const raw = pickDensityFromVisualShortSide(visualShortSide);
  const 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;
}
function computeVisualShortSide(layoutW, layoutH, viewportScale) {
  const s = Number.isFinite(viewportScale) && viewportScale > 0 ? viewportScale : 1;
  const w = Number.isFinite(layoutW) && layoutW > 0 ? layoutW : 0;
  const h = Number.isFinite(layoutH) && layoutH > 0 ? layoutH : 0;
  return Math.min(w, h) * s;
}
function pickDensityFromLayoutAndViewport(layoutW, layoutH, viewportScale) {
  return pickDensityFromVisualShortSide(
    computeVisualShortSide(layoutW, layoutH, viewportScale)
  );
}
function toFloorRoomCardUiDensity(d, shellShape = "rect") {
  if (d === "micro") return "micro";
  if (d === "mini") return shellShape === "circle" ? "micro" : "comfortable";
  return "comfortable";
}
function isFloorRoomCardTooltipDensity(d, shellShape = "rect") {
  if (d === "micro") return true;
  if (d === "mini") return shellShape === "circle";
  return false;
}
var FloorRoomCardDensityContext = (0, import_react.createContext)("comfortable");
function useFloorRoomCardDensity() {
  return (0, import_react.useContext)(FloorRoomCardDensityContext);
}
function useResolvedFloorRoomCardDensity(options) {
  const { rootRef, density, viewportScale } = options;
  const [resolvedAuto, setResolvedAuto] = (0, import_react.useState)("comfortable");
  const prevAutoRef = (0, import_react.useRef)(null);
  const measureAndApply = (0, import_react.useCallback)(() => {
    const el = rootRef.current;
    if (!el) return;
    const w = el.offsetWidth;
    const h = el.offsetHeight;
    const vss = computeVisualShortSide(w, h, viewportScale);
    const next = applyDensityHysteresis(vss, prevAutoRef.current);
    prevAutoRef.current = next;
    setResolvedAuto((prev) => prev === next ? prev : next);
  }, [rootRef, viewportScale]);
  (0, import_react.useLayoutEffect)(() => {
    if (density !== "auto") return;
    measureAndApply();
  }, [density, measureAndApply, viewportScale]);
  (0, import_react.useEffect)(() => {
    if (density !== "auto") return;
    const el = rootRef.current;
    if (!el || typeof ResizeObserver === "undefined") return;
    const ro = new ResizeObserver(() => measureAndApply());
    ro.observe(el);
    return () => ro.disconnect();
  }, [density, measureAndApply, rootRef]);
  if (density !== "auto") {
    return density;
  }
  return resolvedAuto;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FLOOR_ROOM_CARD_DENSITY_HYSTERESIS_PX,
  FloorRoomCardDensityContext,
  applyDensityHysteresis,
  computeVisualShortSide,
  isFloorRoomCardTooltipDensity,
  pickDensityFromLayoutAndViewport,
  pickDensityFromVisualShortSide,
  toFloorRoomCardUiDensity,
  useFloorRoomCardDensity,
  useResolvedFloorRoomCardDensity
});
