import { defineStore } from 'pinia';
import type { LocaleType } from '../../locales/config';
import { store } from '../../store';
import { Storage } from '../../utils/Storage';

interface LocaleState {
  locale: LocaleType;
}

export const useLocaleStore = defineStore({
  id: 'locale',
  state: (): LocaleState => ({
    locale: Storage.get('LOCALE__', 'zh_CN'),
  }),
  getters: {
    getLocale(): LocaleType {
      return this.locale ?? 'zh_CN';
    },
  },
  actions: {
    setLocale(locale: LocaleType) {
      this.locale = locale;
      Storage.set('LOCALE__', locale);
    },
  },
});

// Need to be used outside the setup
export function useLocaleStoreWithOut() {
  return useLocaleStore(store);
}
