{"version":3,"sources":["../../../src/wallet/expo/background.ts"],"names":["contractPollProcessor","TaskManager","BackgroundTask","ExpoIndexerProvider","ExpoArkProvider","createTaskDependencies","runTasks","CONTRACT_POLL_TASK_TYPE","getRandomId"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFO,SAAS,wBAAA,CACZ,UACA,OAAA,EACI;AACJ,EAAA,MAAM;AAAA,IACF,SAAA;AAAA,IACA,gBAAA;AAAA,IACA,kBAAA;AAAA,IACA,UAAA,GAAa,CAACA,uCAAqB;AAAA,GACvC,GAAI,OAAA;AAEJ,EAAYC,sBAAA,CAAA,UAAA,CAAW,UAAU,YAAY;AACzC,IAAA,IAAI;AACA,MAAA,MAAM,MAAA,GAAS,MAAM,SAAA,CAAU,UAAA,EAAsC;AACrE,MAAA,IAAI,CAAC,MAAA,EAAQ;AAGT,QAAA,OAAsBC,yBAAA,CAAA,oBAAA,CAAqB,OAAA;AAAA,MAC/C;AAEA,MAAA,MAAM,eAAA,GAAkB,IAAIC,qCAAA,CAAoB,MAAA,CAAO,YAAY,CAAA;AACnE,MAAA,MAAM,WAAA,GAAc,IAAIC,iCAAA,CAAgB,MAAA,CAAO,YAAY,CAAA;AAE3D,MAAA,MAAM,OAAOC,wCAAA,CAAuB;AAAA,QAChC,gBAAA;AAAA,QACA,kBAAA;AAAA,QACA,eAAA;AAAA,QACA;AAAA,OACH,CAAA;AAED,MAAA,MAAMC,0BAAA,CAAS,SAAA,EAAW,UAAA,EAAY,IAAI,CAAA;AAG1C,MAAA,MAAM,OAAA,GAAU,MAAM,SAAA,CAAU,UAAA,EAAW;AAC3C,MAAA,IAAI,OAAA,CAAQ,SAAS,CAAA,EAAG;AACpB,QAAA,MAAM,SAAA,CAAU,mBAAmB,OAAA,CAAQ,GAAA,CAAI,CAAC,CAAA,KAAM,CAAA,CAAE,EAAE,CAAC,CAAA;AAAA,MAC/D;AAGA,MAAA,MAAM,QAAA,GAAW,MAAM,SAAA,CAAU,QAAA,CAASC,yCAAuB,CAAA;AACjE,MAAA,IAAI,QAAA,CAAS,WAAW,CAAA,EAAG;AACvB,QAAA,MAAM,IAAA,GAAiB;AAAA,UACnB,IAAIC,6BAAA,EAAY;AAAA,UAChB,IAAA,EAAMD,yCAAA;AAAA,UACN,MAAM,EAAC;AAAA,UACP,SAAA,EAAW,KAAK,GAAA;AAAI,SACxB;AACA,QAAA,MAAM,SAAA,CAAU,QAAQ,IAAI,CAAA;AAAA,MAChC;AAEA,MAAA,OAAsBL,yBAAA,CAAA,oBAAA,CAAqB,OAAA;AAAA,IAC/C,SAAS,KAAA,EAAO;AACZ,MAAA,OAAA,CAAQ,KAAA;AAAA,QACJ,mCAAA;AAAA,QACA,KAAA,YAAiB,KAAA,GAAQ,KAAA,CAAM,OAAA,GAAU;AAAA,OAC7C;AACA,MAAA,OAAsBA,yBAAA,CAAA,oBAAA,CAAqB,MAAA;AAAA,IAC/C;AAAA,EACJ,CAAC,CAAA;AACL;AAcA,eAAsB,0BAAA,CAClB,UACA,OAAA,EACa;AACb,EAAA,MAAqBA,4CAAkB,QAAA,EAAU;AAAA,IAC7C,eAAA,EAAiB,SAAS,eAAA,IAAmB;AAAA,GAChD,CAAA;AACL;AASA,eAAsB,6BAA6B,QAAA,EAAiC;AAChF,EAAA,MAAqBA,8CAAoB,QAAQ,CAAA;AACrD","file":"background.cjs","sourcesContent":["/**\n * Expo background task entrypoint — `@arkade-os/sdk/wallet/expo/background`.\n *\n * This subpath is the **only** module in the package that touches\n * `expo-task-manager` / `expo-background-task`. It is split out from\n * `/wallet/expo` on purpose: those packages have no web platform and\n * are declared as optional peer dependencies, so importing them from\n * `/wallet/expo` would regress react-native-web / Node consumers who\n * only need the foreground APIs.\n *\n * The imports are static (not lazy `require()`) because Metro's static\n * dependency collector cannot see modules hidden behind `__require()`\n * in the ESM build — without static imports the packages never enter\n * the bundle graph and resolution fails at runtime. See\n * https://github.com/arkade-os/ts-sdk/issues/486 for details.\n *\n * Consumers install the peers in their Expo app:\n *   npx expo install expo-task-manager expo-background-task\n */\nimport * as TaskManager from \"expo-task-manager\";\nimport * as BackgroundTask from \"expo-background-task\";\n\nimport type { WalletRepository } from \"../../repositories/walletRepository\";\nimport type { ContractRepository } from \"../../repositories/contractRepository\";\nimport type { AsyncStorageTaskQueue } from \"../../worker/expo/asyncStorageTaskQueue\";\nimport type { TaskProcessor } from \"../../worker/expo/taskRunner\";\nimport type { TaskItem } from \"../../worker/expo/taskQueue\";\nimport { runTasks, createTaskDependencies } from \"../../worker/expo/taskRunner\";\nimport { contractPollProcessor, CONTRACT_POLL_TASK_TYPE } from \"../../worker/expo/processors\";\nimport { ExpoArkProvider } from \"../../providers/expoArk\";\nimport { ExpoIndexerProvider } from \"../../providers/expoIndexer\";\nimport { getRandomId } from \"../utils\";\n\n// ── Persisted config ─────────────────────────────────────────────\n\n/**\n * Wallet parameters persisted by @see ExpoWallet.setup and read\n * by the background handler to reconstruct providers and `extendVtxo`\n * without a network call.\n */\nexport interface PersistedBackgroundConfig {\n    arkServerUrl: string;\n    pubkeyHex: string;\n    serverPubKeyHex: string;\n    exitTimelockValue: string;\n    exitTimelockType: \"blocks\" | \"seconds\";\n}\n\n// ── Public API ───────────────────────────────────────────────────\n\n/**\n * Options for @see defineExpoBackgroundTask.\n */\nexport interface DefineBackgroundTaskOptions {\n    /** AsyncStorage-backed queue (must match the one passed to ExpoWallet.setup). */\n    taskQueue: AsyncStorageTaskQueue;\n    /** Wallet repository (fresh instance is fine — connects to the same DB). */\n    walletRepository: WalletRepository;\n    /** Contract repository (fresh instance is fine — connects to the same DB). */\n    contractRepository: ContractRepository;\n    /** Processors to run. Defaults to `[contractPollProcessor]`. */\n    processors?: TaskProcessor[];\n}\n\n/**\n * Define the Expo background task handler.\n *\n * **Must be called at module/global scope** (before React mounts) so\n * Expo's TaskManager can resume the task on cold start.\n *\n * Pair with @see registerExpoBackgroundTask to activate the OS\n * scheduler — `ExpoWallet.setup()` no longer registers the task for\n * you.\n *\n * @example\n * ```ts\n * // App entry (e.g. _layout.tsx) — module scope\n * import { defineExpoBackgroundTask } from \"@arkade-os/sdk/wallet/expo/background\";\n * import { AsyncStorageTaskQueue } from \"@arkade-os/sdk/worker/expo\";\n * import AsyncStorage from \"@react-native-async-storage/async-storage\";\n *\n * const taskQueue = new AsyncStorageTaskQueue(AsyncStorage);\n * defineExpoBackgroundTask(\"ark-background-poll\", {\n *     taskQueue,\n *     walletRepository: new IndexedDBWalletRepository(),\n *     contractRepository: new IndexedDBContractRepository(),\n * });\n * ```\n */\nexport function defineExpoBackgroundTask(\n    taskName: string,\n    options: DefineBackgroundTaskOptions,\n): void {\n    const {\n        taskQueue,\n        walletRepository,\n        contractRepository,\n        processors = [contractPollProcessor],\n    } = options;\n\n    TaskManager.defineTask(taskName, async () => {\n        try {\n            const config = await taskQueue.loadConfig<PersistedBackgroundConfig>();\n            if (!config) {\n                // No config persisted yet — ExpoWallet.setup() hasn't run.\n                // Nothing to do.\n                return BackgroundTask.BackgroundTaskResult.Success;\n            }\n\n            const indexerProvider = new ExpoIndexerProvider(config.arkServerUrl);\n            const arkProvider = new ExpoArkProvider(config.arkServerUrl);\n\n            const deps = createTaskDependencies({\n                walletRepository,\n                contractRepository,\n                indexerProvider,\n                arkProvider,\n            });\n\n            await runTasks(taskQueue, processors, deps);\n\n            // Acknowledge outbox results (no foreground to consume them)\n            const results = await taskQueue.getResults();\n            if (results.length > 0) {\n                await taskQueue.acknowledgeResults(results.map((r) => r.id));\n            }\n\n            // Re-seed the contract-poll task for the next OS wake\n            const existing = await taskQueue.getTasks(CONTRACT_POLL_TASK_TYPE);\n            if (existing.length === 0) {\n                const task: TaskItem = {\n                    id: getRandomId(),\n                    type: CONTRACT_POLL_TASK_TYPE,\n                    data: {},\n                    createdAt: Date.now(),\n                };\n                await taskQueue.addTask(task);\n            }\n\n            return BackgroundTask.BackgroundTaskResult.Success;\n        } catch (error) {\n            console.error(\n                \"[ark-sdk] Background task failed:\",\n                error instanceof Error ? error.message : error,\n            );\n            return BackgroundTask.BackgroundTaskResult.Failed;\n        }\n    });\n}\n\n/**\n * Activate the OS-level background task scheduler.\n *\n * Call once after @see defineExpoBackgroundTask — typically right after\n * `ExpoWallet.setup()`. Safe to call again to update the interval.\n *\n * @param taskName - The task name passed to {@link defineExpoBackgroundTask}.\n * @param options.minimumInterval - Minimum interval in **minutes** (default\n *   15, the floor enforced by `expo-background-task`).\n *\n * @see https://docs.expo.dev/versions/latest/sdk/background-task/#backgroundtaskoptions\n */\nexport async function registerExpoBackgroundTask(\n    taskName: string,\n    options?: { minimumInterval?: number },\n): Promise<void> {\n    await BackgroundTask.registerTaskAsync(taskName, {\n        minimumInterval: options?.minimumInterval ?? 15,\n    });\n}\n\n/**\n * Unregister the background task from the OS scheduler.\n *\n * `ExpoWallet.dispose()` does **not** call this — the OS-level task\n * lifecycle is the consumer's responsibility, matching the explicit\n * `register` step.\n */\nexport async function unregisterExpoBackgroundTask(taskName: string): Promise<void> {\n    await BackgroundTask.unregisterTaskAsync(taskName);\n}\n"]}