import { MutationTree } from 'vuex'
import { State } from './index'

export enum MutationTypes {
  addProfile = 'addProfile',
  removeProfile = 'removeProfile',
  editProfile = 'editProfile'
}

export type Mutations<S = State> = {
  [MutationTypes.addProfile](state: S, profile: any): void
  [MutationTypes.removeProfile](state: S, target: any): void
  [MutationTypes.editProfile](state: S, target: any, key: string): void
}

export const mutations: MutationTree<State> & Mutations = {
  [MutationTypes.addProfile](state: State, profile: any) {
    state.profile = profile
  },
  [MutationTypes.removeProfile](state: State, target: any) {
    const profile = state.profile as any
    const key = target.key
    if (profile[key]) {
      delete profile[key]
      state.profile = profile
    }
  },
  [MutationTypes.editProfile](state: State, target: any) {
    const profile = state.profile as any
    const key = target.key
    const data = target.data
    if (profile[key]) {
      profile[key] = data
      state.profile = profile
    }
  },
}
