var u = Object.defineProperty;
var p = (o, n, e) => n in o ? u(o, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[n] = e;
var a = (o, n, e) => p(o, typeof n != "symbol" ? n + "" : n, e);
import { WebSocketClient as y } from "./websocket.mjs";
const r = class r {
  constructor() {
    a(this, "connections", /* @__PURE__ */ new Map());
    a(this, "connectionConfigs", /* @__PURE__ */ new Map());
    a(this, "pendingConnections", /* @__PURE__ */ new Map());
  }
  static getInstance() {
    return r.instance || (r.instance = new r()), r.instance;
  }
  /**
   * 生成连接的唯一标识
   */
  generateConnectionKey(n) {
    return JSON.stringify(n);
  }
  /**
   * 获取或创建连接
   */
  async getOrCreateConnection(n, e) {
    const c = this.generateConnectionKey(n), i = this.connections.get(c);
    if (i && i.isConnected())
      return i;
    const C = Array.from(this.pendingConnections.entries()).find(
      ([t]) => {
        try {
          return JSON.parse(t).uuid === n.uuid;
        } catch {
          return !1;
        }
      }
    );
    if (C)
      try {
        return await C[1];
      } catch {
      }
    let d = 0;
    Array.from(this.connections.entries()).forEach(([t, l]) => {
      try {
        JSON.parse(t).uuid === n.uuid && (l.disconnect(), this.connections.delete(t), this.connectionConfigs.delete(t), d++);
      } catch {
      }
    }), Array.from(this.pendingConnections.keys()).forEach((t) => {
      try {
        JSON.parse(t).uuid === n.uuid && this.pendingConnections.delete(t);
      } catch {
      }
    });
    const h = (async () => {
      try {
        const t = new y(n, e);
        return await t.connect(), this.connections.set(c, t), this.connectionConfigs.set(c, n), t;
      } catch (t) {
        throw t;
      } finally {
        this.pendingConnections.delete(c);
      }
    })();
    return this.pendingConnections.set(c, h), h;
  }
  /**
   * 检查连接是否存在
   */
  hasConnection(n) {
    const e = this.generateConnectionKey(n), c = this.connections.get(e);
    return c !== void 0 && c.isConnected();
  }
  /**
   * 获取所有连接
   */
  getAllConnections() {
    return new Map(this.connections);
  }
  /**
   * 关闭指定连接
   */
  closeConnection(n) {
    const e = this.generateConnectionKey(n), c = this.connections.get(e);
    return c ? (c.disconnect(), this.connections.delete(e), this.connectionConfigs.delete(e), this.pendingConnections.delete(e), !0) : !1;
  }
  /**
   * 关闭所有连接
   */
  closeAllConnections() {
    for (const [n, e] of this.connections)
      e.disconnect(), this.connections.delete(n), this.connectionConfigs.delete(n);
    return this.connections.clear(), this.connectionConfigs.clear(), this.pendingConnections.clear(), !0;
  }
};
a(r, "instance");
let s = r;
const A = {
  /**
   * 获取连接池实例
   */
  getInstance: () => s.getInstance(),
  /**
   * 检查是否存在指定配置的连接
   */
  hasConnection: (o) => s.getInstance().hasConnection(o),
  /**
   * 关闭指定配置的连接
   */
  closeConnection: (o) => s.getInstance().closeConnection(o),
  /**
   * 关闭所有连接
   */
  closeAllConnections: () => {
    s.getInstance().closeAllConnections();
  },
  /**
   * 关闭并移除key中uuid为uuid的连接
   * @param uuid 连接的唯一标识
   */
  removeConnectionByUUID: (o) => {
    const n = s.getInstance();
    n.getAllConnections().entries().forEach(([c]) => {
      const i = JSON.parse(c);
      i.uuid.includes(o) && n.closeConnection(i);
    });
  },
  /**
   * 强制清理所有连接（用于调试和紧急清理）
   */
  forceCleanAll: () => {
    const o = s.getInstance(), n = o.getAllConnections();
    Array.from(n.entries()).forEach(([e, c]) => {
      try {
        const i = JSON.parse(e);
        c.disconnect();
      } catch {
      }
    }), o.closeAllConnections();
  }
};
export {
  s as WebSocketConnectionPool,
  A as WebSocketConnectionPoolManager
};
