import '@/types/Store/Setting';
import { shopeeSetting, appSetting, clearAllSetting } from '@/plugins/setting';
import configShopee from '@/config/shopee';

/** @type {VuexStoreModule} */
export const settingStore = {
  /** @returns {StoreSetting} */
  state() {
    return {
      activeTab: 'shopee-product',
      configShopee: shopeeSetting.shopeeConfigProduct.get(),
      listBadWord: appSetting.badWord.get(),
      wartermarkProduct: {
        active: false,
        base64: '',
        size: {
          width: 0,
          height: 0,
        },
      },
      shopeeConfigClone: {
        maxDescription: configShopee.product.maxDescription,
        maxTitle: configShopee.product.maxTitle,
      },
      isBetaMode: !!appSetting.betaMode.get(),
      useShopManager: !!appSetting.useShopManager.get(),
      workerFetchService: !!appSetting.workerFetchService.get(),
    };
  },

  getters: {
    /** @param {StoreSetting} state */
    getSettingCloneShopee(state) {
      return state.shopeeConfigClone;
    },

    /** @param {StoreSetting} state */
    getBetaMode(state) {
      return state.isBetaMode;
    },

    /** @param {StoreSetting} state */
    getUseShopManager(state) {
      return state.useShopManager;
    },

    /** @param {StoreSetting} state */
    getWorkerFetchService(state) {
      return state.workerFetchService;
    },

    /** @param {StoreSetting} state */
    getProductWartermark(state) {
      return state.wartermarkProduct;
    },

    /** @param {StoreSetting} state */
    getTabActive(state) {
      return state.activeTab;
    },

    /** @param {StoreSetting} state */
    getListBadword(state) {
      return state.listBadWord;
    },

    /** @param {StoreSetting} state */
    getConfigProductShopee(state) {
      return state.configShopee;
    },

  },

  mutations: {
    /** @param {StoreSetting} state */
    SET_BETA_MODE(state) {
      state.isBetaMode = !state.isBetaMode;
      appSetting.betaMode.set(state.isBetaMode);
    },

    /** @param {StoreSetting} state */
    SET_WORKER_FETCH(state) {
      state.workerFetchService = !state.workerFetchService;
      appSetting.workerFetchService.set(state.workerFetchService);
    },

    /** @param {StoreSetting} state */
    SET_USE_SHOP_MANAGER(state) {
      state.useShopManager = !state.useShopManager;
      appSetting.useShopManager.set(state.useShopManager);
    },

    /**
     * @param {StoreSetting} state @param {string} tab
     * @param tab
     */
    SET_TAB(state, tab) {
      state.activeTab = tab;
      appSetting.activeTab.set(tab);
    },

    /**
     * @param {StoreSetting} state @param {object} config
     * @param config
     */
    SET_CLONE_SHOPEE(state, config) {
      state.shopeeConfigClone = config;
      shopeeSetting.shopeeConfigClone.set(config);
    },

    /**
     * @param {StoreSetting} state @param {object} value
     * @param value
     */
    SET_CONFIG_SHOPEE(state, value) {
      state.configShopee = value;
      shopeeSetting.shopeeConfigProduct.set(value);
    },

    /**
     * @param {StoreSetting} state @param {string[]} list
     * @param list
     */
    SET_BADWORD(state, list) {
      state.listBadWord = list;
      appSetting.badWord.set(list);
    },

    /**
     * @param {StoreSetting} state @param {string} base64
     * @param {object} payload
     */
    SET_WARTERMARK_PRODUCT_BASE64(state, payload) {
      const { base64, size } = payload;
      state.wartermarkProduct.base64 = base64;
      state.wartermarkProduct.size = size;
    },

    /**
     * @param {StoreSetting} state @param {boolean} active
     * @param active
     */
    SET_WARTERMARK_PRODUCT_ACTIVE(state, active) {
      state.wartermarkProduct.active = active;
    },

    SET_CLEAR() {
      clearAllSetting();
    },
  },

  actions: {
    SetUseShopManager({ commit }) {
      commit('SET_USE_SHOP_MANAGER');
    },

    SetBetaMode({ commit }) {
      commit('SET_BETA_MODE');
    },

    SetWorkerFetch({ commit }) {
      commit('SET_WORKER_FETCH');
    },

    SetTabActive({ commit }, tab) {
      commit('SET_TAB', tab);
    },

    SetSettingShopee({ commit }, data) {
      commit('SET_CONFIG_SHOPEE', data);
    },

    SetBadword({ commit }, data) {
      commit('SET_BADWORD', data);
    },

    SetClearSetting({ commit }) {
      commit('SET_CLEAR');
    },

    SetWartermarkProductBase64({ commit }, { base64, size }) {
      commit('SET_WARTERMARK_PRODUCT_BASE64', { base64, size });
    },

    SetWartermarkProductActive({ commit }, active) {
      commit('SET_WARTERMARK_PRODUCT_ACTIVE', active);
    },

    SetSettingCloneShope({ commit }, config) {
      commit('SET_CLONE_SHOPEE', config);
    },
  },
};
