// src/containers/pool.ts
var g = globalThis;
function state() {
  if (!g.__caPool) {
    g.__caPool = { byEnv: /* @__PURE__ */ new Map(), bySession: /* @__PURE__ */ new Map() };
  }
  return g.__caPool;
}
function register(entry) {
  const s = state();
  const list = s.byEnv.get(entry.envId) ?? [];
  if (!list.some((e) => e.sessionId === entry.sessionId)) {
    list.push(entry);
  }
  s.byEnv.set(entry.envId, list);
  s.bySession.set(entry.sessionId, entry);
}
function getBySession(sessionId) {
  return state().bySession.get(sessionId) ?? null;
}
function countInEnv(envId) {
  return state().byEnv.get(envId)?.length ?? 0;
}
function unregister(sessionId) {
  const s = state();
  const entry = s.bySession.get(sessionId);
  if (!entry) return null;
  s.bySession.delete(sessionId);
  const list = s.byEnv.get(entry.envId);
  if (list) {
    const idx = list.findIndex((e) => e.sessionId === sessionId);
    if (idx >= 0) list.splice(idx, 1);
    if (list.length === 0) s.byEnv.delete(entry.envId);
  }
  return entry;
}
function allSessionSandboxes() {
  return Array.from(state().bySession.values());
}

export {
  register,
  getBySession,
  countInEnv,
  unregister,
  allSessionSandboxes
};
