{"version":3,"file":"periodic-sync.cjs","names":[],"sources":["../../src/sw/periodic-sync.ts"],"sourcesContent":["/**\n * Main-thread helper to register a Periodic Background Sync, the counterpart to\n * the `periodicsync` listener installed by `installBackgroundSync`. Chrome-only\n * and gated behind the `periodic-background-sync` permission plus a site\n * engagement heuristic; degrades to a no-op (returns `false`) everywhere else.\n */\n\ninterface PeriodicSyncManagerLike {\n    register(tag: string, options?: { minInterval: number }): Promise<void>;\n    unregister(tag: string): Promise<void>;\n}\n\n/** Options for {@link registerPeriodicSync}. */\nexport interface RegisterPeriodicSyncOptions {\n    /** The active service-worker registration. */\n    registration: ServiceWorkerRegistration;\n    /** Sync tag; must match `installBackgroundSync`'s `periodicSyncTag`. Default `tempest-bg-sync-periodic`. */\n    tag?: string;\n    /**\n     * Minimum interval between runs, in minutes. The browser treats this as a\n     * floor and may space runs out much further. Default `720` (12h).\n     */\n    minIntervalMinutes?: number;\n}\n\n/**\n * Request a Periodic Background Sync registration.\n *\n * Checks the `periodic-background-sync` permission first and only registers\n * when it is granted, so calling this unconditionally is safe. The browser\n * ultimately decides whether and how often the `periodicsync` event fires.\n *\n * @param options - The registration, tag and interval.\n * @returns `true` when the periodic sync was registered, else `false`\n *   (unsupported, permission denied, or registration threw).\n *\n * @example\n * const reg = await navigator.serviceWorker.ready;\n * await registerPeriodicSync({ registration: reg, minIntervalMinutes: 360 });\n */\nexport async function registerPeriodicSync(options: RegisterPeriodicSyncOptions): Promise<boolean> {\n    const manager = (options.registration as unknown as { periodicSync?: PeriodicSyncManagerLike })\n        .periodicSync;\n    if (!manager) return false;\n\n    try {\n        if (typeof navigator !== \"undefined\" && navigator.permissions?.query) {\n            const status = await navigator.permissions.query({\n                name: \"periodic-background-sync\" as PermissionName,\n            });\n            if (status.state !== \"granted\") return false;\n        }\n        const minInterval = (options.minIntervalMinutes ?? 720) * 60 * 1000;\n        await manager.register(options.tag ?? \"tempest-bg-sync-periodic\", { minInterval });\n        return true;\n    } catch {\n        return false;\n    }\n}\n"],"mappings":"AAwCA,eAAsB,EAAqB,EAAwD,CAC/F,IAAM,EAAW,EAAQ,aACpB,aACL,GAAI,CAAC,EAAS,MAAO,GAErB,GAAI,CACA,GAAI,OAAO,UAAc,KAAe,UAAU,aAAa,QAIvD,MAHiB,UAAU,YAAY,MAAM,CAC7C,KAAM,0BACV,CAAC,EAAA,CACU,QAAU,UAAW,MAAO,GAE3C,IAAM,GAAe,EAAQ,oBAAsB,KAAO,GAAK,IAE/D,OADA,MAAM,EAAQ,SAAS,EAAQ,KAAO,2BAA4B,CAAE,aAAY,CAAC,EAC1E,EACX,MAAQ,CACJ,MAAO,EACX,CACJ"}