"use strict";

let currentClient = null;
let currentClientKey = '';
const initLocks = new Map();
const DEFAULT_DEV_SERVER = 'xmpp.chat.ethora.com';
export function buildXmppClientKey(username, xmppSettings) {
  const devServer = xmppSettings?.devServer || DEFAULT_DEV_SERVER;
  const host = xmppSettings?.host || '';
  const conference = xmppSettings?.conference || '';
  return `${username}|${devServer}|${host}|${conference}`;
}
export function isXmppClientReusable(client) {
  if (!client) {
    return false;
  }
  return client.status === 'online' || client.status === 'connecting';
}
export function setGlobalXmppClient(client, key) {
  currentClient = client;
  currentClientKey = client ? key || currentClientKey || '' : '';
}
export function getGlobalXmppClient() {
  return currentClient;
}
export function getGlobalXmppClientKey() {
  return currentClientKey;
}
export function getReusableXmppClientByKey(key) {
  if (!key || key !== currentClientKey) {
    return null;
  }
  if (!isXmppClientReusable(currentClient)) {
    return null;
  }
  return currentClient;
}
export async function withXmppClientInitLock(key, init) {
  const existing = initLocks.get(key);
  if (existing) {
    return existing;
  }
  const createdPromise = init().finally(() => {
    if (initLocks.get(key) === createdPromise) {
      initLocks.delete(key);
    }
  });
  initLocks.set(key, createdPromise);
  return createdPromise;
}
export function requireXmppClient() {
  if (!currentClient) {
    throw new Error('XMPP client is not initialized');
  }
  return currentClient;
}
export default {
  buildXmppClientKey,
  isXmppClientReusable,
  setGlobalXmppClient,
  getGlobalXmppClient,
  getGlobalXmppClientKey,
  getReusableXmppClientByKey,
  withXmppClientInitLock,
  requireXmppClient
};
//# sourceMappingURL=clientRegistry.js.map