import { useState, useEffect, useCallback } from "react";
import { getAllEntityConfigs } from "../lib/entities/registry.client.js";
const configCache = /* @__PURE__ */ new Map();
let registryLoaded = false;
function useEntityConfig(entityType, options = {}) {
  const { silent = false, cache = true } = options;
  const [state, setState] = useState({
    config: null,
    isLoading: true,
    error: null,
    isOverride: false
  });
  const detectOverride = useCallback((entityName) => {
    const knownOverrides = ["tasks"];
    return knownOverrides.includes(entityName.toLowerCase());
  }, []);
  const loadEntityConfig = useCallback(() => {
    if (!entityType) {
      setState((prev) => ({
        ...prev,
        isLoading: false,
        error: "Entity type is required"
      }));
      return;
    }
    try {
      setState((prev) => ({ ...prev, isLoading: true, error: null }));
      if (cache && configCache.has(entityType)) {
        const cachedConfig = configCache.get(entityType);
        const hasOverride2 = detectOverride(entityType);
        setState({
          config: cachedConfig,
          isLoading: false,
          error: null,
          isOverride: hasOverride2
        });
        if (!silent) {
          console.log(`[useEntityConfig] Cache hit for ${entityType}`, {
            config: cachedConfig,
            hasOverride: hasOverride2
          });
        }
        return;
      }
      const allEntities = getAllEntityConfigs();
      const foundEntity = allEntities.find((entity) => entity.slug === entityType);
      if (!foundEntity) {
        const error = `Entidad "${entityType}" no encontrada en el registry`;
        setState({
          config: null,
          isLoading: false,
          error,
          isOverride: false
        });
        if (!silent) {
          console.error(`[useEntityConfig] ${error}`, {
            entityType,
            availableEntities: allEntities.map((e) => e.slug)
          });
        }
        return;
      }
      const hasOverride = detectOverride(entityType);
      if (cache) {
        configCache.set(entityType, foundEntity);
      }
      setState({
        config: foundEntity,
        isLoading: false,
        error: null,
        isOverride: hasOverride
      });
      if (!silent) {
        console.log(`[useEntityConfig] Loaded config for ${entityType}`, {
          config: foundEntity,
          hasOverride,
          cached: cache
        });
      }
    } catch (err) {
      const error = err instanceof Error ? err.message : "Error desconocido al cargar configuraci\xF3n de entidad";
      setState({
        config: null,
        isLoading: false,
        error,
        isOverride: false
      });
      if (!silent) {
        console.error(`[useEntityConfig] Error loading config for ${entityType}:`, err);
      }
    }
  }, [entityType, cache, silent, detectOverride]);
  useEffect(() => {
    loadEntityConfig();
  }, [loadEntityConfig]);
  return state;
}
function preloadEntityConfigs() {
  if (registryLoaded) return;
  try {
    const allEntities = getAllEntityConfigs();
    allEntities.forEach((entity) => {
      configCache.set(entity.slug, entity);
    });
    registryLoaded = true;
    console.log("[useEntityConfig] Preloaded all entity configurations", {
      count: allEntities.length,
      entities: allEntities.map((e) => e.slug)
    });
  } catch (error) {
    console.error("[useEntityConfig] Error preloading entity configurations:", error);
  }
}
function clearEntityConfigCache() {
  configCache.clear();
  registryLoaded = false;
  console.log("[useEntityConfig] Cache cleared");
}
function getEntityConfigCacheStats() {
  return {
    size: configCache.size,
    entities: Array.from(configCache.keys()),
    registryLoaded
  };
}
export {
  clearEntityConfigCache,
  getEntityConfigCacheStats,
  preloadEntityConfigs,
  useEntityConfig
};
