import { setStore, getStore } from '@/util/store'
import website from '@/const/website.js'
import { diff } from '@/util/util'
const tagWel = website.fistPage
const isFirstPage = website.isFirstPage
// 处理首个标签
function setFistTag(list) {
  if (list.length === 1) {
    list[0].close = false
  } else {
    list.forEach(ele => {
      if (ele.value === tagWel.value && isFirstPage === false) {
        ele.close = false
      } else {
        ele.close = true
      }
    })
  }
}

const tags = {
  state: {
    tagList: getStore({ name: 'tagList' }) || [],
    tag: getStore({ name: 'tag' }) || {},
    tagWel: tagWel
  },
  mutations: {
    ADD_TAG: (state, action) => {
      state.tag = action
      setStore({ name: 'tag', content: state.tag, type: 'session' })
      if (state.tagList.some(ele => diff(ele, action))) return
      state.tagList.push(action)
      setFistTag(state.tagList)
      setStore({ name: 'tagList', content: state.tagList, type: 'session' })
    },
    DEL_TAG: (state, action) => {
     state.tagList = state.tagList.filter(item => {
            return !diff(item, action)
          })
          setFistTag(state.tagList)
          setStore({ name: 'tagList', content: state.tagList, type: 'session' })
    },
   DEL_ALL_TAG: (state) => {
        state.tagList = [state.tagWel]
        setStore({ name: 'tagList', content: state.tagList, type: 'session' })
      },
      DEL_TAG_OTHER: (state) => {
        state.tagList = state.tagList.filter(item => {
          if (item.value === state.tag.value) {
            return true
          } else if (!website.isFirstPage && item.value === website.fistPage.value) {
            return true
          }
        })
        setFistTag(state.tagList)
        setStore({ name: 'tagList', content: state.tagList, type: 'session' })
      }
  }
}
export default tags
