"use client";
import { useEffect } from "react";
import { useLocale } from "next-intl";
const DASHBOARD_NAMESPACES = ["common", "dashboard", "settings", "public", "teams", "admin"];
const AUTH_NAMESPACES = ["common", "auth", "validation"];
const PUBLIC_NAMESPACES = ["common", "public", "auth"];
function useTranslationPreload(context = "auto") {
  const locale = useLocale();
  useEffect(() => {
    const preloadNamespaces = async (namespaces, contextName) => {
      console.log(`[TranslationPreload] \u{1F680} Iniciando precarga ${contextName} para ${locale}:`, namespaces);
      const promises = namespaces.map(async (namespace) => {
        try {
          await import(`../messages/${locale}/${namespace}.json`);
          console.log(`[TranslationPreload] \u2713 Precargado: ${namespace}`);
          return { namespace, success: true };
        } catch (error) {
          console.warn(`[TranslationPreload] \u26A0\uFE0F Error precargando ${namespace}:`, error);
          return { namespace, success: false, error };
        }
      });
      const results = await Promise.allSettled(promises);
      const successful = results.filter(
        (result) => result.status === "fulfilled" && result.value.success
      ).length;
      console.log(`[TranslationPreload] \u2705 Precarga ${contextName} completa: ${successful}/${namespaces.length} namespaces`);
    };
    const schedulePreload = () => {
      let namespacesToLoad = [];
      let contextName = "";
      if (context === "dashboard") {
        namespacesToLoad = DASHBOARD_NAMESPACES;
        contextName = "dashboard";
      } else if (context === "auth") {
        namespacesToLoad = AUTH_NAMESPACES;
        contextName = "auth";
      } else {
        if (typeof window !== "undefined") {
          const currentPath = window.location.pathname;
          if (currentPath.startsWith("/dashboard")) {
            namespacesToLoad = DASHBOARD_NAMESPACES;
            contextName = "dashboard (auto-detected)";
          } else if (currentPath.includes("login") || currentPath.includes("signup") || currentPath.includes("auth")) {
            namespacesToLoad = AUTH_NAMESPACES;
            contextName = "auth (auto-detected)";
          } else {
            namespacesToLoad = PUBLIC_NAMESPACES;
            contextName = "public (auto-detected)";
          }
        } else {
          namespacesToLoad = [...DASHBOARD_NAMESPACES, ...AUTH_NAMESPACES];
          contextName = "mixed (server-side)";
        }
      }
      if (typeof window !== "undefined" && "requestIdleCallback" in window) {
        window.requestIdleCallback(() => {
          preloadNamespaces(namespacesToLoad, contextName);
        }, { timeout: 2e3 });
      } else {
        setTimeout(() => {
          preloadNamespaces(namespacesToLoad, contextName);
        }, 1e3);
      }
    };
    schedulePreload();
  }, [locale, context]);
}
function useNavigationPreload() {
  const locale = useLocale();
  const preloadForRoute = async (pathname) => {
    let namespacesToLoad = [];
    if (pathname.startsWith("/dashboard")) {
      namespacesToLoad = DASHBOARD_NAMESPACES;
    } else if (pathname.startsWith("/auth")) {
      namespacesToLoad = AUTH_NAMESPACES;
    } else {
      namespacesToLoad = PUBLIC_NAMESPACES;
    }
    console.log(`[NavigationPreload] Precargando para ${pathname}:`, namespacesToLoad);
    const promises = namespacesToLoad.map(
      (namespace) => import(`../messages/${locale}/${namespace}.json`).catch((error) => {
        console.warn(`[NavigationPreload] Error: ${namespace}`, error);
        return null;
      })
    );
    await Promise.all(promises);
    console.log(`[NavigationPreload] \u2705 Listo para navegar a ${pathname}`);
  };
  return { preloadForRoute };
}
export {
  useNavigationPreload,
  useTranslationPreload
};
