import { getItem, setItem } from "@/utils/storage";
import { TAGS_VIEW } from "@/constant/index.js";
export default {
  namespaced: true,
  state: () => ({
    sidebarOpened: true,
    tagsViewList: getItem(TAGS_VIEW) || []
  }),
  mutations: {
    triggerSidebarOpened(state) {
      state.sidebarOpened = !state.sidebarOpened;
    },

    //添加tags
    addTagsViewList(state, tag) {
      const isFind = state.tagsViewList.find((item) => {
        return item.path === tag.path;
      });
      //处理重复
      if (!isFind) {
        state.tagsViewList.push(tag);
        setItem(TAGS_VIEW, state.tagsViewList);
      }
    },

    /**
     * 删除 tag
     */
    removeTagsView(state, payload) {
      const tagsViewList = state.tagsViewList;
      if (payload.type == "index") {
        tagsViewList.splice(payload.index, 1);
      } else {
        tagsViewList.splice(tagsViewList.indexOf(payload.data), 1);
      }
      setItem(TAGS_VIEW, tagsViewList);
    },
    /**
     * 清空 tag
     */
    clearTag(state, menu) {
      if (menu != null) {
        state.tagsViewList = [menu]
      } else {
        state.tagsViewList = []
      }
      setItem(TAGS_VIEW, state.tagsViewList);
    }
  }
};
