"use client";

// src/hooks/use-tenant-product-data.ts
import * as React2 from "react";

// src/lib/persisted-state.ts
import * as React from "react";
var EXXAT_STORAGE_PREFIX = "exxat-ds:";
var DEFAULT_DEBOUNCE_MS = 400;
var isBrowser = () => typeof window !== "undefined";
function getDriverImpl(driver) {
  if (!isBrowser()) return null;
  try {
    return driver === "session" ? window.sessionStorage : window.localStorage;
  } catch {
    return null;
  }
}
function namespacedKey(key) {
  return key.startsWith(EXXAT_STORAGE_PREFIX) ? key : `${EXXAT_STORAGE_PREFIX}${key}`;
}
function getStorageItem(key, driver = "local") {
  const store = getDriverImpl(driver);
  if (!store) return null;
  try {
    return store.getItem(namespacedKey(key));
  } catch {
    return null;
  }
}
function setStorageItem(key, value, driver = "local") {
  const store = getDriverImpl(driver);
  if (!store) return;
  try {
    store.setItem(namespacedKey(key), value);
  } catch {
  }
}
var debounceTimers = /* @__PURE__ */ new Map();
function scheduleStorageWrite(key, value, options = {}) {
  if (!isBrowser()) return;
  const { debounceMs = DEFAULT_DEBOUNCE_MS, driver = "local" } = options;
  const fullKey = namespacedKey(key);
  const prev = debounceTimers.get(fullKey);
  if (prev) clearTimeout(prev);
  const t = setTimeout(() => {
    debounceTimers.delete(fullKey);
    setStorageItem(key, value, driver);
  }, debounceMs);
  debounceTimers.set(fullKey, t);
}

// src/product-framework/tenant-products/types.ts
var TENANT_STORE_KEY = "tenant-store:v1";
var TENANT_STORE_VERSION = 1;
var TENANT_PRODUCT_DATA_VERSION = 1;
function emptyProductDataBag() {
  return {
    v: TENANT_PRODUCT_DATA_VERSION,
    hubs: {},
    entities: {},
    preferences: {},
    legacy: {},
    extensions: {}
  };
}
function emptyTenantStore() {
  return {
    v: TENANT_STORE_VERSION,
    catalog: [],
    products: {},
    meta: {}
  };
}

// src/product-framework/tenant-products/store.ts
function loadTenantStore() {
  const raw = getStorageItem(TENANT_STORE_KEY);
  if (!raw) return emptyTenantStore();
  try {
    const parsed = JSON.parse(raw);
    if (!parsed || typeof parsed !== "object") return emptyTenantStore();
    return {
      ...emptyTenantStore(),
      ...parsed,
      v: TENANT_STORE_VERSION,
      catalog: Array.isArray(parsed.catalog) ? parsed.catalog : [],
      products: parsed.products && typeof parsed.products === "object" ? parsed.products : {},
      meta: parsed.meta && typeof parsed.meta === "object" ? parsed.meta : {}
    };
  } catch {
    return emptyTenantStore();
  }
}
function saveTenantStore(store) {
  setStorageItem(TENANT_STORE_KEY, JSON.stringify({ ...store, v: TENANT_STORE_VERSION }));
}
function ensureProductDataBag(store, productId) {
  if (!store.products[productId]) {
    store.products[productId] = emptyProductDataBag();
  }
  return store.products[productId];
}

// src/product-framework/tenant-products/data.ts
function pathSegments(path) {
  return path.split(".").filter(Boolean);
}
function getAtPath(obj, path) {
  const parts = pathSegments(path);
  let cur = obj;
  for (const part of parts) {
    if (cur == null || typeof cur !== "object") return void 0;
    cur = cur[part];
  }
  return cur;
}
function setAtPath(obj, path, value) {
  const parts = pathSegments(path);
  if (parts.length === 0) return;
  let cur = obj;
  for (let i = 0; i < parts.length - 1; i++) {
    const part = parts[i];
    const next = cur[part];
    if (next == null || typeof next !== "object" || Array.isArray(next)) {
      cur[part] = {};
    }
    cur = cur[part];
  }
  cur[parts[parts.length - 1]] = value;
}
function getTenantProductData(productId, path, defaultValue) {
  const store = loadTenantStore();
  const bag = store.products[productId];
  if (!bag) return defaultValue;
  const root = path.startsWith("legacy.") ? bag.legacy : path.startsWith("hubs.") ? bag.hubs : path.startsWith("entities.") ? bag.entities : path.startsWith("preferences.") ? bag.preferences : path.startsWith("extensions.") ? bag.extensions : bag.extensions;
  const relative = path.startsWith("legacy.") || path.startsWith("hubs.") || path.startsWith("entities.") || path.startsWith("preferences.") || path.startsWith("extensions.") ? path.slice(path.indexOf(".") + 1) : path;
  const value = getAtPath(root, relative);
  return value === void 0 ? defaultValue : value;
}
function setTenantProductData(productId, path, value, options = {}) {
  const store = loadTenantStore();
  const bag = ensureProductDataBag(store, productId);
  if (path.startsWith("legacy.")) {
    setAtPath(bag.legacy, path.slice("legacy.".length), value);
  } else if (path.startsWith("hubs.")) {
    setAtPath(bag.hubs, path.slice("hubs.".length), value);
  } else if (path.startsWith("entities.")) {
    setAtPath(bag.entities, path.slice("entities.".length), value);
  } else if (path.startsWith("preferences.")) {
    setAtPath(bag.preferences, path.slice("preferences.".length), value);
  } else if (path.startsWith("extensions.")) {
    setAtPath(bag.extensions, path.slice("extensions.".length), value);
  } else {
    setAtPath(bag.extensions, path, value);
  }
  if (options.debounceMs !== void 0 && options.debounceMs > 0) {
    scheduleStorageWrite(TENANT_STORE_KEY, JSON.stringify(store), {
      debounceMs: options.debounceMs
    });
  } else {
    saveTenantStore(store);
  }
}

// src/hooks/use-tenant-product-data.ts
function useTenantProductData(productId, path, defaultValue) {
  const [value, setValue] = React2.useState(
    () => typeof window === "undefined" ? defaultValue : getTenantProductData(productId, path, defaultValue)
  );
  React2.useLayoutEffect(() => {
    setValue(getTenantProductData(productId, path, defaultValue));
  }, [productId, path, defaultValue]);
  const setPersisted = React2.useCallback(
    (next) => {
      setValue((prev) => {
        const resolved = typeof next === "function" ? next(prev) : next;
        setTenantProductData(productId, path, resolved);
        return resolved;
      });
    },
    [productId, path]
  );
  return [value, setPersisted];
}
export {
  useTenantProductData
};
//# sourceMappingURL=use-tenant-product-data.js.map