import { defineStore } from 'pinia';

export const CommonStore = defineStore('common', {
  state: () => {
    return {
      currentPagePath: '/',
      alivePages: [
        {
          title: '首页',
          path: '/',
          name: 'Index',
        },
      ] as any[],
      dictData: {} as any,
    };
  },
  actions: {
    setCurrentPage(path: string) {
      this.currentPagePath = path;
    },
    addAlivePage(data: any) {
      this.setCurrentPage(data.path);
      const has = this.alivePages.some((page) => {
        if (page.path === data.path) {
          return true;
        }
      });
      if (!has) {
        this.alivePages.push(data);
      }
    },
    removeAlivePage(data: any) {
      this.alivePages.some((page, index) => {
        if (page.path === data.path) {
          this.alivePages.splice(index, 1);
          return true;
        }
      });
    },
    setAllDictData(data: any) {
      this.dictData = data;
    },
    getDictData(key: string) {
      if (this.dictData[key]) {
        return this.dictData[key];
      } else {
        return {
          A: [],
          O: {},
        };
      }
    },
  },
});
