import lodash from 'lodash';
import { userSetting } from '@/plugins/setting';
import { getKeyProductClone } from './utils';

export const mutations = {
  /**
   * @param {StateUser} state @param {string} token
   * @param token
   */
  SET_USER_TOKEN(state, token) {
    state.token = token;
    userSetting.userToken.set(token);
  },

  /**
   * @param {StateUser} state @param {UserData} userData
   * @param userData
   */
  SET_USER_DATA(state, userData) {
    state.userData = userData;
    state.id = userData._id;
    userSetting.userData.set(userData);
    userSetting.userId.set(userData._id);

    if (userData.extensionPlan) {
      const { extensionPlan } = userData;
      state.extensionPlan = extensionPlan;
      userSetting.userExtensionPlan.set(extensionPlan);
    }
  },

  /**
   * @param {StateUser} state @param {object} root @param {string} root.username @param {string} root.password
   * @param root0
   * @param root0.username
   * @param root0.password
   */
  SET_USER_LOGIN(state, { username, password }) {
    state.username = username;
    state.password = password;
    userSetting.userPassword.set(password);
    userSetting.userName.set(username);
  },

  /** @param {StateUser} state */
  SET_USER_LOGOUT(state) {
    state.token = '';
    state.id = '';
    state.userData = {};
    state.extensionPlan = 'Free';
  },

  /**
   * @param {StateUser} state @param {string} platform
   * @param platform
   */
  DECREASE_USER_SCORE(state, platform) {
    const key = getKeyProductClone(platform);
    const set = lodash.get(state, key, 0) - 1;
    lodash.set(state, key, set);
  },

};
