"use strict";

import { xml } from '@xmpp/client';
import { createTimeoutPromise } from './createTimeoutPromise.xmpp';
export 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 = xml('query', {
      xmlns: 'ns:getrooms'
    });
    const set = xml('set', {
      xmlns: 'http://jabber.org/protocol/rsm'
    }, xml('max', {}, maxResults.toString()));
    if (before) {
      set.append(xml('before', {}, before));
    }
    query.append(set);
    const message = 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 createTimeoutPromise(2000, unsubscribe).catch(reject);
  });
};
//# sourceMappingURL=getRoomsPaged.xmpp.js.map