import type { ActionTree } from 'vuex';
import type { AppState } from './state';
import { SET_LANG, SET_SYSTEM_INFO } from './mutations';
import { loadLanguageAsync } from '@/locales';
import { localStorage } from '@remote/icpx-platform/utils';
/* import { localStorage } from '@/utils/local-storage'; */
import { STORAGE_LANG_KEY } from '@/store/mutation-type';
import type { RootState } from '@/store/root-state';
import { getLoginPageInfo } from '@/api/platform/setting/systemSetting';
export const GET_SYSTEM_INFO = 'GET_SYSTEM_INFO';

export const actions: ActionTree<AppState, RootState> = {
  [SET_LANG]({ commit }, lang: string) {
    return new Promise<void>((resolve, reject) => {
      loadLanguageAsync(lang)
        .then(() => {
          commit(SET_LANG, lang);
          localStorage.set(STORAGE_LANG_KEY, lang);
          window.localStorage.setItem('lang', lang);
          resolve();
        })
        .catch((e: Error) => {
          reject(e);
        });
    });
  },
  // 获取系统设置信息
  [GET_SYSTEM_INFO]({ commit }) {
    return new Promise<void>(resolve => {
      getLoginPageInfo().then((res: any) => {
        commit(SET_SYSTEM_INFO, res.data);
        resolve(res);
      });
    });
  },
};
