"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useQRCodeChat = exports.handleQRChatId = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const QR_CHAT_STORAGE_KEY = '@ethora/chat-component-qrChatId';
const handleQRChatId = async () => {
  try {
    const initialUrl = await _reactNative.Linking.getInitialURL();
    if (initialUrl) {
      const url = new URL(initialUrl);
      const qrChatId = new URLSearchParams(url.search).get('qrChatId');
      if (qrChatId) {
        await _asyncStorage.default.setItem(QR_CHAT_STORAGE_KEY, qrChatId);
      }
    }
  } catch (error) {
    console.error('Error handling QR chat ID in RN:', error);
  }
};
exports.handleQRChatId = handleQRChatId;
const useQRCodeChat = (setCurrentRoom, conferenceServer) => {
  const [wasAutoSelected, setWasAutoSelected] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    handleQRChatId();
  }, []);
  (0, _react.useEffect)(() => {
    const checkStoredQrId = async () => {
      try {
        const qrChatId = await _asyncStorage.default.getItem(QR_CHAT_STORAGE_KEY);
        if (qrChatId) {
          const roomJID = conferenceServer ? `${qrChatId}@${conferenceServer}` : `${qrChatId}@conference.xmpp.ethoradev.com`;
          setCurrentRoom({
            roomJID
          });
          await _asyncStorage.default.removeItem(QR_CHAT_STORAGE_KEY);
          setWasAutoSelected(true);
        }
      } catch (error) {
        console.error('Error using QR chat ID:', error);
      }
    };
    checkStoredQrId();
  }, [setCurrentRoom, conferenceServer]);
  return {
    wasAutoSelected
  };
};
exports.useQRCodeChat = useQRCodeChat;
//# sourceMappingURL=useQRCodeChatHandler.js.map