import { getListShop, refreshToken } from '@/api/autoshopee/lazada';
import i18n from '@/vue/i18n';

/** @type {VuexStoreModule} */
export const lazadaStore = {
  namespaced: true,

  state() {
    return {
      shop: {
        selected: '',
        list: [],
      },
    };
  },

  mutations: {
    SET_SHOP_SELECT(state, id) {
      state.shop.selected = id;
    },

    SET_SHOP_LIST(state, list) {
      state.shop.list = list;
    },
  },

  getters: {
    getLazadaShopSelect(state) {
      return state.shop.selected;
    },

    getLazadaShopList(state) {
      return state.shop.list.map((item) => ({
        ...item,
        label: [item.name, item.disconnected ? `(${i18n.t('SHOP_DISCONNECTED')})` : ''].join(' '),
        disabled: item.disconnected,
      }));
    },
  },

  actions: {
    LazadaSelectShop({ commit }, id) {
      commit('SET_SHOP_SELECT', id);
      refreshToken(id);
    },

    async LazadaGetListShop({ commit }) {
      const { data: list } = await getListShop();
      commit('SET_SHOP_LIST', list);
    },
  },
};
