'use client';

import images from '@/assets/images/images';
import { TLocales } from '@/models/layout';
import Select from '@/components/uikit/select';
import { usePathname } from 'next/navigation';

const languages = [
  { value: 'en', label: 'English', image: images.en },
  { value: 'fa', label: 'فارسی', image: images.fa },
];

export default function LanguageSwitch() {
  const pathname = usePathname();
  const currentLocale = pathname.split('/')[1] as TLocales;

  const changeLocale = (locale: string) => {
    if (currentLocale === locale) return;

    document.cookie = `locale=${locale}; path=/; max-age=31536000; samesite=lax`;
    let newPath = pathname.replace(/^\/(en|fa)/, '');
    if (newPath === '/') newPath = '';

    window.location.pathname = `/${locale}${newPath}`;
  };

  return (
    <Select
      options={languages}
      value={currentLocale}
      onChange={changeLocale}
      size="small"
    />
  );
}
