"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.checkNotificationPermission = checkNotificationPermission;
exports.disablePushNotifications = disablePushNotifications;
exports.getFCMToken = getFCMToken;
exports.getInitialNotification = getInitialNotification;
exports.initPushNotifications = initPushNotifications;
exports.onForegroundMessage = onForegroundMessage;
exports.onNotificationOpenedApp = onNotificationOpenedApp;
exports.requestNotificationPermission = requestNotificationPermission;
exports.setBackgroundMessageHandler = setBackgroundMessageHandler;
var _messaging = _interopRequireDefault(require("@react-native-firebase/messaging"));
var _app = require("@react-native-firebase/app");
var _reactNative = require("react-native");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
let appInstance;
try {
  appInstance = (0, _app.getApp)();
} catch (error) {}
const AuthorizationStatus = _messaging.default.AuthorizationStatus;
let cachedMessagingInstance = null;
const getMessagingInstance = () => {
  if (cachedMessagingInstance) {
    return cachedMessagingInstance;
  }
  if (!appInstance) {
    try {
      appInstance = (0, _app.getApp)();
    } catch (e) {}
  }
  cachedMessagingInstance = (0, _messaging.default)();
  return cachedMessagingInstance;
};
async function requestNotificationPermission() {
  const messagingInstance = getMessagingInstance();
  if (_reactNative.Platform.OS === 'ios') {
    const authStatus = await messagingInstance.requestPermission();
    return authStatus === AuthorizationStatus.AUTHORIZED || authStatus === AuthorizationStatus.PROVISIONAL;
  }
  if (_reactNative.Platform.OS === 'android' && _reactNative.Platform.Version >= 33) {
    const granted = await _reactNative.PermissionsAndroid.request(_reactNative.PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);
    return granted === _reactNative.PermissionsAndroid.RESULTS.GRANTED;
  }
  return true;
}
async function getFCMToken() {
  try {
    const messagingInstance = getMessagingInstance();
    if (!messagingInstance.isDeviceRegisteredForRemoteMessages) {
      await messagingInstance.registerDeviceForRemoteMessages();
    }
    if (_reactNative.Platform.OS === 'ios') {
      await new Promise(resolve => setTimeout(resolve, 2000));
      let apnsToken = await messagingInstance.getAPNSToken();
      if (!apnsToken) {
        await new Promise(resolve => setTimeout(resolve, 3000));
        apnsToken = await messagingInstance.getAPNSToken();
      }

      // No APNS token means push not configured - this is OK for development
      if (!apnsToken) {
        console.log('Push notifications not configured (missing APNs). App will work without push.');
        return null;
      }
    }
    const token = await messagingInstance.getToken();
    return token;
  } catch (error) {
    // Silently handle missing aps-environment (push not configured in Xcode)
    const errorMsg = error?.message || String(error);
    if (errorMsg.includes('aps-environment') || errorMsg.includes('messaging/unknown')) {
      console.log('Push notifications not configured in Xcode. App will work without push.');
      return null;
    }
    console.warn('Error getting FCM token:', error);
    return null;
  }
}
function onForegroundMessage(callback) {
  const messagingInstance = getMessagingInstance();
  return messagingInstance.onMessage(callback);
}
function setBackgroundMessageHandler(handler) {
  const messagingInstance = getMessagingInstance();
  messagingInstance.setBackgroundMessageHandler(handler);
}
function onNotificationOpenedApp(callback) {
  const messagingInstance = getMessagingInstance();
  return messagingInstance.onNotificationOpenedApp(callback);
}
async function getInitialNotification() {
  try {
    const messagingInstance = getMessagingInstance();
    const message = await messagingInstance.getInitialNotification();
    return message;
  } catch (error) {
    console.error('Error getting initial notification:', error);
    return null;
  }
}
async function initPushNotifications() {
  const hasPermission = await requestNotificationPermission();
  if (!hasPermission) {
    console.error('Push notification permission denied');
    return null;
  }
  const token = await getFCMToken();
  return token;
}
async function checkNotificationPermission() {
  try {
    const messagingInstance = getMessagingInstance();
    if (_reactNative.Platform.OS === 'ios') {
      try {
        const apnsToken = await messagingInstance.getAPNSToken();
        return apnsToken !== null;
      } catch (error) {
        return false;
      }
    }
    if (_reactNative.Platform.OS === 'android' && _reactNative.Platform.Version >= 33) {
      const granted = await _reactNative.PermissionsAndroid.check(_reactNative.PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);
      return granted;
    }
    return true;
  } catch (error) {
    console.warn('Error checking notification permission:', error);
    return false;
  }
}
async function disablePushNotifications() {
  try {
    const messagingInstance = getMessagingInstance();
    await messagingInstance.deleteToken();
    console.log('FCM token deleted successfully');
  } catch (error) {
    console.warn('Error deleting FCM token:', error);
  }
}
//# sourceMappingURL=pushNotifications.js.map