import { useCallback, useEffect, useState } from "react";
import { useLocale } from "next-intl";
import { TranslationService } from "../lib/services/translation.service";
import { loadMergedTranslations } from "../lib/translations/registry";
import { useEnabledEntities } from "./useEnabledEntities";
function useEntityTranslations(entityName) {
  const locale = useLocale();
  const { entities } = useEnabledEntities();
  const [translations, setTranslations] = useState({});
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);
  const loadTranslations = useCallback(async () => {
    try {
      setIsLoading(true);
      setError(null);
      const entity = entities.find((e) => e.slug === entityName);
      if (!entity) {
        setError(`Entity ${entityName} not found`);
        return;
      }
      const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME || "default";
      const entityTranslations = await TranslationService.loadEntity(activeTheme, entityName, locale);
      setTranslations(entityTranslations);
    } catch (err) {
      console.error(`Error loading translations for entity ${entityName}:`, err);
      setError(err instanceof Error ? err.message : "Unknown error");
    } finally {
      setIsLoading(false);
    }
  }, [entityName, locale, entities]);
  useEffect(() => {
    if (entities.length > 0) {
      loadTranslations();
    }
  }, [loadTranslations, entities.length]);
  const t = useCallback((key, fallback) => {
    const keys = key.split(".");
    let value = translations;
    for (const k of keys) {
      if (value && typeof value === "object" && value !== null && k in value) {
        value = value[k];
      } else {
        return fallback || key;
      }
    }
    return typeof value === "string" ? value : fallback || key;
  }, [translations]);
  const hasTranslation = useCallback((key) => {
    const keys = key.split(".");
    let value = translations;
    for (const k of keys) {
      if (value && typeof value === "object" && value !== null && k in value) {
        value = value[k];
      } else {
        return false;
      }
    }
    return typeof value === "string";
  }, [translations]);
  return {
    translations,
    t,
    hasTranslation,
    isLoading,
    error,
    reload: loadTranslations
  };
}
function useAllEntityTranslations() {
  const locale = useLocale();
  const { entities, isLoading: entitiesLoading } = useEnabledEntities();
  const [allTranslations, setAllTranslations] = useState({});
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);
  const loadAllTranslationsCallback = useCallback(async () => {
    try {
      setIsLoading(true);
      setError(null);
      const translations = await loadMergedTranslations(locale);
      setAllTranslations(translations);
    } catch (err) {
      console.error("Error loading all entity translations:", err);
      setError(err instanceof Error ? err.message : "Unknown error");
    } finally {
      setIsLoading(false);
    }
  }, [locale]);
  useEffect(() => {
    if (!entitiesLoading && entities.length > 0) {
      loadAllTranslationsCallback();
    }
  }, [loadAllTranslationsCallback, entitiesLoading, entities.length]);
  return {
    translations: allTranslations,
    isLoading: isLoading || entitiesLoading,
    error,
    reload: loadAllTranslationsCallback
  };
}
export {
  useAllEntityTranslations,
  useEntityTranslations
};
