import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
//#region src/plus/pisellReservation/floorRoomCard/floorRoomCardDensity.ts
/**
* 平面图 RoomCard：密度档位（布局像素 × viewportScale → visualShortSide）。
*/
/** `visualShortSide`（px）分档：comfortable 提高后，compact/mini 按比例由原 160/110/76 缩放约 180/160 */
const THRESH_COMFORTABLE = 180;
const THRESH_COMPACT = 124;
const THRESH_MINI = 86;
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 = 6;
	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;
	return Math.min(Number.isFinite(layoutW) && layoutW > 0 ? layoutW : 0, Number.isFinite(layoutH) && layoutH > 0 ? layoutH : 0) * s;
}
/**
* 根节点 `data-density`：方桌仅 **micro** → micro；圆桌 **mini / micro** → micro。
*/
function toFloorRoomCardUiDensity(d, shellShape = "rect") {
	if (d === "micro") return "micro";
	if (d === "mini") return shellShape === "circle" ? "micro" : "comfortable";
	return "comfortable";
}
/**
* Tooltip 极简小卡壳：方桌仅 **micro**；圆桌 **mini** 与 **micro**。
*/
function isFloorRoomCardTooltipDensity(d, shellShape = "rect") {
	if (d === "micro") return true;
	if (d === "mini") return shellShape === "circle";
	return false;
}
const FloorRoomCardDensityContext = createContext("comfortable");
function useFloorRoomCardDensity() {
	return useContext(FloorRoomCardDensityContext);
}
function useResolvedFloorRoomCardDensity(options) {
	const { rootRef, density, viewportScale } = options;
	const [resolvedAuto, setResolvedAuto] = useState("comfortable");
	const prevAutoRef = useRef(null);
	const measureAndApply = useCallback(() => {
		const el = rootRef.current;
		if (!el) return;
		const w = el.offsetWidth;
		const h = el.offsetHeight;
		const next = applyDensityHysteresis(computeVisualShortSide(w, h, viewportScale), prevAutoRef.current);
		prevAutoRef.current = next;
		setResolvedAuto((prev) => prev === next ? prev : next);
	}, [rootRef, viewportScale]);
	useLayoutEffect(() => {
		if (density !== "auto") return;
		measureAndApply();
	}, [
		density,
		measureAndApply,
		viewportScale
	]);
	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;
}
//#endregion
export { FloorRoomCardDensityContext, isFloorRoomCardTooltipDensity, toFloorRoomCardUiDensity, useFloorRoomCardDensity, useResolvedFloorRoomCardDensity };
