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/hooks/useReservationResourceWallPersist.ts
var useReservationResourceWallPersist_exports = {};
__export(useReservationResourceWallPersist_exports, {
  useReservationResourceWallPersist: () => useReservationResourceWallPersist
});
module.exports = __toCommonJS(useReservationResourceWallPersist_exports);
var import_react = require("react");
var import_resourceWallLocalStorage = require("../data/resourceWallLocalStorage");
var import_reservationConstants = require("../reservationConstants");
function useReservationResourceWallPersist({
  bodyViewStorageKey,
  isInternalListData,
  searchParams,
  onSearch,
  venueWallCards,
  floorDayBookingLoading
}) {
  const resourceWallFilterHydratedRef = (0, import_react.useRef)(false);
  const [venueWallBoot] = (0, import_react.useState)(
    () => (0, import_resourceWallLocalStorage.readResourceWallPersistedSliceFromDisk)(bodyViewStorageKey)
  );
  const [venueWallAppearanceTheme, setVenueWallAppearanceTheme] = (0, import_react.useState)(venueWallBoot.venueWallAppearanceTheme);
  const [venueWallAppearanceMode, setVenueWallAppearanceMode] = (0, import_react.useState)(venueWallBoot.venueWallAppearanceMode);
  const [venueWallResourceLayout, setVenueWallResourceLayout] = (0, import_react.useState)(
    venueWallBoot.venueWallResourceLayout
  );
  const resourceWallFilterStorageKey = `${import_reservationConstants.PISELL_RESERVATION_RESOURCE_WALL_FILTER_STORAGE_PREFIX}:${bodyViewStorageKey ?? "default"}`;
  (0, import_react.useEffect)(() => {
    if (!isInternalListData) return;
    if (resourceWallFilterHydratedRef.current) return;
    const fv = searchParams == null ? void 0 : searchParams.filter;
    const values = fv && typeof fv === "object" && fv !== null && typeof fv.values === "object" && fv.values !== null ? fv.values : {};
    const whitelist = import_reservationConstants.RESERVATION_RESOURCE_WALL_PERSISTED_FILTER_VALUE_KEYS;
    let lsParsed = {
      filterValuePatches: {}
    };
    try {
      const raw = localStorage.getItem(resourceWallFilterStorageKey);
      lsParsed = (0, import_resourceWallLocalStorage.parseResourceWallLocalStorage)(raw, whitelist);
    } catch {
    }
    if ((lsParsed == null ? void 0 : lsParsed.venueWallAppearanceTheme) != null && (0, import_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({
          ...searchParams,
          filter: {
            ...typeof searchParams.filter === "object" && searchParams.filter !== null ? searchParams.filter : {},
            values: {
              ...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 ?? [];
    const finalList = explicitRid ? (0, import_resourceWallLocalStorage.dedupeResourceWallIdsPreserveOrder)(idsFromLs) : [];
    resourceWallFilterHydratedRef.current = true;
    if (!onSearch) return;
    onSearch({
      ...searchParams,
      filter: {
        ...typeof searchParams.filter === "object" && searchParams.filter !== null ? searchParams.filter : {},
        values: {
          ...typeof values === "object" ? values : {},
          ...lsParsed.filterValuePatches,
          resource_wall_resource_ids: finalList
        }
      }
    });
  }, [isInternalListData, resourceWallFilterStorageKey, onSearch, searchParams]);
  (0, import_react.useEffect)(() => {
    if (!isInternalListData) return;
    if (!resourceWallFilterHydratedRef.current) return;
    if (floorDayBookingLoading) return;
    if (venueWallCards.length === 0) return;
    const fv = searchParams == null ? 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 ? void 0 : onSearch({
      ...searchParams,
      filter: {
        ...fv,
        values: {
          ...vals,
          resource_wall_resource_ids: prunedIds
        }
      }
    });
  }, [
    isInternalListData,
    venueWallCards,
    floorDayBookingLoading,
    searchParams,
    onSearch
  ]);
  (0, import_react.useEffect)(() => {
    if (!isInternalListData) return;
    const fv = searchParams == null ? 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 = (0, import_resourceWallLocalStorage.dedupeResourceWallIdsPreserveOrder)(rid.map(String));
    const filter_values = {};
    for (const k of import_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 ((0, import_resourceWallLocalStorage.isVenueWallAppearanceThemeNonEmpty)(venueWallAppearanceTheme)) {
      pack.venueWallAppearanceTheme = venueWallAppearanceTheme;
    }
    pack.venueWallAppearanceMode = venueWallAppearanceMode;
    pack.venueWallResourceLayout = venueWallResourceLayout;
    try {
      localStorage.setItem(
        resourceWallFilterStorageKey,
        JSON.stringify(pack)
      );
    } catch {
    }
  }, [
    isInternalListData,
    searchParams,
    resourceWallFilterStorageKey,
    venueWallAppearanceTheme,
    venueWallAppearanceMode,
    venueWallResourceLayout
  ]);
  return {
    venueWallAppearanceTheme,
    setVenueWallAppearanceTheme,
    venueWallAppearanceMode,
    setVenueWallAppearanceMode,
    venueWallResourceLayout,
    setVenueWallResourceLayout
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationResourceWallPersist
});
