import { RENDER_NOTE, ADD_NOTE, DELETE_NOTE, UPDATE_NOTE, STAR_NOTE, GET_NOTELIST, GET_STAR, ADD_TAG, SHOW_TAG_LIST, SHOW_NOTE_LIST } from './mutation-types'

function getIndex (list, id) {
  return list.findIndex((ele) => {
    return +ele.id === id
  })
}

const mutations = {
  [RENDER_NOTE] (state, data) {
    if (data.isNew) {
      state.activeItem = {
        title: '',
        content: ''
      }
    } else {
      let _data = state.list[getIndex(state.list, data.id)]
      state.activeItem = _data || null
    }
    state.isNew = data.isNew
  },
  [ADD_NOTE] (state, data) {
    console.log(data)
    data.id = state.list[state.list.length - 1].id + 1
    data.summary = data.content
    data.star = false
    console.log(data)
    state.list = [...state.list, data]
  },
  [DELETE_NOTE] (state, id) {
    // let index = state.list.findIndex((ele) => {
    //   return +ele.id === id
    // })
    // console.log(index)
    state.list.splice(getIndex(state.list, id), 1)
    state.activeItem = null
  },
  [UPDATE_NOTE] (state, newData) {
    let _id = newData.id
    state.list[_id] = newData
  },
  [STAR_NOTE] (state, id) {
    let index = getIndex(state.list, id)
    state.list[index].star = !state.list[index].star
  },
  [GET_NOTELIST] (state, data) {
    state.list = data
  },
  [GET_STAR] (state, data) {
    state.showStar = !state.showStar
    // state = {...state, showStar: !state.showStar}
    // console.log(state)
  },
  [ADD_TAG] (state, data) {
    let index = getIndex(state.list, state.activeItem.id)
    state.list[index].tagList.push({
      'name': data
    })
  },
  [SHOW_TAG_LIST] (state) {
    state.showNoteList = !state.showNoteList
  },
  [SHOW_NOTE_LIST] (state) {
    state.showNoteList = true
  }
}

export default mutations

@@ -4,134 +4,18 @@
 ort 
-%7B RENDER_NOTE, ADD_NOTE, DELETE_NOTE, UPDATE_NOTE, STAR_NOTE, GET_NOTELIST, GET_STAR, ADD_TAG, SHOW_TAG_LIST, SHOW_NOTE_LIST %7D
+* as types
  fro
@@ -159,16 +159,22 @@
  = %7B%0A  %5B
+types.
 RENDER_N
@@ -441,24 +441,30 @@
 New%0A  %7D,%0A  %5B
+types.
 ADD_NOTE%5D (s
@@ -677,16 +677,22 @@
   %7D,%0A  %5B
+types.
 DELETE_N
@@ -915,16 +915,22 @@
   %7D,%0A  %5B
+types.
 UPDATE_N
@@ -1016,16 +1016,22 @@
   %7D,%0A  %5B
+types.
 STAR_NOT
@@ -1145,24 +1145,30 @@
 tar%0A  %7D,%0A  %5B
+types.
 GET_NOTELIST
@@ -1215,16 +1215,22 @@
   %7D,%0A  %5B
+types.
 GET_STAR
@@ -1371,16 +1371,22 @@
   %7D,%0A  %5B
+types.
 ADD_TAG%5D
@@ -1527,24 +1527,30 @@
  %7D)%0A  %7D,%0A  %5B
+types.
 SHOW_TAG_LIS
@@ -1615,16 +1615,22 @@
   %7D,%0A  %5B
+types.
 SHOW_NOT

update mutation-type