require("./chunk-9hOWP6kD.cjs");
let react = require("react");
let _tanstack_react_query = require("@tanstack/react-query");
let react_jsx_runtime = require("react/jsx-runtime");
//#region ../../shareables/core/src/query-keys.ts
function normalizeProductIds(ids) {
	return [...new Set(ids)].toSorted((a, b) => a - b);
}
const shareablesKeys = {
	media: {
		all: ["media"],
		list: (search, sortDesc, repContext, ownership, locale) => [
			"media",
			"list",
			search,
			sortDesc,
			repContext,
			ownership,
			locale
		],
		detail: (id, repContext, locale) => [
			"media",
			"detail",
			id,
			repContext,
			locale
		],
		detailById: (id) => [
			"media",
			"detail",
			id
		],
		grid: () => ["media", "grid"]
	},
	playlists: {
		all: ["playlists"],
		list: (search, sort, ownership) => [
			"playlists",
			"list",
			search,
			sort,
			ownership
		],
		detail: (id, locale) => locale === void 0 ? [
			"playlists",
			"detail",
			id
		] : [
			"playlists",
			"detail",
			id,
			locale
		],
		detailById: (id) => [
			"playlists",
			"detail",
			id
		]
	},
	pages: {
		all: ["pages"],
		list: (search, sort, locale) => [
			"pages",
			"list",
			search,
			sort,
			locale
		],
		detail: (id, locale) => [
			"pages",
			"detail",
			id,
			locale
		]
	},
	productMedia: {
		all: ["productMedia"],
		byProductIds: (ids) => [
			"productMedia",
			"byProductIds",
			normalizeProductIds(ids)
		],
		counts: (ids) => [
			"productMedia",
			"counts",
			normalizeProductIds(ids)
		],
		count: (id) => [
			"productMedia",
			"count",
			id
		],
		carouselProducts: (locale, maxProducts) => [
			"productMedia",
			"carouselProducts",
			locale,
			maxProducts
		]
	},
	mediaProducts: {
		all: ["mediaProducts"],
		list: (mediaId, locale) => [
			"mediaProducts",
			"list",
			mediaId,
			locale
		]
	},
	shareLinks: {
		all: ["shareLinks"],
		link: (type, id, contactId, locale) => [
			"shareLinks",
			"link",
			type,
			id,
			contactId,
			locale
		]
	}
};
//#endregion
//#region ../../shareables/core/src/shareables-api-context.tsx
const ShareablesApiContext = (0, react.createContext)(null);
function ShareablesApiProvider({ value, children }) {
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ShareablesApiContext.Provider, {
		value,
		children
	});
}
function useShareablesApi() {
	const ctx = (0, react.use)(ShareablesApiContext);
	if (!ctx) throw new Error("useShareablesApi must be used within a ShareablesApiProvider");
	return ctx;
}
//#endregion
//#region ../../shareables/core/src/hooks/use-share-link.ts
function useShareLink(item, shareableType, contactId, locale = "en") {
	const api = useShareablesApi();
	const queryClient = (0, _tanstack_react_query.useQueryClient)();
	const isIdRequired = shareableType !== "MySite";
	const isValidDatabaseId = item?.id && Number.isInteger(item.id) && item.id > 1;
	const shouldFetch = isIdRequired ? isValidDatabaseId : true;
	const queryKey = shareablesKeys.shareLinks.link(shareableType, item?.id, contactId, locale);
	const { data: shareLink, isLoading: loading, error, refetch } = (0, _tanstack_react_query.useQuery)({
		queryKey,
		queryFn: async () => {
			if (item?.share_link && !contactId) return item.share_link;
			if (isIdRequired && !item?.id) throw new Error("Something went wrong, please try again.");
			if (isIdRequired && !isValidDatabaseId) throw new Error("Share links are not available for this item.");
			const input = {
				locale,
				relateableType: shareableType,
				...isIdRequired && item?.id && { relateableId: item.id },
				...contactId && { contactId }
			};
			return await api.share.createShareLink(input);
		},
		enabled: isIdRequired ? Boolean(shouldFetch && item?.id) : true,
		staleTime: 300 * 1e3,
		gcTime: 600 * 1e3
	});
	return {
		loading,
		shareLink,
		getShareLink: (0, react.useCallback)(async () => {
			if (shareLink) return shareLink;
			return (await refetch()).data;
		}, [shareLink, refetch]),
		resetShareLink: (0, react.useCallback)(() => {
			queryClient.removeQueries({ queryKey });
		}, [queryClient, queryKey]),
		error
	};
}
//#endregion
Object.defineProperty(exports, "ShareablesApiProvider", {
	enumerable: true,
	get: function() {
		return ShareablesApiProvider;
	}
});
Object.defineProperty(exports, "shareablesKeys", {
	enumerable: true,
	get: function() {
		return shareablesKeys;
	}
});
Object.defineProperty(exports, "useShareLink", {
	enumerable: true,
	get: function() {
		return useShareLink;
	}
});
Object.defineProperty(exports, "useShareablesApi", {
	enumerable: true,
	get: function() {
		return useShareablesApi;
	}
});

//# sourceMappingURL=use-share-link-CRRJj2zA.cjs.map