{"version":3,"file":"use-share-link-mOe2vbOG.mjs","names":[],"sources":["../../../shareables/core/src/query-keys.ts","../../../shareables/core/src/shareables-api-context.tsx","../../../shareables/core/src/hooks/use-share-link.ts"],"sourcesContent":["function normalizeProductIds(ids: number[]): number[] {\n  return [...new Set(ids)].toSorted((a, b) => a - b);\n}\n\nexport const shareablesKeys = {\n  media: {\n    all: [\"media\"] as const,\n    list: (\n      search?: string,\n      sortDesc?: boolean,\n      repContext?: boolean,\n      ownership?: string,\n      locale?: string,\n    ) =>\n      [\n        \"media\",\n        \"list\",\n        search,\n        sortDesc,\n        repContext,\n        ownership,\n        locale,\n      ] as const,\n    detail: (id: number, repContext?: boolean, locale?: string) =>\n      [\"media\", \"detail\", id, repContext, locale] as const,\n    /**\n     * Prefix that matches every detail cache entry for `id`, regardless of\n     * `repContext` or `locale`. Use with `invalidateQueries` / `removeQueries`\n     * when a single mutation should clear all variants.\n     */\n    detailById: (id: number) => [\"media\", \"detail\", id] as const,\n    grid: () => [\"media\", \"grid\"] as const,\n  },\n  playlists: {\n    all: [\"playlists\"] as const,\n    list: (search?: string, sort?: string, ownership?: string) =>\n      [\"playlists\", \"list\", search, sort, ownership] as const,\n    detail: (id: number, locale?: string) =>\n      locale === undefined\n        ? ([\"playlists\", \"detail\", id] as const)\n        : ([\"playlists\", \"detail\", id, locale] as const),\n    /**\n     * Prefix that matches every detail cache entry for `id`, regardless of\n     * locale. Use with prefix-based query operations when a mutation should\n     * update all localized variants.\n     */\n    detailById: (id: number) => [\"playlists\", \"detail\", id] as const,\n  },\n  pages: {\n    all: [\"pages\"] as const,\n    list: (search?: string, sort?: string, locale?: string) =>\n      [\"pages\", \"list\", search, sort, locale] as const,\n    detail: (id: number, locale?: string) =>\n      [\"pages\", \"detail\", id, locale] as const,\n  },\n  productMedia: {\n    all: [\"productMedia\"] as const,\n    byProductIds: (ids: number[]) =>\n      [\"productMedia\", \"byProductIds\", normalizeProductIds(ids)] as const,\n    counts: (ids: number[]) =>\n      [\"productMedia\", \"counts\", normalizeProductIds(ids)] as const,\n    count: (id: number) => [\"productMedia\", \"count\", id] as const,\n    carouselProducts: (locale: string, maxProducts: number) =>\n      [\"productMedia\", \"carouselProducts\", locale, maxProducts] as const,\n  },\n  mediaProducts: {\n    all: [\"mediaProducts\"] as const,\n    list: (mediaId: number, locale?: string) =>\n      [\"mediaProducts\", \"list\", mediaId, locale] as const,\n  },\n  shareLinks: {\n    all: [\"shareLinks\"] as const,\n    link: (type: string, id?: number, contactId?: number, locale?: string) =>\n      [\"shareLinks\", \"link\", type, id, contactId, locale] as const,\n  },\n};\n","import { createContext, use } from \"react\";\nimport type { ContentDomainApi } from \"./content-domain-api\";\n\nconst ShareablesApiContext = createContext<ContentDomainApi | null>(null);\n\nexport function ShareablesApiProvider({\n  value,\n  children,\n}: {\n  value: ContentDomainApi;\n  children: React.ReactNode;\n}): React.JSX.Element {\n  return (\n    <ShareablesApiContext.Provider value={value}>\n      {children}\n    </ShareablesApiContext.Provider>\n  );\n}\n\nexport function useShareablesApi(): ContentDomainApi {\n  const ctx = use(ShareablesApiContext);\n  if (!ctx) {\n    throw new Error(\n      \"useShareablesApi must be used within a ShareablesApiProvider\",\n    );\n  }\n  return ctx;\n}\n","import { useCallback } from \"react\";\nimport { useQuery, useQueryClient } from \"@tanstack/react-query\";\nimport type { shareables } from \"../types\";\nimport { useShareablesApi } from \"../shareables-api-context\";\nimport { shareablesKeys } from \"../query-keys\";\n\nexport interface ShareLinkItem {\n  id?: number;\n  share_link?: string;\n}\n\nexport function useShareLink(\n  item: ShareLinkItem,\n  shareableType: string,\n  contactId?: number,\n  locale = \"en\",\n) {\n  const api = useShareablesApi();\n  const queryClient = useQueryClient();\n\n  // Check if we need an ID for this shareable type\n  const isIdRequired = shareableType !== \"MySite\";\n\n  // Check if the ID is a valid database ID (integer) vs a generated float ID.\n  // `> 0`, not `> 1`: Number.isInteger already excludes the synthetic\n  // float ids this guard exists for, so the extra bound only rejected\n  // the FIRST row of a table. Postgres sequences start at 1, so id 1 is\n  // an ordinary record — and for it the query stayed disabled and\n  // refetch threw \"Share links are not available for this item.\"\n  // forever, with Retry unable to change the outcome.\n  const isValidDatabaseId =\n    item?.id && Number.isInteger(item.id) && item.id > 0;\n\n  const shouldFetch = isIdRequired ? isValidDatabaseId : true;\n\n  const queryKey = shareablesKeys.shareLinks.link(\n    shareableType,\n    item?.id,\n    contactId,\n    locale,\n  );\n\n  const {\n    data: shareLink,\n    isLoading: loading,\n    error,\n    refetch,\n  } = useQuery({\n    queryKey,\n    queryFn: async () => {\n      // If we have an existing share_link, return it\n      if (item?.share_link && !contactId) {\n        return item.share_link;\n      }\n\n      if (isIdRequired && !item?.id) {\n        throw new Error(\"Something went wrong, please try again.\");\n      }\n\n      if (isIdRequired && !isValidDatabaseId) {\n        throw new Error(\"Share links are not available for this item.\");\n      }\n\n      const input: shareables.CreateShareLinkInput = {\n        locale,\n        relateableType: shareableType,\n        ...(isIdRequired && item?.id && { relateableId: item.id }),\n        ...(contactId && { contactId }),\n      };\n\n      const link = await api.share.createShareLink(input);\n      return link;\n    },\n    enabled: isIdRequired ? Boolean(shouldFetch && item?.id) : true,\n    staleTime: 5 * 60 * 1000,\n    gcTime: 10 * 60 * 1000,\n  });\n\n  const getShareLink = useCallback(async () => {\n    if (shareLink) return shareLink;\n    const result = await refetch();\n    return result.data;\n  }, [shareLink, refetch]);\n\n  const resetShareLink = useCallback(() => {\n    queryClient.removeQueries({ queryKey });\n  }, [queryClient, queryKey]);\n\n  return {\n    loading,\n    shareLink,\n    getShareLink,\n    resetShareLink,\n    error,\n  };\n}\n"],"mappings":";;;;AAAA,SAAS,oBAAoB,KAAyB;AACpD,QAAO,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,CAAC,UAAU,GAAG,MAAM,IAAI,EAAE;;AAGpD,MAAa,iBAAiB;CAC5B,OAAO;EACL,KAAK,CAAC,QAAQ;EACd,OACE,QACA,UACA,YACA,WACA,WAEA;GACE;GACA;GACA;GACA;GACA;GAC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