const state = () => ({
  visitedRoutes: [],
});
const getters = {
  visitedRoutes: state => state.visitedRoutes,
};
const mutations = {
  addVisitedRoute(state, route) {
    const target = state.visitedRoutes.find(item => item.path === route.path);
    if (target) {
      if (route !== target) Object.assign(target, route);
    } else state.visitedRoutes.push(Object.assign({}, route));
  },
  delVisitedRoute(state, path) {
    state.visitedRoutes.forEach((item, index) => {
      if (item.path === path) state.visitedRoutes.splice(index, 1);
    });
  },
  delOthersVisitedRoutes(state, path) {
    state.visitedRoutes = state.visitedRoutes.filter(
      item => item.meta.affix || item.path === path
    );
  },
  delLeftVisitedRoutes(state, path) {
    const idx = state.visitedRoutes.indexOf(
      state.visitedRoutes.find(item => item.path === path)
    );
    state.visitedRoutes = state.visitedRoutes.filter((item, index) => {
      return item.meta.affix || index >= idx;
    });
  },
  delRightVisitedRoutes(state, path) {
    const idx = state.visitedRoutes.indexOf(
      state.visitedRoutes.find(item => item.path === path)
    );
    state.visitedRoutes = state.visitedRoutes.filter((item, index) => {
      return item.meta.affix || index <= idx;
    });
  },
  delAllVisitedRoutes(state) {
    state.visitedRoutes = state.visitedRoutes.filter(item => item.meta.affix);
  },
};
const actions = {
  addVisitedRoute({ commit }, route) {
    commit('addVisitedRoute', route);
  },
  delVisitedRoute({ commit }, route) {
    commit('delVisitedRoute', route);
  },
  delOthersVisitedRoutes({ commit }, route) {
    commit('delOthersVisitedRoutes', route);
  },
  delLeftVisitedRoutes({ commit }, route) {
    commit('delLeftVisitedRoutes', route);
  },
  delRightVisitedRoutes({ commit }, route) {
    commit('delRightVisitedRoutes', route);
  },
  delAllVisitedRoutes({ commit }) {
    commit('delAllVisitedRoutes');
  },
};
export default { state, getters, mutations, actions };
