require("../../../_virtual/_rolldown/runtime.js");
const require_reservationConstants = require("../reservationConstants.js");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellReservation/data/resourceWallLocalStorage.ts
/**
* 解析资源墙 localStorage：兼容旧版纯 `string[]`，或 v1 对象（资源 id、白名单 filter 片段、六态配色覆盖）。
*/
function sanitizeVenueWallResourceLayout(raw) {
	if (raw == null || typeof raw !== "object") return void 0;
	const o = raw;
	const clampInt = (v, min, max, d) => {
		const n = typeof v === "number" ? v : Number(v);
		if (!Number.isFinite(n)) return d;
		return Math.max(min, Math.min(max, Math.round(n)));
	};
	const heroRaw = typeof o.heroMaxWidthRatio === "number" ? o.heroMaxWidthRatio : Number(o.heroMaxWidthRatio);
	const heroMaxWidthRatio = Number.isFinite(heroRaw) ? Math.max(.4, Math.min(.8, heroRaw)) : _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST.heroMaxWidthRatio;
	return {
		layoutKey: typeof o.layoutKey === "string" && o.layoutKey.length > 0 ? o.layoutKey : _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST.layoutKey,
		customCols: clampInt(o.customCols, 1, 6, _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST.customCols),
		customRows: clampInt(o.customRows, 1, 6, _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST.customRows),
		pageDurationSec: clampInt(o.pageDurationSec, 3, 60, _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST.pageDurationSec),
		boardingSoonBeforeMin: clampInt(o.boardingSoonBeforeMin, 1, 60, _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST.boardingSoonBeforeMin),
		heroMaxWidthRatio
	};
}
/** 资源墙外观主题是否含有效数据（用于决定是否落盘 / 是否用磁盘值覆盖 state） */
function isVenueWallAppearanceThemeNonEmpty(t) {
	const slotHasData = (s) => {
		if (!s) return false;
		if (s.occupiedCardColor || s.availableCardColor) return true;
		const o = s.statusToneOverrides;
		return o != null && Object.keys(o).length > 0;
	};
	return slotHasData(t.light) || slotHasData(t.dark);
}
/** 资源墙多选 id：去重且保持首次出现顺序（与勾选 / 落盘顺序一致）。 */
function dedupeResourceWallIdsPreserveOrder(ids) {
	const seen = /* @__PURE__ */ new Set();
	const out = [];
	for (const raw of ids) {
		const t = String(raw).trim();
		if (!t || seen.has(t)) continue;
		seen.add(t);
		out.push(t);
	}
	return out;
}
function parseResourceWallLocalStorage(raw, whitelist) {
	const empty = { filterValuePatches: {} };
	if (!raw) return empty;
	try {
		const parsed = JSON.parse(raw);
		if (Array.isArray(parsed)) return {
			resourceIds: parsed.map(String),
			resourceWallResourceIdsInFile: true,
			filterValuePatches: {}
		};
		if (!parsed || typeof parsed !== "object") return empty;
		const o = parsed;
		const hasRidKey = Object.prototype.hasOwnProperty.call(o, "resource_wall_resource_ids");
		const ridRaw = o.resource_wall_resource_ids;
		const resourceIds = !hasRidKey ? void 0 : Array.isArray(ridRaw) ? ridRaw.map(String) : [];
		const patches = {};
		const fv = o.filter_values;
		if (fv && typeof fv === "object") for (const k of whitelist) {
			if (k === "resource_wall_resource_ids") continue;
			if (Object.prototype.hasOwnProperty.call(fv, k)) patches[k] = fv[k];
		}
		const legacyTones = o.venueWallStatusToneOverrides;
		const diskTheme = o.venueWallAppearanceTheme;
		let venueWallAppearanceTheme = diskTheme && typeof diskTheme === "object" ? diskTheme : void 0;
		if (!venueWallAppearanceTheme && legacyTones && typeof legacyTones === "object" && Object.keys(legacyTones).length > 0) venueWallAppearanceTheme = {
			light: { statusToneOverrides: legacyTones },
			dark: { statusToneOverrides: legacyTones }
		};
		const rawMode = o.venueWallAppearanceMode;
		const venueWallAppearanceMode = rawMode === "light" || rawMode === "dark" ? rawMode : void 0;
		const venueWallResourceLayout = sanitizeVenueWallResourceLayout(o.venueWallResourceLayout);
		return {
			resourceIds,
			resourceWallResourceIdsInFile: hasRidKey,
			filterValuePatches: patches,
			venueWallAppearanceTheme,
			venueWallAppearanceMode,
			venueWallResourceLayout
		};
	} catch (_unused) {
		return empty;
	}
}
/**
* 首屏同步读盘，供 `useState` 初值使用。
* 避免同一提交内「persist effect 先于 hydrate 的 setState 落盘」时用默认 layout 覆盖 localStorage 里已保存的版式。
*/
function readResourceWallPersistedSliceFromDisk(bodyViewStorageKey) {
	var _ls$venueWallResource;
	const key = `${require_reservationConstants.PISELL_RESERVATION_RESOURCE_WALL_FILTER_STORAGE_PREFIX}:${bodyViewStorageKey !== null && bodyViewStorageKey !== void 0 ? bodyViewStorageKey : "default"}`;
	let raw = null;
	try {
		if (typeof localStorage !== "undefined") raw = localStorage.getItem(key);
	} catch (_unused2) {
		raw = null;
	}
	const ls = parseResourceWallLocalStorage(raw, require_reservationConstants.RESERVATION_RESOURCE_WALL_PERSISTED_FILTER_VALUE_KEYS);
	return {
		venueWallAppearanceTheme: ls.venueWallAppearanceTheme != null && isVenueWallAppearanceThemeNonEmpty(ls.venueWallAppearanceTheme) ? ls.venueWallAppearanceTheme : {},
		venueWallAppearanceMode: ls.venueWallAppearanceMode === "light" || ls.venueWallAppearanceMode === "dark" ? ls.venueWallAppearanceMode : "light",
		venueWallResourceLayout: (_ls$venueWallResource = ls.venueWallResourceLayout) !== null && _ls$venueWallResource !== void 0 ? _ls$venueWallResource : _pisell_materials.DEFAULT_RESOURCE_WALL_LAYOUT_PERSIST
	};
}
//#endregion
exports.dedupeResourceWallIdsPreserveOrder = dedupeResourceWallIdsPreserveOrder;
exports.isVenueWallAppearanceThemeNonEmpty = isVenueWallAppearanceThemeNonEmpty;
exports.parseResourceWallLocalStorage = parseResourceWallLocalStorage;
exports.readResourceWallPersistedSliceFromDisk = readResourceWallPersistedSliceFromDisk;
