import { createContext, use } from "react";
import { useQuery } from "@tanstack/react-query";
//#region ../core/src/store-api-context.ts
const StoreApiContext = createContext(null);
const StoreApiProvider = StoreApiContext.Provider;
function useStoreApi() {
	const api = use(StoreApiContext);
	if (!api) throw new Error("useStoreApi must be used within a StoreApiProvider");
	return api;
}
/**
* Non-throwing variant for contexts where a StoreApiProvider may be absent
* (e.g. the screen-builder widget palette/drag-preview, which mounts only the
* widget preview providers). Returns null instead of throwing so callers can
* fall back to default copy.
*/
function useOptionalStoreApi() {
	return use(StoreApiContext);
}
//#endregion
//#region ../react/src/hooks/use-optional-store.ts
/** Shared cache key + staleTime for the tenant store query. */
const STORE_QUERY_KEY = ["portalTenant", "store"];
const STORE_STALE_TIME = 300 * 1e3;
/**
* Builds the tenant-store query fn. Shared by `useStore()` (sdk) and
* `useOptionalStore()` so the fetch + response mapping lives in one place.
*/
function fetchStoreQuery(api) {
	return async () => {
		return (await api.fetchStore()).store;
	};
}
/**
* Tenant-store query that tolerates a missing `StoreApiProvider` (e.g. the
* widget palette / drag-preview, which mounts widgets without one). Returns a
* disabled query instead of throwing, and shares the cache key + staleTime with
* `useStore()` so both resolve from the same cache entry.
*/
function useOptionalStore() {
	const api = useOptionalStoreApi();
	return useQuery({
		queryKey: STORE_QUERY_KEY,
		queryFn: fetchStoreQuery(api),
		enabled: api !== null,
		staleTime: STORE_STALE_TIME
	});
}
//#endregion
export { StoreApiProvider as a, useOptionalStore as i, STORE_STALE_TIME as n, useStoreApi as o, fetchStoreQuery as r, STORE_QUERY_KEY as t };

//# sourceMappingURL=use-optional-store-DpB_jK4Q.mjs.map