/**
 * 平面图视口缓存：localStorage 读写、锁定态；供 useFloorMapTransformMetrics 使用。
 */

/** 平面图包围盒单侧默认外扩（像素）；过大会导致首屏大量空白 */
export var DEFAULT_BOUNDING_PADDING = 56;

/** 视口内与首屏对齐时，图元束左上与容器边缘的留白 */
export var VIEWPORT_TOP_LEFT_MARGIN = 16;

/** 平面图视口（缩放 + 平移）持久化到 localStorage 的 key 前缀 */
export var VIEWPORT_STORAGE_PREFIX = 'pisell-floor-map-layout.viewport:';

/** 视口锁定态持久化前缀（与视口位置分开存储） */
export var VIEWPORT_LOCK_STORAGE_PREFIX = 'pisell-floor-map-layout.viewport-lock:';

/** 视口持久化写入节流（避免拖动 / 缩放过程中高频写 localStorage） */
export var VIEWPORT_PERSIST_DEBOUNCE_MS = 300;

/** 判定当前 transform 是否处于「适配视口」结果时的容差 */
export var FIT_BOUNDS_MATCH_POS_EPSILON = 0.5;
export var FIT_BOUNDS_MATCH_SCALE_EPSILON = 0.005;
export function readPersistedViewport(key) {
  if (typeof window === 'undefined') return null;
  try {
    var raw = window.localStorage.getItem(VIEWPORT_STORAGE_PREFIX + key);
    if (!raw) return null;
    var parsed = JSON.parse(raw);
    if (!parsed || typeof parsed.x !== 'number' || typeof parsed.y !== 'number' || typeof parsed.scale !== 'number' || !Number.isFinite(parsed.x) || !Number.isFinite(parsed.y) || !Number.isFinite(parsed.scale) || parsed.scale <= 0) {
      return null;
    }
    return {
      x: parsed.x,
      y: parsed.y,
      scale: parsed.scale
    };
  } catch (_unused) {
    return null;
  }
}
export function writePersistedViewport(key, snapshot) {
  if (typeof window === 'undefined') return;
  try {
    window.localStorage.setItem(VIEWPORT_STORAGE_PREFIX + key, JSON.stringify(snapshot));
  } catch (_unused2) {
    /** 隐私模式 / quota 超限等情况静默失败 */
  }
}
export function readPersistedViewportLock(key) {
  if (typeof window === 'undefined') return false;
  try {
    return window.localStorage.getItem(VIEWPORT_LOCK_STORAGE_PREFIX + key) === '1';
  } catch (_unused3) {
    return false;
  }
}
export function writePersistedViewportLock(key, locked) {
  if (typeof window === 'undefined') return;
  try {
    if (locked) {
      window.localStorage.setItem(VIEWPORT_LOCK_STORAGE_PREFIX + key, '1');
    } else {
      window.localStorage.removeItem(VIEWPORT_LOCK_STORAGE_PREFIX + key);
    }
  } catch (_unused4) {
    /* noop */
  }
}