import { useContext } from "react";
import { OutletContext } from "../components/AppMainLayout";
import useTranslation from "./useTranslation";

/**
 * Convenience hook that reads findText from AppMainLayout's OutletContext
 * if available, otherwise falls back to initializing useTranslation locally.
 *
 * Safe to use anywhere — inside or outside AppMainLayout.
 */
const useFindText = () => {
  const context = useContext(OutletContext);

  // If wrapped by AppMainLayout, use context values (no extra S3 call)
  if (context?.findText) {
    return {
      findText: context.findText,
      defaultLang: context.defaultLang,
      setDefaultLang: context.setDefaultLang,
      isTranslationsLoading: context.isTranslationsLoading,
      setIsTranslationsLoading: context.setIsTranslationsLoading,
    };
  }

  // Fallback: initialize locally (for auth pages, landing page, etc.)
  return useTranslation();
};

export default useFindText;
