"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.usePendingNotification = usePendingNotification;
var _react = require("react");
var _reactRedux = require("react-redux");
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const PENDING_NOTIFICATION_JID_KEY = 'ethora_pending_notification_jid';
function usePendingNotification() {
  const dispatch = (0, _reactRedux.useDispatch)();
  const rooms = (0, _reactRedux.useSelector)(state => state.rooms.rooms);
  const pendingNotificationJid = (0, _reactRedux.useSelector)(state => state.rooms.pendingNotificationJid);
  const isLoading = (0, _reactRedux.useSelector)(state => state.rooms.isLoading);
  (0, _react.useEffect)(() => {
    const checkPendingNotification = async () => {
      if (isLoading) {
        return;
      }
      let jidToOpen = pendingNotificationJid;
      if (!jidToOpen) {
        try {
          const storedJid = await _asyncStorage.default.getItem(PENDING_NOTIFICATION_JID_KEY);
          if (storedJid) {
            jidToOpen = storedJid;
            await _asyncStorage.default.removeItem(PENDING_NOTIFICATION_JID_KEY);
          }
        } catch (error) {
          console.error('Error reading from AsyncStorage:', error);
        }
      }
      if (jidToOpen) {
        const room = rooms[jidToOpen];
        if (room) {
          return;
        }
      }
    };
    checkPendingNotification();
  }, [rooms, pendingNotificationJid, isLoading, dispatch]);
  return {
    pendingNotificationJid
  };
}
var _default = exports.default = usePendingNotification;
//# sourceMappingURL=usePendingNotification.js.map