{"version":3,"sources":["../../../src/hooks/useCreateChannelEndpoint.ts"],"sourcesContent":["import type { ChannelEndpointResponse, CreateChannelEndpointArgs, NovuError } from '@novu/js';\nimport { useCallback, useRef, useState } from 'react';\nimport { useNovu } from './NovuProvider';\n\nexport type UseCreateChannelEndpointProps = {\n  onSuccess?: (data: ChannelEndpointResponse) => void;\n  onError?: (error: NovuError) => void;\n};\n\nexport type UseCreateChannelEndpointResult = {\n  isCreating: boolean;\n  error?: NovuError;\n  create: (args: CreateChannelEndpointArgs) => Promise<{\n    data?: ChannelEndpointResponse | undefined;\n    error?: NovuError | undefined;\n  }>;\n};\n\nexport const useCreateChannelEndpoint = (props: UseCreateChannelEndpointProps = {}): UseCreateChannelEndpointResult => {\n  const propsRef = useRef<UseCreateChannelEndpointProps>(props);\n  propsRef.current = props;\n  const novu = useNovu();\n  const [isCreating, setIsCreating] = useState(false);\n  const [error, setError] = useState<NovuError>();\n\n  const create = useCallback(\n    async (args: CreateChannelEndpointArgs) => {\n      const { onSuccess, onError } = propsRef.current;\n      setError(undefined);\n      setIsCreating(true);\n\n      const response = await novu.channelEndpoints.create(args);\n\n      setIsCreating(false);\n\n      if (response.error) {\n        setError(response.error as NovuError);\n        onError?.(response.error as NovuError);\n      } else if (response.data) {\n        onSuccess?.(response.data as ChannelEndpointResponse);\n      }\n\n      return response as { data?: ChannelEndpointResponse; error?: NovuError };\n    },\n    [novu]\n  );\n\n  return {\n    create,\n    isCreating,\n    error,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAA8C;AAC9C,0BAAwB;AAgBjB,IAAM,2BAA2B,CAAC,QAAuC,CAAC,MAAsC;AACrH,QAAM,eAAW,qBAAsC,KAAK;AAC5D,WAAS,UAAU;AACnB,QAAM,WAAO,6BAAQ;AACrB,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,KAAK;AAClD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAoB;AAE9C,QAAM,aAAS;AAAA,IACb,OAAO,SAAoC;AACzC,YAAM,EAAE,WAAW,QAAQ,IAAI,SAAS;AACxC,eAAS,MAAS;AAClB,oBAAc,IAAI;AAElB,YAAM,WAAW,MAAM,KAAK,iBAAiB,OAAO,IAAI;AAExD,oBAAc,KAAK;AAEnB,UAAI,SAAS,OAAO;AAClB,iBAAS,SAAS,KAAkB;AACpC,2CAAU,SAAS;AAAA,MACrB,WAAW,SAAS,MAAM;AACxB,+CAAY,SAAS;AAAA,MACvB;AAEA,aAAO;AAAA,IACT;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}