let _cachedRegistry = null;
let _cachedMetadata = null;
function setEntityRegistry(registry, metadata) {
  _cachedRegistry = registry;
  _cachedMetadata = metadata || null;
  const entityCount = Object.keys(registry || {}).length;
  console.log(`[EntityQueries] Registry set with ${entityCount} entities`);
}
function addEntityToRegistry(entry) {
  if (!_cachedRegistry) {
    _cachedRegistry = {};
  }
  _cachedRegistry[entry.name] = entry;
}
function removeEntityFromRegistry(name) {
  if (!_cachedRegistry || !_cachedRegistry[name]) {
    return false;
  }
  delete _cachedRegistry[name];
  return true;
}
function clearEntityRegistry() {
  _cachedRegistry = null;
  _cachedMetadata = null;
}
function isRegistryInitialized() {
  return _cachedRegistry !== null && Object.keys(_cachedRegistry).length > 0;
}
function getRegistry() {
  if (_cachedRegistry) return _cachedRegistry;
  console.log("[EntityQueries] Registry not yet initialized (will be set by layout)");
  return {};
}
function getRegisteredEntities() {
  return Object.values(getRegistry()).map((entry) => entry.config);
}
function getEntity(name) {
  var _a;
  return (_a = getRegistry()[name]) == null ? void 0 : _a.config;
}
function getEntityMetadata(name) {
  return getRegistry()[name];
}
function getRootEntities() {
  return Object.values(getRegistry()).filter((entry) => entry.parent === null);
}
function getChildEntities(parentName) {
  return Object.values(getRegistry()).filter((entry) => entry.parent === parentName);
}
function getEntityTree() {
  const roots = getRootEntities();
  return roots.map((root) => ({
    ...root,
    childNodes: buildEntityHierarchy(root.name)
  }));
}
function buildEntityHierarchy(entityName) {
  const children = getChildEntities(entityName);
  return children.map((child) => ({
    ...child,
    childNodes: buildEntityHierarchy(child.name)
  }));
}
function getEntitiesByDepth(depth) {
  return Object.values(getRegistry()).filter((entry) => entry.depth === depth);
}
function getEntityTableName(name) {
  var _a;
  return (_a = getRegistry()[name]) == null ? void 0 : _a.tableName;
}
function getEntityByTableName(tableName) {
  return Object.values(getRegistry()).find((entry) => entry.tableName === tableName);
}
function getPluginEntities(pluginName) {
  return Object.values(getRegistry()).filter(
    (entry) => {
      var _a;
      return ((_a = entry.pluginContext) == null ? void 0 : _a.pluginName) === pluginName;
    }
  );
}
function getThemeEntities(themeName) {
  return Object.values(getRegistry()).filter(
    (entry) => {
      var _a;
      return ((_a = entry.themeContext) == null ? void 0 : _a.themeName) === themeName;
    }
  );
}
function getEntityOwner(entityName) {
  const entity = getRegistry()[entityName];
  if (!entity) return null;
  if (entity.pluginContext) {
    return { type: "plugin", name: entity.pluginContext.pluginName };
  }
  if (entity.themeContext) {
    return { type: "theme", name: entity.themeContext.themeName };
  }
  return null;
}
function getEntityBySlug(slug) {
  const entry = Object.values(getRegistry()).find((entry2) => "slug" in entry2.config && entry2.config.slug === slug);
  return entry == null ? void 0 : entry.config;
}
function getEntityRegistryMetadata() {
  getRegistry();
  return _cachedMetadata;
}
function getEntityRegistry() {
  return getRegistry();
}
export {
  addEntityToRegistry,
  clearEntityRegistry,
  getChildEntities,
  getEntitiesByDepth,
  getEntity,
  getEntityBySlug,
  getEntityByTableName,
  getEntityMetadata,
  getEntityOwner,
  getEntityRegistry,
  getEntityRegistryMetadata,
  getEntityTableName,
  getEntityTree,
  getPluginEntities,
  getRegisteredEntities,
  getRootEntities,
  getThemeEntities,
  isRegistryInitialized,
  removeEntityFromRegistry,
  setEntityRegistry
};
