{"version":3,"sources":["../../src/direct/use-direct-vana-connect.ts"],"sourcesContent":["/**\n * React hook for the browser side of the direct Data Portability flow.\n *\n * @remarks\n * `useDirectVanaConnect` is a thin `useSyncExternalStore` binding over the\n * framework-agnostic {@link createDirectConnectFlow} store. The browser never\n * sees the app private key and never chooses scopes — it only calls the app's\n * own backend routes via the injected transports.\n *\n * This module is browser-safe and imports nothing Node-only. `react` is a peer\n * dependency.\n *\n * @category Direct\n * @module direct/use-direct-vana-connect\n */\n\nimport { useCallback, useMemo, useRef, useSyncExternalStore } from \"react\";\nimport {\n  createDirectConnectFlow,\n  type DirectConnectOptions,\n  type DirectConnectRetryOutcome,\n  type DirectConnectState,\n  type DirectConnectTransports,\n} from \"./connect-flow\";\n\n/** Options for {@link useDirectVanaConnect}: transports plus flow tunables. */\nexport type UseDirectVanaConnectOptions<T = unknown> =\n  DirectConnectTransports<T> & DirectConnectOptions;\n\n/** Return value of {@link useDirectVanaConnect}. */\nexport interface UseDirectVanaConnectResult<T = unknown> {\n  /** Current flow state (`state.type` is `\"idle\"` until `start()` is called). */\n  state: DirectConnectState<T>;\n  /** Begin the connect flow (create request, open Vana, poll, read). */\n  start: () => void;\n  /** Retry the read and report whether prior consent could be reused. */\n  retryRead: () => Promise<DirectConnectRetryOutcome>;\n  /** Reset back to `idle` and cancel any in-flight polling. */\n  reset: () => void;\n}\n\n/**\n * Drive the two-tab connect flow from a React component.\n *\n * @param options - The `createRequest`/`getStatus`/`readResult` transports plus\n * optional polling/timeout tunables.\n * @returns `{ state, start, reset }`.\n *\n * @example\n * ```tsx\n * const connect = useDirectVanaConnect({\n *   createRequest: () => fetch(\"/api/vana/request\", { method: \"POST\" }).then((r) => r.json()),\n *   getStatus: (id) => fetch(`/api/vana/status?requestId=${id}`).then((r) => r.json()),\n *   readResult: (id) => fetch(`/api/vana/data?requestId=${id}`).then((r) => r.json()),\n * });\n * return <button disabled={connect.state.type !== \"idle\"} onClick={connect.start}>Connect</button>;\n * ```\n */\nexport function useDirectVanaConnect<T = unknown>(\n  options: UseDirectVanaConnectOptions<T>,\n): UseDirectVanaConnectResult<T> {\n  // Keep the latest options in a ref so the store reads current callbacks\n  // without being recreated on every render.\n  const optionsRef = useRef(options);\n  optionsRef.current = options;\n\n  const flow = useMemo(\n    () =>\n      createDirectConnectFlow<T>(\n        {\n          createRequest: () => optionsRef.current.createRequest(),\n          getStatus: (id) => optionsRef.current.getStatus(id),\n          readResult: (id) => optionsRef.current.readResult(id),\n        },\n        {\n          get pollIntervalMs() {\n            return optionsRef.current.pollIntervalMs;\n          },\n          get timeoutMs() {\n            return optionsRef.current.timeoutMs;\n          },\n          get openApprovalWindow() {\n            return optionsRef.current.openApprovalWindow;\n          },\n          get browserPlatformPolicy() {\n            return optionsRef.current.browserPlatformPolicy;\n          },\n        },\n      ),\n    // Created once per component instance; callbacks are read via optionsRef.\n    [],\n  );\n\n  const state = useSyncExternalStore(\n    flow.subscribe,\n    flow.getState,\n    flow.getState,\n  );\n\n  const start = useCallback(() => {\n    void flow.start();\n  }, [flow]);\n\n  const reset = useCallback(() => {\n    flow.reset();\n  }, [flow]);\n\n  const retryRead = useCallback(() => flow.retryRead(), [flow]);\n\n  return { state, start, retryRead, reset };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBA,mBAAmE;AACnE,0BAMO;AAmCA,SAAS,qBACd,SAC+B;AAG/B,QAAM,iBAAa,qBAAO,OAAO;AACjC,aAAW,UAAU;AAErB,QAAM,WAAO;AAAA,IACX,UACE;AAAA,MACE;AAAA,QACE,eAAe,MAAM,WAAW,QAAQ,cAAc;AAAA,QACtD,WAAW,CAAC,OAAO,WAAW,QAAQ,UAAU,EAAE;AAAA,QAClD,YAAY,CAAC,OAAO,WAAW,QAAQ,WAAW,EAAE;AAAA,MACtD;AAAA,MACA;AAAA,QACE,IAAI,iBAAiB;AACnB,iBAAO,WAAW,QAAQ;AAAA,QAC5B;AAAA,QACA,IAAI,YAAY;AACd,iBAAO,WAAW,QAAQ;AAAA,QAC5B;AAAA,QACA,IAAI,qBAAqB;AACvB,iBAAO,WAAW,QAAQ;AAAA,QAC5B;AAAA,QACA,IAAI,wBAAwB;AAC1B,iBAAO,WAAW,QAAQ;AAAA,QAC5B;AAAA,MACF;AAAA,IACF;AAAA;AAAA,IAEF,CAAC;AAAA,EACH;AAEA,QAAM,YAAQ;AAAA,IACZ,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,EACP;AAEA,QAAM,YAAQ,0BAAY,MAAM;AAC9B,SAAK,KAAK,MAAM;AAAA,EAClB,GAAG,CAAC,IAAI,CAAC;AAET,QAAM,YAAQ,0BAAY,MAAM;AAC9B,SAAK,MAAM;AAAA,EACb,GAAG,CAAC,IAAI,CAAC;AAET,QAAM,gBAAY,0BAAY,MAAM,KAAK,UAAU,GAAG,CAAC,IAAI,CAAC;AAE5D,SAAO,EAAE,OAAO,OAAO,WAAW,MAAM;AAC1C;","names":[]}