import { getEntityConfig, getAllEntityConfigs } from "./registry";
class MetaSystemAdapter {
  /**
   * Get meta configuration for an entity
   * Derives meta table info from the entity's slug
   */
  getMetaConfig(entityName) {
    var _a;
    const config = getEntityConfig(entityName);
    if (config && ((_a = config.access) == null ? void 0 : _a.metadata)) {
      return {
        tableName: config.slug,
        metaTableName: `${config.slug}_metas`,
        idColumn: "entityId",
        apiPath: `/api/v1/${config.slug}`
      };
    }
    if (entityName === "user" || entityName === "users") {
      return {
        tableName: "users",
        metaTableName: "users_metas",
        idColumn: "userId",
        apiPath: "/api/v1/users"
      };
    }
    return null;
  }
  /**
   * Check if entity supports metadata
   */
  supportsMetadata(entityName) {
    var _a;
    const config = getEntityConfig(entityName);
    if (config) {
      return ((_a = config.access) == null ? void 0 : _a.metadata) || false;
    }
    return entityName === "user" || entityName === "users";
  }
  /**
   * Get entity API path
   */
  getApiPath(entityName) {
    const config = this.getMetaConfig(entityName);
    return (config == null ? void 0 : config.apiPath) || null;
  }
  /**
   * Get all supported entity types that have metadata
   */
  getSupportedEntityTypes() {
    return getAllEntityConfigs().filter((config) => {
      var _a;
      return (_a = config.access) == null ? void 0 : _a.metadata;
    }).map((config) => config.slug);
  }
  /**
   * Convert entity config to meta config format
   */
  toMetaConfig(entityConfig) {
    var _a;
    if (!((_a = entityConfig.access) == null ? void 0 : _a.metadata)) {
      return null;
    }
    return {
      entityType: entityConfig.slug,
      tableName: entityConfig.slug,
      metaTableName: `${entityConfig.slug}_metas`,
      idColumn: "entityId",
      apiPath: `/api/v1/${entityConfig.slug}`
    };
  }
  /**
   * Validate entity configuration compatibility
   */
  validateCompatibility(entityName) {
    const config = getEntityConfig(entityName);
    if (!config) {
      return {
        compatible: false,
        issues: [`Entity "${entityName}" not found in registry`],
        recommendations: ["Add entity configuration to the appropriate location"]
      };
    }
    return {
      compatible: true,
      issues: [],
      recommendations: []
    };
  }
  /**
   * Generate migration report for all entities
   */
  generateMigrationReport() {
    const entities = getAllEntityConfigs();
    return {
      fullyMigrated: entities.map((e) => e.slug),
      needsMigration: [],
      newEntitiesOnly: [],
      compatibilityIssues: []
    };
  }
}
const metaSystemAdapter = new MetaSystemAdapter();
function getMetaConfigForEntity(entityName) {
  return metaSystemAdapter.getMetaConfig(entityName);
}
function checkEntitySupportsMetadata(entityName) {
  return metaSystemAdapter.supportsMetadata(entityName);
}
function getEntityApiPath(entityName) {
  return metaSystemAdapter.getApiPath(entityName);
}
function validateEntityCompatibility(entityName) {
  return metaSystemAdapter.validateCompatibility(entityName);
}
export {
  MetaSystemAdapter,
  checkEntitySupportsMetadata,
  getEntityApiPath,
  getMetaConfigForEntity,
  metaSystemAdapter,
  validateEntityCompatibility
};
