

import { defineStore } from 'pinia';
import { tagsClass } from '/nerv-lib/saas/view/system/application';
export const useTags = defineStore({
  id: 'tags',
  state() {
    return { tagList: [] };
  },
  getters: {
    getTags: (state: any) => state.tagList,
  },
  actions: {
    addTags(val: tagsClass) {
      if (val.check) {
        const index = this.tagList.findIndex(
          (x) => x.currentUrl && x.currentUrl === val.currentUrl,
        );
        if (index === -1) {
          this.tagList.push(val as never);
        }
      } else {
        const index = this.tagList.findIndex((x) => x.name === val.name);
        if (index === -1) {
          this.tagList.push(val as never);
        }
      }
      sessionStorage.setItem('tagList', JSON.stringify(this.tagList));
    },
    initTags() {
      const sessionTagList = sessionStorage.getItem('tagList');
      if (sessionTagList) {
        this.tagList = JSON.parse(sessionTagList);
      }
    },
    clearTags() {
      this.tagList = [];
      sessionStorage.setItem('tagList', JSON.stringify(this.tagList));
    },
    unshiftTags(val: tagsClass) {
      this.tagList.unshift(val as never);
      sessionStorage.setItem('tagList', JSON.stringify(this.tagList));
    },
    sliceTags(index: number) {
      this.tagList = this.tagList.slice(index, 1);
      sessionStorage.setItem('tagList', JSON.stringify(this.tagList));
    },
    spliceTags(index: number) {
      this.tagList.splice(index, 1);
      sessionStorage.setItem('tagList', JSON.stringify(this.tagList));
    },
    updateTagTitle(index: number, val: String) {
      this.tagList[index].title = val;
      sessionStorage.setItem('tagList', JSON.stringify(this.tagList));
    },
  },
  // 开启数据缓存
  // persist: {
  //   enabled: true,
  // },
});
