"use client";
import { useState, useEffect, useMemo } from "react";
import { getEnabledEntities } from "../lib/entities/registry.client.js";
function useEnabledEntities() {
  const [entities, setEntities] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);
  useEffect(() => {
    try {
      setIsLoading(true);
      const enabledEntities = getEnabledEntities();
      console.log(`[useEnabledEntities] Loaded ${enabledEntities.length} enabled entities:`, enabledEntities.map((e) => e.slug));
      setEntities(enabledEntities);
      setError(null);
    } catch (err) {
      console.error("Failed to load entity configurations:", err);
      setError(err instanceof Error ? err : new Error("Failed to load entities"));
      setEntities([]);
    } finally {
      setIsLoading(false);
    }
  }, []);
  const refetch = () => {
    try {
      setIsLoading(true);
      setError(null);
      const enabledEntities = getEnabledEntities();
      console.log(`[useEnabledEntities.refetch] Reloaded ${enabledEntities.length} enabled entities:`, enabledEntities.map((e) => e.slug));
      setEntities(enabledEntities);
      setError(null);
    } catch (err) {
      console.error("Failed to reload entity configurations:", err);
      setError(err instanceof Error ? err : new Error("Failed to reload entities"));
    } finally {
      setIsLoading(false);
    }
  };
  return {
    entities,
    isLoading,
    error,
    refetch
  };
}
function useIsEntityEnabled(entityName) {
  const { entities } = useEnabledEntities();
  return useMemo(
    () => entities.some((entity) => entity.slug === entityName && entity.enabled),
    [entities, entityName]
  );
}
function useEnabledEntityConfig(entityName) {
  const { entities } = useEnabledEntities();
  return useMemo(
    () => entities.find((entity) => entity.slug === entityName) || null,
    [entities, entityName]
  );
}
export {
  useEnabledEntities,
  useEnabledEntityConfig,
  useIsEntityEnabled
};
