{"version":3,"file":"web-push-client.cjs","names":[],"sources":["../../src/push/web-push-client.ts"],"sourcesContent":["import { isPushSupported, urlBase64ToUint8Array } from \"./utils\";\n\nexport interface WebPushClientConfig {\n    /** VAPID public key (URL-safe base64). */\n    vapidPublicKey: string;\n    /**\n     * Persist the subscription on the backend. The SDK does not own this call —\n     * the host app decides the endpoint and how the payload is serialized.\n     */\n    onSubscribe: (subscription: PushSubscriptionJSON) => Promise<void> | void;\n    /** Remove the subscription from the backend. Called before `subscription.unsubscribe()`. */\n    onUnsubscribe?: (subscription: PushSubscriptionJSON) => Promise<void> | void;\n    /**\n     * Override how the service worker registration is obtained.\n     * Defaults to `navigator.serviceWorker.ready` — provide this only when you\n     * register the SW yourself and want to reuse the registration.\n     */\n    getRegistration?: () => Promise<ServiceWorkerRegistration>;\n}\n\nexport class WebPushUnsupportedError extends Error {\n    constructor() {\n        super(\"Web Push não suportado neste navegador.\");\n        this.name = \"WebPushUnsupportedError\";\n    }\n}\n\nexport class WebPushPermissionDeniedError extends Error {\n    constructor() {\n        super(\"Permissão de notificação negada pelo usuário.\");\n        this.name = \"WebPushPermissionDeniedError\";\n    }\n}\n\n/**\n * Browser-side Web Push helper. Wraps `Notification.requestPermission`,\n * `pushManager.subscribe`, and the corresponding teardown. Transport is up to\n * the caller via `onSubscribe` / `onUnsubscribe` callbacks.\n */\nexport class WebPushClient {\n    private readonly config: WebPushClientConfig;\n\n    constructor(config: WebPushClientConfig) {\n        this.config = config;\n    }\n\n    /** Whether the runtime supports the Push API. */\n    static isSupported(): boolean {\n        return isPushSupported();\n    }\n\n    private async registration(): Promise<ServiceWorkerRegistration> {\n        if (!isPushSupported()) throw new WebPushUnsupportedError();\n        return this.config.getRegistration\n            ? await this.config.getRegistration()\n            : await navigator.serviceWorker.ready;\n    }\n\n    /** Current `Notification.permission` value, or `\"unsupported\"`. */\n    permission(): NotificationPermission | \"unsupported\" {\n        if (typeof Notification === \"undefined\") return \"unsupported\";\n        return Notification.permission;\n    }\n\n    /**\n     * Ask the user for notification permission.\n     *\n     * @throws {WebPushUnsupportedError} If the runtime lacks the Notification API.\n     * @returns The resulting `NotificationPermission` value.\n     */\n    async requestPermission(): Promise<NotificationPermission> {\n        if (typeof Notification === \"undefined\") throw new WebPushUnsupportedError();\n        return Notification.requestPermission();\n    }\n\n    /** Return the active push subscription, if any. */\n    async getSubscription(): Promise<PushSubscription | null> {\n        const registration = await this.registration();\n        return registration.pushManager.getSubscription();\n    }\n\n    /** True when a subscription already exists for this browser. */\n    async isSubscribed(): Promise<boolean> {\n        if (!isPushSupported()) return false;\n        const existing = await this.getSubscription();\n        return existing !== null;\n    }\n\n    /**\n     * Subscribe the current device. Requests permission, creates a push\n     * subscription if none exists, and forwards the JSON payload to\n     * `onSubscribe` for backend persistence.\n     *\n     * @returns The active `PushSubscription`.\n     * @throws {WebPushUnsupportedError} If Web Push is unavailable.\n     * @throws {WebPushPermissionDeniedError} If the user denies the prompt.\n     */\n    async subscribe(): Promise<PushSubscription> {\n        const permission = await this.requestPermission();\n        if (permission !== \"granted\") throw new WebPushPermissionDeniedError();\n\n        const registration = await this.registration();\n        const existing = await registration.pushManager.getSubscription();\n        if (existing) {\n            await this.config.onSubscribe(existing.toJSON());\n            return existing;\n        }\n\n        const subscription = await registration.pushManager.subscribe({\n            userVisibleOnly: true,\n            applicationServerKey: urlBase64ToUint8Array(this.config.vapidPublicKey),\n        });\n        await this.config.onSubscribe(subscription.toJSON());\n        return subscription;\n    }\n\n    /**\n     * Cancel the current subscription. Calls `onUnsubscribe` first (so the\n     * backend can purge the record) and then `subscription.unsubscribe()`.\n     *\n     * @returns True when an active subscription was removed, false otherwise.\n     */\n    async unsubscribe(): Promise<boolean> {\n        const subscription = await this.getSubscription();\n        if (!subscription) return false;\n        if (this.config.onUnsubscribe) {\n            await this.config.onUnsubscribe(subscription.toJSON());\n        }\n        return subscription.unsubscribe();\n    }\n}\n"],"mappings":"+BAoBA,IAAa,EAAb,cAA6C,KAAM,CAC/C,aAAc,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