import { createContext, ReactNode, useContext, useState } from "react";

type LanguageContextType = {
  language: "en" | "ar";
  setLanguage: (lang: "en" | "ar") => void;
  t: (en: string, ar: string) => string;
  toggleLanguage: () => void;
  isArabic: boolean;
};

const LanguageContext = createContext<LanguageContextType | null>(null);

export function LanguageProvider({ children }: { children: ReactNode }) {
  const [language, setLanguage] = useState<"en" | "ar">("en");

  const t = (en: string, ar: string) => {
    return language === "en" ? en : ar;
  };

  const toggleLanguage = () => {
    setLanguage(prev => prev === "en" ? "ar" : "en");
  };

  const isArabic = language === "ar";

  return (
    <LanguageContext.Provider value={{ language, setLanguage, t, toggleLanguage, isArabic }}>
      {children}
    </LanguageContext.Provider>
  );
}

export function useLanguage() {
  const context = useContext(LanguageContext);
  if (!context) {
    throw new Error("useLanguage must be used within a LanguageProvider");
  }
  return context;
}