import { useTranslation } from 'react-i18next';
import { Button } from '@/components/ui/button';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { Globe } from 'lucide-react';
import { languages } from '@/i18n/config';
import { useEffect } from 'react';

export function LanguageSwitcher() {
  const { i18n } = useTranslation();

  useEffect(() => {
    // Update document direction based on language
    const currentLang = languages[i18n.language as keyof typeof languages];
    if (currentLang) {
      document.documentElement.dir = currentLang.dir;
      document.documentElement.lang = i18n.language;
    }
  }, [i18n.language]);

  const changeLanguage = (lng: string) => {
    i18n.changeLanguage(lng);
    const lang = languages[lng as keyof typeof languages];
    if (lang) {
      document.documentElement.dir = lang.dir;
      document.documentElement.lang = lng;
    }
  };

  const currentLanguage = languages[i18n.language as keyof typeof languages] || languages.en;

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="ghost" size="sm" className="gap-2">
          <Globe className="h-4 w-4" />
          <span className="hidden sm:inline">{currentLanguage.name}</span>
          {currentLanguage.flagImg ? (
            <img src={currentLanguage.flagImg} alt="" className="h-5 w-5 rounded-sm object-cover" />
          ) : (
            <span className="text-lg">{currentLanguage.flag}</span>
          )}
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end" className="w-48">
        {Object.entries(languages).map(([code, lang]) => (
          <DropdownMenuItem
            key={code}
            onClick={() => changeLanguage(code)}
            className={`cursor-pointer ${i18n.language === code ? 'bg-yellow-100 dark:bg-yellow-900' : ''}`}
          >
            {lang.flagImg ? (
              <img src={lang.flagImg} alt="" className="h-5 w-5 rounded-sm object-cover mr-2" />
            ) : (
              <span className="text-lg mr-2">{lang.flag}</span>
            )}
            <span>{lang.name}</span>
          </DropdownMenuItem>
        ))}
      </DropdownMenuContent>
    </DropdownMenu>
  );
}