"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.usePushNotifications = usePushNotifications;
var _react = require("react");
var _reactNative = require("react-native");
var _pushNotifications = require("../services/pushNotifications");
var _pushSubscriptionService = require("../services/pushSubscriptionService");
var _reactRedux = require("react-redux");
var _useLocalStorage = require("./useLocalStorage");
var _roomsSlice = require("../roomStore/roomsSlice");
function usePushNotifications() {
  const [fcmToken, setFcmToken] = (0, _react.useState)(null);
  const [notification, setNotification] = (0, _react.useState)(null);
  const {
    user,
    config
  } = (0, _reactRedux.useSelector)(state => state.chatSettingStore);
  const rooms = (0, _reactRedux.useSelector)(state => state.rooms.rooms);
  const dispatch = (0, _reactRedux.useDispatch)();
  const {
    set,
    get
  } = (0, _useLocalStorage.useLocalStorage)('fcmToken');
  const handleNotificationPress = remoteMessage => {
    console.log('[usePushNotifications] Notification pressed:', JSON.stringify(remoteMessage, null, 2));
    const jid = remoteMessage?.data?.jid || remoteMessage?.data?.chatJid || remoteMessage?.jid;
    if (jid) {
      dispatch((0, _roomsSlice.setPendingNotificationJid)(jid));
    } else {
      console.warn('No JID found in notification data:', remoteMessage?.data);
    }
  };
  const initToken = async () => {
    try {
      const token = await get();
      if (token) {
        setFcmToken(token);
        return;
      }
      const newToken = await (0, _pushNotifications.initPushNotifications)();
      setFcmToken(newToken);
      set(newToken);
    } catch (error) {
      console.error('Failed to init token:', error);
    }
  };
  (0, _react.useEffect)(() => {
    initToken();
    const unsubscribeForeground = (0, _pushNotifications.onForegroundMessage)(message => {
      setNotification(message);
      const {
        notification: notif,
        data
      } = message;
      if (notif) {
        _reactNative.Alert.alert(notif.title || 'Notification', notif.body || '', [{
          text: 'OK'
        }, {
          text: 'Open',
          onPress: () => handleNotificationPress(message)
        }]);
      }
    });
    const unsubscribeOpened = (0, _pushNotifications.onNotificationOpenedApp)(message => {
      console.log('[usePushNotifications] App opened from notification:', JSON.stringify(message, null, 2));
      setNotification(message);
      handleNotificationPress(message);
    });
    (0, _pushNotifications.getInitialNotification)().then(message => {
      if (message) {
        console.log('[usePushNotifications] Initial notification:', JSON.stringify(message, null, 2));
        setNotification(message);
        handleNotificationPress(message);
      }
    });
    return () => {
      unsubscribeForeground();
      unsubscribeOpened();
    };
  }, [rooms]);
  (0, _react.useEffect)(() => {
    if (fcmToken && user && user.xmppPassword && user.defaultWallet?.walletAddress) {
      _pushSubscriptionService.pushSubscriptionService.subscribeToPush(fcmToken, user, config?.projectName || '').catch(error => {
        console.error('Failed to subscribe to push:', error);
      });
    }
  }, [fcmToken, user?.defaultWallet?.walletAddress, user?.xmppPassword]);
  return {
    fcmToken,
    notification
  };
}
//# sourceMappingURL=usePushNotifications.js.map