import { b as private_env } from './shared-server-sSGG17Df.js';
import apn from '@parse/node-apn';
import { t as toErrorMessage } from './error-DDXB3duW.js';

class LibraryAPNsService {
  bundleId;
  configured = false;
  keyId;
  privateKey;
  provider = null;
  teamId;
  constructor() {
    this.keyId = private_env.APNS_KEY_ID;
    this.teamId = private_env.APNS_TEAM_ID;
    this.bundleId = private_env.APNS_BUNDLE_ID;
    this.privateKey = private_env.APNS_KEY;
    if (!this.keyId || !this.teamId || !this.bundleId || !this.privateKey) {
      console.error(
        "[apns] Missing required configuration (APNS_KEY_ID, APNS_TEAM_ID, APNS_BUNDLE_ID, or APNS_KEY)"
      );
      this.configured = false;
      return;
    }
    const production = private_env.APNS_PRODUCTION === "true";
    const options = {
      production,
      token: {
        key: this.privateKey,
        keyId: this.keyId,
        teamId: this.teamId
      }
    };
    try {
      this.provider = new apn.Provider(options);
      this.configured = true;
      console.log(`[apns] Provider initialized (${production ? "production" : "sandbox"} mode)`);
    } catch (error) {
      console.error("[apns] Failed to create provider:", toErrorMessage(error));
      this.configured = false;
    }
  }
  isConfigured() {
    return this.configured;
  }
  async sendNotification(deviceToken, payload) {
    if (!this.configured || !this.provider) {
      throw new Error("APNs service not configured properly");
    }
    if (!deviceToken || !payload) {
      throw new Error("Device token and payload are required");
    }
    const notification = new apn.Notification();
    notification.alert = {
      body: payload.body ?? payload.message ?? "",
      title: payload.title
    };
    notification.badge = payload.badge ?? 1;
    notification.sound = payload.sound ?? "default";
    notification.topic = this.bundleId;
    if (payload.category) {
      notification.aps.category = payload.category;
    }
    if (payload.data) {
      notification.payload = payload.data;
    }
    const result = await this.provider.send(notification, deviceToken);
    if (result.failed && result.failed.length > 0) {
      console.error(`[apns] Full failed result: ${JSON.stringify(result.failed[0])}`);
      const failedItem = result.failed[0];
      const rawReason = failedItem?.response?.reason ?? failedItem?.status ?? failedItem?.error;
      const reason = typeof rawReason === "string" ? rawReason : JSON.stringify(rawReason ?? failedItem);
      console.error(`[apns] Delivery failed: ${reason}`);
      return {
        error: reason,
        failed: result.failed?.length || 0,
        sent: result.sent?.length || 0,
        success: false
      };
    }
    if (result.sent && result.sent.length > 0) {
      return {
        details: result.sent,
        failed: 0,
        sent: result.sent.length,
        success: true
      };
    }
    return {
      details: [result],
      error: "Unexpected result format",
      failed: 1,
      sent: 0,
      success: false
    };
  }
  shutdown() {
    if (this.provider) {
      void this.provider.shutdown();
    }
  }
}

export { LibraryAPNsService as L };
//# sourceMappingURL=library-apns-BqJbvSKh.js.map
