{"version":3,"sources":["../../../src/hooks/useChannelEndpoint.ts"],"sourcesContent":["import type { ChannelEndpointResponse, NovuError } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { useNovu } from './NovuProvider';\n\nexport type UseChannelEndpointProps = {\n  identifier: string;\n  onSuccess?: (data: ChannelEndpointResponse | null) => void;\n  onError?: (error: NovuError) => void;\n};\n\nexport type UseChannelEndpointResult = {\n  endpoint?: ChannelEndpointResponse | null;\n  error?: NovuError;\n  isLoading: boolean;\n  isFetching: boolean;\n  refetch: () => Promise<void>;\n};\n\n/**\n * Get a channel endpoint by identifier.\n */\nexport const useChannelEndpoint = (props: UseChannelEndpointProps): UseChannelEndpointResult => {\n  const novu = useNovu();\n  const propsRef = useRef<UseChannelEndpointProps>(props);\n  propsRef.current = props;\n\n  const [endpoint, setEndpoint] = useState<ChannelEndpointResponse | null>();\n  const [error, setError] = useState<NovuError>();\n  const [isLoading, setIsLoading] = useState(true);\n  const [isFetching, setIsFetching] = useState(false);\n\n  const fetchEndpoint = useCallback(\n    async (options?: { refetch: boolean }) => {\n      const { identifier, onSuccess, onError } = propsRef.current;\n      if (options?.refetch) {\n        setError(undefined);\n        setIsLoading(true);\n      }\n\n      setIsFetching(true);\n\n      const response = await novu.channelEndpoints.get({ identifier });\n\n      if (response.error) {\n        setError(response.error as NovuError);\n        onError?.(response.error as NovuError);\n      } else if (response.data !== undefined) {\n        onSuccess?.(response.data);\n        setEndpoint(response.data);\n      }\n      setIsLoading(false);\n      setIsFetching(false);\n    },\n    [novu]\n  );\n\n  // biome-ignore lint/correctness/useExhaustiveDependencies: props.identifier is needed to re-run the effect and initial fetch when the identifier changes\n  useEffect(() => {\n    const cleanupGetPending = novu.on('channel-endpoint.get.pending', () => {\n      setIsFetching(true);\n    });\n\n    const cleanupGetResolved = novu.on('channel-endpoint.get.resolved', ({ data, error: resolvedError }) => {\n      const { onSuccess, onError } = propsRef.current;\n      if (resolvedError) {\n        setError(resolvedError as NovuError);\n        onError?.(resolvedError as NovuError);\n      } else {\n        setEndpoint((data as ChannelEndpointResponse) ?? null);\n        onSuccess?.((data as ChannelEndpointResponse) ?? null);\n      }\n      setIsFetching(false);\n    });\n\n    const cleanupCreateResolved = novu.on('channel-endpoint.create.resolved', ({ data }) => {\n      if (data) {\n        setEndpoint(data as ChannelEndpointResponse);\n        propsRef.current.onSuccess?.(data as ChannelEndpointResponse);\n      }\n      setIsFetching(false);\n    });\n\n    const cleanupDeleteResolved = novu.on('channel-endpoint.delete.resolved', ({ args }) => {\n      if (!args || args.identifier !== propsRef.current.identifier) {\n        return;\n      }\n      setEndpoint(null);\n      propsRef.current.onSuccess?.(null);\n      setIsFetching(false);\n    });\n\n    void fetchEndpoint({ refetch: true });\n\n    return () => {\n      cleanupGetPending();\n      cleanupGetResolved();\n      cleanupCreateResolved();\n      cleanupDeleteResolved();\n    };\n  }, [novu, fetchEndpoint, props.identifier]);\n\n  const refetch = useCallback(() => fetchEndpoint({ refetch: true }), [fetchEndpoint]);\n\n  return {\n    endpoint,\n    error,\n    isLoading,\n    isFetching,\n    refetch,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAyD;AACzD,0BAAwB;AAmBjB,IAAM,qBAAqB,CAAC,UAA6D;AAC9F,QAAM,WAAO,6BAAQ;AACrB,QAAM,eAAW,qBAAgC,KAAK;AACtD,WAAS,UAAU;AAEnB,QAAM,CAAC,UAAU,WAAW,QAAI,uBAAyC;AACzE,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAoB;AAC9C,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAS,IAAI;AAC/C,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,KAAK;AAElD,QAAM,oBAAgB;AAAA,IACpB,OAAO,YAAmC;AACxC,YAAM,EAAE,YAAY,WAAW,QAAQ,IAAI,SAAS;AACpD,UAAI,mCAAS,SAAS;AACpB,iBAAS,MAAS;AAClB,qBAA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