const REGISTRY_KEY = "almostnode-db-registry";
const ACTIVE_KEY = "almostnode-active-db";
const DEFAULT_NAMESPACE = "global";
let currentNamespace = DEFAULT_NAMESPACE;
function normalizeNamespace(namespace) {
  const trimmed = namespace == null ? void 0 : namespace.trim();
  if (!trimmed) {
    return DEFAULT_NAMESPACE;
  }
  const normalized = trimmed.replace(/[^a-zA-Z0-9_-]/g, "-").slice(0, 80);
  return normalized || DEFAULT_NAMESPACE;
}
function getRegistryKey(namespace = currentNamespace) {
  const normalized = normalizeNamespace(namespace);
  return normalized === DEFAULT_NAMESPACE ? REGISTRY_KEY : `${REGISTRY_KEY}:${normalized}`;
}
function getActiveKey(namespace = currentNamespace) {
  const normalized = normalizeNamespace(namespace);
  return normalized === DEFAULT_NAMESPACE ? ACTIVE_KEY : `${ACTIVE_KEY}:${normalized}`;
}
function getIndexedDbName(name, namespace = currentNamespace) {
  const normalizedNamespace = normalizeNamespace(namespace);
  return normalizedNamespace === DEFAULT_NAMESPACE ? `almostnode-db-${name}` : `almostnode-db-${normalizedNamespace}-${name}`;
}
function isValidDatabaseName(name) {
  return Boolean(name) && name.length <= 50 && /^[a-zA-Z0-9_-]+$/.test(name);
}
function getValidatedDatabaseName(name) {
  const trimmed = name.trim();
  if (!trimmed) throw new Error("Database name cannot be empty");
  if (trimmed.length > 50) throw new Error("Database name must be 50 characters or less");
  if (!/^[a-zA-Z0-9_-]+$/.test(trimmed)) {
    throw new Error("Database name can only contain letters, numbers, hyphens, and underscores");
  }
  return trimmed;
}
function resolveStorageKey(name, namespace = currentNamespace) {
  const entry = loadRegistry(namespace).find((candidate) => candidate.name === name);
  return (entry == null ? void 0 : entry.storageKey) || name;
}
function loadRegistry(namespace = currentNamespace) {
  try {
    const raw = localStorage.getItem(getRegistryKey(namespace));
    const entries = raw ? JSON.parse(raw) : [];
    if (!Array.isArray(entries)) {
      return [];
    }
    return entries.filter((entry) => Boolean(entry) && typeof entry === "object" && typeof entry.name === "string" && typeof entry.createdAt === "string").map((entry) => ({
      name: entry.name,
      createdAt: entry.createdAt,
      storageKey: typeof entry.storageKey === "string" && entry.storageKey ? entry.storageKey : void 0
    }));
  } catch {
    return [];
  }
}
function saveRegistry(entries, namespace = currentNamespace) {
  localStorage.setItem(getRegistryKey(namespace), JSON.stringify(entries));
}
function getDatabaseNamespace() {
  return currentNamespace;
}
function setDatabaseNamespace(namespace) {
  currentNamespace = normalizeNamespace(namespace);
  return currentNamespace;
}
function listDatabases(namespace = currentNamespace) {
  return loadRegistry(namespace);
}
function createDatabase(name, namespace = currentNamespace) {
  const trimmed = getValidatedDatabaseName(name);
  const entries = loadRegistry(namespace);
  if (entries.some((e) => e.name === trimmed)) {
    throw new Error(`Database "${trimmed}" already exists`);
  }
  const entry = { name: trimmed, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
  entries.push(entry);
  saveRegistry(entries, namespace);
  return entry;
}
function deleteDatabase(name, namespace = currentNamespace) {
  const entries = loadRegistry(namespace);
  const entry = entries.find((candidate) => candidate.name === name);
  const storageKey = (entry == null ? void 0 : entry.storageKey) || name;
  const filtered = entries.filter((e) => e.name !== name);
  if (filtered.length === entries.length) {
    throw new Error(`Database "${name}" not found`);
  }
  saveRegistry(filtered, namespace);
  if (getActiveDatabase(namespace) === name) {
    localStorage.removeItem(getActiveKey(namespace));
  }
  indexedDB.deleteDatabase(`/pglite/${getIndexedDbName(storageKey, namespace)}`);
}
function getActiveDatabase(namespace = currentNamespace) {
  return localStorage.getItem(getActiveKey(namespace));
}
function setActiveDatabase(name, namespace = currentNamespace) {
  localStorage.setItem(getActiveKey(namespace), name);
}
function ensureDefaultDatabase(namespace = currentNamespace, preferredName) {
  const preferred = isValidDatabaseName(preferredName == null ? void 0 : preferredName.trim()) ? preferredName.trim() : null;
  let entries = loadRegistry(namespace);
  if (preferred && preferred !== "default") {
    const legacyDefault = entries.find((entry) => entry.name === "default");
    const preferredEntry = entries.find((entry) => entry.name === preferred);
    if (legacyDefault && !preferredEntry) {
      legacyDefault.name = preferred;
      legacyDefault.storageKey = legacyDefault.storageKey || "default";
      saveRegistry(entries, namespace);
      entries = loadRegistry(namespace);
    }
  }
  if (entries.length === 0) {
    createDatabase(preferred || "default", namespace);
    entries = loadRegistry(namespace);
  }
  const active = getActiveDatabase(namespace);
  if (active && entries.some((e) => e.name === active)) {
    return active;
  }
  const nextActive = preferred && entries.some((entry) => entry.name === preferred) ? preferred : entries[0].name;
  setActiveDatabase(nextActive, namespace);
  return nextActive;
}
function getIdbPath(name, namespace = currentNamespace) {
  return `idb://${getIndexedDbName(resolveStorageKey(name, namespace), namespace)}`;
}
export {
  createDatabase,
  deleteDatabase,
  ensureDefaultDatabase,
  getActiveDatabase,
  getDatabaseNamespace,
  getIdbPath,
  listDatabases,
  setActiveDatabase,
  setDatabaseNamespace
};
//# sourceMappingURL=db-manager-DadVjJc9.js.map
