{"version":3,"sources":["../../../src/hooks/useTelegramSubscriberLink.ts"],"sourcesContent":["import type {\n  TelegramSubscriberLinkOptions,\n  TelegramSubscriberLinkState,\n  TelegramSubscriberLinkStatus,\n} from '@novu/js';\nimport { TelegramSubscriberLink } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\n\nexport type UseTelegramSubscriberLinkProps = TelegramSubscriberLinkOptions;\n\nexport type UseTelegramSubscriberLinkResult = {\n  /** Current deep-link URL (`t.me/<bot>?start=<code>`), or `null` before the first issue. */\n  deepLinkUrl: string | null;\n  /** Telegram bot username (no `@`), or `null` before the first issue. */\n  botUsername: string | null;\n  /** Lifecycle status: `loading` while resolving initial state, `pending` while waiting for Start, `connected` once linked, `expired` briefly before auto-reissue. */\n  status: TelegramSubscriberLinkStatus;\n  /** Last error, if any. */\n  error: Error | null;\n  /** Re-issue a fresh deep link and restart polling. */\n  refresh: () => Promise<void>;\n};\n\n/**\n * React hook wrapping {@link TelegramSubscriberLink} from `@novu/js`.\n *\n * Issues a Telegram subscriber-link deep link, polls for connection, and\n * re-issues automatically on code expiry. Returns reactive state that\n * updates as the lifecycle progresses.\n *\n * **Important:** The underlying API endpoint requires `INTEGRATION_WRITE` permission.\n * Either pass `secretKey` for direct server-side usage, or provide a custom\n * `fetchFn` / `apiUrl` that routes through your backend proxy.\n *\n * @example\n * ```tsx\n * import { useTelegramSubscriberLink } from '@novu/react';\n *\n * function TelegramConnect() {\n *   const { deepLinkUrl, botUsername, status, refresh } = useTelegramSubscriberLink({\n *     apiUrl: '/api/novu-proxy',\n *     integrationIdentifier: '<telegram-integration-identifier>',\n *     subscriberId: 'user-42',\n *   });\n *\n *   if (status === 'connected') return <p>Connected!</p>;\n *\n *   return (\n *     <div>\n *       {deepLinkUrl && <a href={deepLinkUrl}>Open @{botUsername} in Telegram</a>}\n *       <button onClick={refresh}>Refresh link</button>\n *     </div>\n *   );\n * }\n * ```\n */\nexport function useTelegramSubscriberLink(props: UseTelegramSubscriberLinkProps): UseTelegramSubscriberLinkResult {\n  const [state, setState] = useState<TelegramSubscriberLinkState>({\n    status: 'loading',\n    deepLinkUrl: null,\n    botUsername: null,\n    expiresAt: null,\n    error: null,\n  });\n\n  const instanceRef = useRef<TelegramSubscriberLink | null>(null);\n\n  const { apiUrl, secretKey, integrationIdentifier, subscriberId, pollIntervalMs, fetchFn } = props;\n\n  // Keep the latest `fetchFn` in a ref so a new inline-function identity on every\n  // render does not re-trigger the effect (which would tear down and re-issue the\n  // link on a loop). The stable wrapper below reads from this ref.\n  const fetchFnRef = useRef(fetchFn);\n  fetchFnRef.current = fetchFn;\n\n  const stableFetchFn = useCallback<typeof fetch>((input, init) => {\n    const fn = fetchFnRef.current ?? fetch;\n\n    return fn(input, init);\n  }, []);\n\n  useEffect(() => {\n    const link = new TelegramSubscriberLink({\n      apiUrl,\n      secretKey,\n      integrationIdentifier,\n      subscriberId,\n      pollIntervalMs,\n      fetchFn: stableFetchFn,\n    });\n\n    instanceRef.current = link;\n    setState({ ...link.state });\n\n    const unsubscribe = link.onStateChange((next) => {\n      setState({ ...next });\n    });\n\n    void link.start();\n\n    return () => {\n      unsubscribe();\n      link.stop();\n      instanceRef.current = null;\n    };\n  }, [apiUrl, secretKey, integrationIdentifier, subscriberId, pollIntervalMs, stableFetchFn]);\n\n  const refresh = useCallback(async () => {\n    if (instanceRef.current) {\n      await instanceRef.current.refresh();\n    }\n  }, []);\n\n  return {\n    deepLinkUrl: state.deepLinkUrl,\n    botUsername: state.botUsername,\n    status: state.status,\n    error: state.error,\n    refresh,\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,gBAAuC;AACvC,mBAAyD;AAkDlD,SAAS,0BAA0B,OAAwE;AAChH,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAsC;AAAA,IAC9D,QAAQ;AAAA,IACR,aAAa;AAAA,IACb,aAAa;AAAA,IACb,WAAW;AAAA,IACX,OAAO;AAAA,E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