"use client";
import { useEffect, useCallback, useRef } from "react";
import { usePathname } from "next/navigation";
import { useLocale } from "next-intl";
const CONTEXT_NAMESPACES = {
  public: ["common", "public", "auth"],
  auth: ["common", "auth", "validation"],
  dashboard: ["common", "dashboard", "settings", "public", "teams", "admin"]
};
function useContextAwareTranslations() {
  const pathname = usePathname();
  const locale = useLocale();
  const previousContext = useRef(null);
  const loadedNamespaces = useRef(/* @__PURE__ */ new Set());
  const getCurrentContext = useCallback((path) => {
    if (path.startsWith("/dashboard")) return "dashboard";
    if (path.includes("login") || path.includes("signup") || path.includes("auth") || path.includes("forgot-password") || path.includes("reset-password") || path.includes("verify-email")) {
      return "auth";
    }
    return "public";
  }, []);
  const loadContextNamespaces = useCallback(async (context) => {
    const requiredNamespaces = CONTEXT_NAMESPACES[context];
    const newNamespaces = requiredNamespaces.filter((ns) => !loadedNamespaces.current.has(ns));
    if (newNamespaces.length === 0) {
      console.log(`[useContextAwareTranslations] \u2713 All namespaces for ${context} already loaded`);
      return;
    }
    console.log(`[useContextAwareTranslations] \u{1F504} Loading namespaces for ${context}:`, newNamespaces);
    try {
      const loadPromises = newNamespaces.map(async (namespace) => {
        try {
          const messages = await import(`../messages/${locale}/${namespace}.json`);
          loadedNamespaces.current.add(namespace);
          console.log(`[useContextAwareTranslations] \u2713 Loaded ${namespace} for ${locale}`);
          return { namespace, messages: messages.default };
        } catch (error) {
          console.warn(`[useContextAwareTranslations] \u26A0\uFE0F Failed to load ${namespace}:`, error);
          return null;
        }
      });
      await Promise.all(loadPromises);
      console.log(`[useContextAwareTranslations] \u2705 Context ${context} loading complete`);
    } catch (error) {
      console.error(`[useContextAwareTranslations] \u274C Error loading namespaces for ${context}:`, error);
    }
  }, [locale]);
  useEffect(() => {
    const currentContext = getCurrentContext(pathname);
    if (previousContext.current !== currentContext) {
      console.log(`[useContextAwareTranslations] \u{1F4CD} Context change: ${previousContext.current} \u2192 ${currentContext}`);
      if (currentContext === "dashboard" && previousContext.current === "public") {
        console.log(`[useContextAwareTranslations] \u{1F511} User authenticated, loading dashboard namespaces`);
        loadContextNamespaces("dashboard");
      } else if (currentContext === "public" && previousContext.current === "dashboard") {
        console.log(`[useContextAwareTranslations] \u{1F310} Returning to public, ensuring public namespaces available`);
        loadContextNamespaces("public");
      } else if (currentContext === "auth") {
        console.log(`[useContextAwareTranslations] \u{1F510} Auth context, loading auth-only namespaces`);
        loadContextNamespaces("auth");
      } else {
        loadContextNamespaces(currentContext);
      }
      previousContext.current = currentContext;
    }
  }, [pathname, getCurrentContext, loadContextNamespaces]);
  useEffect(() => {
    const currentContext = getCurrentContext(pathname);
    const preloadRelatedNamespaces = () => {
      if (currentContext === "public") {
        console.log(`[useContextAwareTranslations] \u{1F504} Preloading auth namespaces for potential login`);
        loadContextNamespaces("auth");
      } else if (currentContext === "dashboard") {
        console.log(`[useContextAwareTranslations] \u{1F504} Ensuring public namespaces available for navigation`);
        loadContextNamespaces("public");
      }
    };
    if (typeof window !== "undefined") {
      if ("requestIdleCallback" in window) {
        const idleCallback = window.requestIdleCallback(preloadRelatedNamespaces, { timeout: 5e3 });
        return () => window.cancelIdleCallback(idleCallback);
      } else {
        const timeout = setTimeout(preloadRelatedNamespaces, 2e3);
        return () => clearTimeout(timeout);
      }
    }
  }, [pathname, getCurrentContext, loadContextNamespaces]);
  return {
    currentContext: getCurrentContext(pathname),
    loadedNamespaces: Array.from(loadedNamespaces.current),
    loadContextNamespaces
  };
}
export {
  useContextAwareTranslations
};
