"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createRoomPresence = createRoomPresence;
var _client = require("@xmpp/client");
var _createTimeoutPromise = require("./createTimeoutPromise.xmpp");
function createRoomPresence(roomId, client) {
  let stanzaHdlrPointer;
  const unsubscribe = () => {
    client.off('stanza', stanzaHdlrPointer);
  };
  const responsePromise = new Promise((resolve, reject) => {
    stanzaHdlrPointer = stanza => {
      if (stanza.is('presence') && stanza.attrs.from.split('/')[0] === roomId) {
        const xEls = stanza.getChildren('x');
        if (xEls.length === 2) {
          const x = xEls.find(el => el.attrs.xmlns === 'http://jabber.org/protocol/muc#user');
          if (x) {
            const statuses = x.getChildren('status');
            if (!statuses) {
              unsubscribe();
              reject('!statuses');
            }
            const codes = statuses.map(el => el.attrs.code);
            if (codes.includes('201') && codes.includes('110')) {
              console.log('createRoomPresence:resolve true');
              unsubscribe();
              resolve(true);
            } else {
              console.log('createRoomPresence:reject, such room already exists');
              unsubscribe();
              resolve(true);
            }
          } else {
            unsubscribe();
            reject();
          }
        }
      }
    };
    client.on('stanza', stanzaHdlrPointer);
    const message = (0, _client.xml)('presence', {
      to: `${roomId}/${client.jid.getLocal()}`
    }, (0, _client.xml)('x', 'http://jabber.org/protocol/muc'));
    client.send(message);
  });
  const timeoutPromise = (0, _createTimeoutPromise.createTimeoutPromise)(2000, unsubscribe);
  return Promise.race([responsePromise, timeoutPromise]);
}
//# sourceMappingURL=createRoomPresence.xmpp.js.map