import { defineStore } from 'pinia'
import i18n from '@/i18n'
import { customStorage } from '../persist'

export const useI18nStore = defineStore('i18n', {
	state: () => ({
		currentLocale: 'zh-CN',
		availableLocales: [
			{ label: '简体中文', value: 'zh-CN' },
			{ label: 'English', value: 'en-US' },
		],
	}),

	getters: {
		localeLabel: (state) => {
			const locale = state.availableLocales.find((item) => item.value === state.currentLocale)
			return locale ? locale.label : ''
		},
	},

	actions: {
		setLocale(locale) {
			this.currentLocale = locale
			i18n.global.locale.value = locale
		},
	},

	persist: {
		key: 'i18n-store',
		storage: customStorage,
		pick: ['currentLocale'],
	},
})
