import {
  ENTITY_REGISTRY,
  ENTITY_METADATA
} from "@nextsparkjs/registries/entity-registry";
function getRegisteredEntities() {
  return Object.values(ENTITY_REGISTRY).map((entry) => entry.config);
}
function getEntity(name) {
  var _a;
  return (_a = ENTITY_REGISTRY[name]) == null ? void 0 : _a.config;
}
function getEntityMetadata(name) {
  return ENTITY_REGISTRY[name];
}
function getRootEntities() {
  return Object.values(ENTITY_REGISTRY).filter((entry) => entry.parent === null);
}
function getChildEntities(parentName) {
  return Object.values(ENTITY_REGISTRY).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(ENTITY_REGISTRY).filter((entry) => entry.depth === depth);
}
function getEntityTableName(name) {
  var _a;
  return (_a = ENTITY_REGISTRY[name]) == null ? void 0 : _a.tableName;
}
function getEntityByTableName(tableName) {
  return Object.values(ENTITY_REGISTRY).find((entry) => entry.tableName === tableName);
}
function getPluginEntities(pluginName) {
  return Object.values(ENTITY_REGISTRY).filter(
    (entry) => {
      var _a;
      return ((_a = entry.pluginContext) == null ? void 0 : _a.pluginName) === pluginName;
    }
  );
}
function getThemeEntities(themeName) {
  return Object.values(ENTITY_REGISTRY).filter(
    (entry) => {
      var _a;
      return ((_a = entry.themeContext) == null ? void 0 : _a.themeName) === themeName;
    }
  );
}
function getEntityOwner(entityName) {
  const entity = ENTITY_REGISTRY[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) {
  var _a;
  return (_a = Object.values(ENTITY_REGISTRY).find((entry) => entry.config.slug === slug)) == null ? void 0 : _a.config;
}
function getEntityRegistryMetadata() {
  return ENTITY_METADATA;
}
export {
  ENTITY_METADATA,
  ENTITY_REGISTRY,
  getChildEntities,
  getEntitiesByDepth,
  getEntity,
  getEntityBySlug,
  getEntityByTableName,
  getEntityMetadata,
  getEntityOwner,
  getEntityRegistryMetadata,
  getEntityTableName,
  getEntityTree,
  getPluginEntities,
  getRegisteredEntities,
  getRootEntities,
  getThemeEntities
};
