import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';

export type Lang = 'RU' | 'EN';

export const useLang = create<{
  value: Lang;
  updateLang: (lang: Lang) => void;
}>()(
  devtools(
    persist(
      set => ({
        value: JSON.parse(localStorage.getItem('lang') ?? '{}').state ?? 'RU',
        updateLang: (lang: Lang) => {
          localStorage.setItem(
            'lang',
            JSON.stringify({ state: { value: lang } }),
          );
          set({ value: lang });
        },
      }),
      {
        name: 'lang',
        partialize: state => ({ value: state.value }),
      },
    ),
  ),
);
