require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_reservationConstants = require("../reservationConstants.js");
const require_resourceWallLocalStorage = require("../data/resourceWallLocalStorage.js");
let react = require("react");
//#region src/plus/pisellReservation/hooks/useReservationResourceWallPersist.ts
/**
* 资源墙：首屏读盘 state、localStorage 水合 / id 剪枝 / 筛参落盘（三 effect 顺序与 PisellReservation 旧版一致）。
*/
function useReservationResourceWallPersist({ bodyViewStorageKey, isInternalListData, searchParams, onSearch, venueWallCards, floorDayBookingLoading }) {
	const resourceWallFilterHydratedRef = (0, react.useRef)(false);
	const [venueWallBoot] = (0, react.useState)(() => require_resourceWallLocalStorage.readResourceWallPersistedSliceFromDisk(bodyViewStorageKey));
	const [venueWallAppearanceTheme, setVenueWallAppearanceTheme] = (0, react.useState)(venueWallBoot.venueWallAppearanceTheme);
	const [venueWallAppearanceMode, setVenueWallAppearanceMode] = (0, react.useState)(venueWallBoot.venueWallAppearanceMode);
	const [venueWallResourceLayout, setVenueWallResourceLayout] = (0, react.useState)(venueWallBoot.venueWallResourceLayout);
	const resourceWallFilterStorageKey = `${require_reservationConstants.PISELL_RESERVATION_RESOURCE_WALL_FILTER_STORAGE_PREFIX}:${bodyViewStorageKey !== null && bodyViewStorageKey !== void 0 ? bodyViewStorageKey : "default"}`;
	/** 大屏资源多选：localStorage v1 恢复（id 与勾选一致，不与画布/日历列表求交），并持久化变更（不上传独立接口） */
	(0, react.useEffect)(() => {
		var _lsParsed$resourceIds;
		if (!isInternalListData) return;
		if (resourceWallFilterHydratedRef.current) return;
		const fv = searchParams === null || searchParams === void 0 ? void 0 : searchParams.filter;
		const values = fv && typeof fv === "object" && fv !== null && typeof fv.values === "object" && fv.values !== null ? fv.values : {};
		const whitelist = require_reservationConstants.RESERVATION_RESOURCE_WALL_PERSISTED_FILTER_VALUE_KEYS;
		let lsParsed = { filterValuePatches: {} };
		try {
			lsParsed = require_resourceWallLocalStorage.parseResourceWallLocalStorage(localStorage.getItem(resourceWallFilterStorageKey), whitelist);
		} catch (_unused) {}
		if ((lsParsed === null || lsParsed === void 0 ? void 0 : lsParsed.venueWallAppearanceTheme) != null && require_resourceWallLocalStorage.isVenueWallAppearanceThemeNonEmpty(lsParsed.venueWallAppearanceTheme)) setVenueWallAppearanceTheme(lsParsed.venueWallAppearanceTheme);
		if (lsParsed.venueWallAppearanceMode === "light" || lsParsed.venueWallAppearanceMode === "dark") setVenueWallAppearanceMode(lsParsed.venueWallAppearanceMode);
		if (lsParsed.venueWallResourceLayout != null) setVenueWallResourceLayout(lsParsed.venueWallResourceLayout);
		const existing = values.resource_wall_resource_ids;
		if (Array.isArray(existing) && existing.length > 0) {
			const hasPatches = Object.keys(lsParsed.filterValuePatches).length > 0;
			if (onSearch && hasPatches) onSearch(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, searchParams), {}, { filter: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, typeof searchParams.filter === "object" && searchParams.filter !== null ? searchParams.filter : {}), {}, { values: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, typeof values === "object" ? values : {}), lsParsed.filterValuePatches), {}, { resource_wall_resource_ids: existing.map(String) }) }) }));
			resourceWallFilterHydratedRef.current = true;
			return;
		}
		const explicitRid = lsParsed.resourceWallResourceIdsInFile === true;
		const idsFromLs = (_lsParsed$resourceIds = lsParsed.resourceIds) !== null && _lsParsed$resourceIds !== void 0 ? _lsParsed$resourceIds : [];
		const finalList = explicitRid ? require_resourceWallLocalStorage.dedupeResourceWallIdsPreserveOrder(idsFromLs) : [];
		resourceWallFilterHydratedRef.current = true;
		if (!onSearch) return;
		onSearch(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, searchParams), {}, { filter: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, typeof searchParams.filter === "object" && searchParams.filter !== null ? searchParams.filter : {}), {}, { values: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, typeof values === "object" ? values : {}), lsParsed.filterValuePatches), {}, { resource_wall_resource_ids: finalList }) }) }));
	}, [
		isInternalListData,
		resourceWallFilterStorageKey,
		onSearch,
		searchParams
	]);
	(0, react.useEffect)(() => {
		if (!isInternalListData) return;
		if (!resourceWallFilterHydratedRef.current) return;
		if (floorDayBookingLoading) return;
		if (venueWallCards.length === 0) return;
		const fv = searchParams === null || searchParams === void 0 ? void 0 : searchParams.filter;
		if (!fv || typeof fv !== "object" || fv === null) return;
		const vals = fv.values;
		if (!vals || typeof vals !== "object" || Array.isArray(vals)) return;
		const existingRaw = vals.resource_wall_resource_ids;
		if (!Array.isArray(existingRaw)) return;
		const existingIds = existingRaw.map((x) => String(x));
		const validIdSet = new Set(venueWallCards.map((c) => String(c.resourceId)));
		const prunedIds = existingIds.filter((id) => validIdSet.has(id));
		if (prunedIds.length === existingIds.length) return;
		onSearch === null || onSearch === void 0 || onSearch(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, searchParams), {}, { filter: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, fv), {}, { values: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, vals), {}, { resource_wall_resource_ids: prunedIds }) }) }));
	}, [
		isInternalListData,
		venueWallCards,
		floorDayBookingLoading,
		searchParams,
		onSearch
	]);
	(0, react.useEffect)(() => {
		if (!isInternalListData) return;
		const fv = searchParams === null || searchParams === void 0 ? void 0 : searchParams.filter;
		if (!fv || typeof fv !== "object" || fv === null || typeof fv.values !== "object" || fv.values === null) return;
		const vals = fv.values;
		const rid = vals.resource_wall_resource_ids;
		if (!Array.isArray(rid)) return;
		const ridStable = require_resourceWallLocalStorage.dedupeResourceWallIdsPreserveOrder(rid.map(String));
		const filter_values = {};
		for (const k of require_reservationConstants.RESERVATION_RESOURCE_WALL_PERSISTED_FILTER_VALUE_KEYS) {
			if (k === "resource_wall_resource_ids") continue;
			if (Object.prototype.hasOwnProperty.call(vals, k)) filter_values[k] = vals[k];
		}
		const pack = {
			v: 1,
			resource_wall_resource_ids: ridStable
		};
		if (Object.keys(filter_values).length > 0) pack.filter_values = filter_values;
		if (require_resourceWallLocalStorage.isVenueWallAppearanceThemeNonEmpty(venueWallAppearanceTheme)) pack.venueWallAppearanceTheme = venueWallAppearanceTheme;
		pack.venueWallAppearanceMode = venueWallAppearanceMode;
		pack.venueWallResourceLayout = venueWallResourceLayout;
		try {
			localStorage.setItem(resourceWallFilterStorageKey, JSON.stringify(pack));
		} catch (_unused2) {}
	}, [
		isInternalListData,
		searchParams,
		resourceWallFilterStorageKey,
		venueWallAppearanceTheme,
		venueWallAppearanceMode,
		venueWallResourceLayout
	]);
	return {
		venueWallAppearanceTheme,
		setVenueWallAppearanceTheme,
		venueWallAppearanceMode,
		setVenueWallAppearanceMode,
		venueWallResourceLayout,
		setVenueWallResourceLayout
	};
}
//#endregion
exports.useReservationResourceWallPersist = useReservationResourceWallPersist;
