import React, { createContext, useContext, useEffect, useState } from "react";

const LangContext = createContext();

export const useLang = () => {
  return useContext(LangContext);
};

const LangProvider = ({ children }) => {
  const [language, setLanguage] = useState(() => {
    return localStorage.getItem("language") || "en-GB";
  });

  useEffect(() => {
    localStorage.setItem("language", language);
  }, [language]);

  const toggleLanguage = () => {
    setLanguage((prevLang) => (prevLang === "en-GB" ? "de-DE" : "en-GB"));
  };

  return (
    <LangContext.Provider value={{ language, toggleLanguage }}>
      {children}
    </LangContext.Provider>
  );
};

export default LangProvider;
