"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRoomsPaged = void 0;
var _client = require("@xmpp/client");
var _createTimeoutPromise = require("./createTimeoutPromise.xmpp");
const getRoomsPaged = async (client, maxResults = 3, before = null) => {
  let stanzaHdlrPointer;
  const unsubscribe = () => {
    client.off('stanza', stanzaHdlrPointer);
  };
  return new Promise(async (resolve, reject) => {
    stanzaHdlrPointer = stanza => {
      if (stanza.is('iq') && stanza.attrs.id === 'getUserRooms') {
        unsubscribe();
        resolve(stanza);
      }
    };
    client.on('stanza', stanzaHdlrPointer);
    const query = (0, _client.xml)('query', {
      xmlns: 'ns:getrooms'
    });
    const set = (0, _client.xml)('set', {
      xmlns: 'http://jabber.org/protocol/rsm'
    }, (0, _client.xml)('max', {}, maxResults.toString()));
    if (before) {
      set.append((0, _client.xml)('before', {}, before));
    }
    query.append(set);
    const message = (0, _client.xml)('iq', {
      type: 'get',
      from: client.jid?.toString(),
      id: 'getUserRoomsPaged'
    }, query);
    try {
      client.send(message);
    } catch (err) {
      console.error('Error sending getRooms request:', err);
      unsubscribe();
      reject(err);
    }
    await (0, _createTimeoutPromise.createTimeoutPromise)(2000, unsubscribe).catch(reject);
  });
};
exports.getRoomsPaged = getRoomsPaged;
//# sourceMappingURL=getRoomsPaged.xmpp.js.map