import cache from "@/utils/cache";
import website from "@/const/website";

import { ref } from "vue";
import { diff } from "@/utils/util";
import { ITagType } from "@/const/interface_type";
import store from "..";

const tagWel = website.fistPage;
const tagObj = ref<ITagType>({
  label: "", // 标题名称
  value: "", // 标题的路径
  params: "", // 标题的路径参数
  query: "" // 标题的参数
  // group: [] // 分组
});

export default {
  state: () => ({
    tagList: cache.session.getJSON("tagList") || [],
    tag: cache.session.getJSON("tag") || tagObj,
    tagWel: tagWel
  }),
  mutations: {
    ADD_TAG: (state: any, action: ITagType) => {
      state.tag = action;
      cache.session.setJSON("tag", state.tag);
      if (state.tagList.some((ele: any) => diff(ele, action))) return;
      state.tagList.push(action);
      cache.session.setJSON("tagList", state.tagList);
    },
    DEL_TAG: (state: any, action: ITagType) => {
      const query = store.getters.detailQuery;
      const isDel = Object.keys(query).find((item) => action.value.includes(item));
      if (isDel) store.commit("DEL_QUERY", isDel);
      state.tagList = state.tagList.filter((item: ITagType) => {
        return !diff(item, action);
      });
      cache.session.setJSON("tagList", state.tagList);
    },
    DEL_ALL_TAG: (state: any) => {
      state.tagList = [];
      store.commit("DEL_ALL_QUERY");
      cache.session.setJSON("tagList", state.tagList);
    },
    DEL_TAG_OTHER: (state: any) => {
      const query = store.getters.detailQuery;
      const isRetain = Object.keys(query).find((item) => state.tag.value.includes(item));
      if (isRetain) store.commit("DEL_OTHER_QUERY", isRetain);
      state.tagList = state.tagList.filter((item: ITagType) => {
        if (item.value === state.tag.value) {
          return true;
        }
      });
      cache.session.setJSON("tagList", state.tagList);
    }
  },
  actions: {}
};
