/* eslint-disable no-empty */
import Vuex from 'vuex'
import { getTeamBaseInfo, findSound } from '@/api/buildTeam'

const store = new Vuex.Store({
  state: {
    res: null,
    baseInfo: null,
    musicGroup: {},
    sounds: []
  },
  mutations: {
    updateBaseinfo(state, res) {
      state.res = res
      state.baseInfo = res.data
      if (res.data.musicGroup) {
        state.musicGroup = {...res.data.musicGroup}
      } else {
        state.musicGroup = {}
      }
    },
    updateSound(state, list) {
      state.sounds = [...list]
    }
  },
  actions: {
    async getBaseInfo({ commit, state }, payload) {
      try {
        if (payload.data?.musicGroupId !== state.musicGroup.id || payload.force) {
          const res = await getTeamBaseInfo(payload.data)
          commit('updateBaseinfo', res)
          return Promise.resolve(res)
        }
        return Promise.resolve(state.res)
      } catch (error) {}
      return Promise.reject()
    },
    async getFindSound({ commit, state }, payload) {
      try {
        if (state.sounds.length) {
          return Promise.resolve(state.sounds)
        }
        const res = await findSound({
          ...payload.data
        })
        commit('updateSound', res.data)
        return Promise.resolve(state.sounds)
      } catch (error) {}
      return Promise.reject()
    }
  }
})

export default store
