import { defineStore } from 'pinia';

const appStore = defineStore('app', {
  state: () => ({
    // 场景列表
    list: [],
    //当前场景
    currentScene: {},
    //场景数据
    metadata: {},
    //当前一级分组
    currentCatalogRoot: {},
    //当前二级分组
    currentSecondary: {},
    //二级分组
    secondaryList: {},
    //当前场景分组
    currentScenesList: {},
  }),
  getters: {},
  actions: {
    // 设置当前二级分组
    setCurrentSecondary(payload) {
      this.currentSecondary = payload;

      let arr = this.list.filter((item) => {
        return this.currentSecondary.id == item.category;
      });

      this.setCurrentScenesList(arr.sort((a, b) => a.weight - b.weight))
    },

    // 设置当前场景列表
    setCurrentScenesList(payload) {
      this.currentScenesList = payload;
    },

    // 设置当前一级分组
    setCurrentCatalogRoot(payload) {
      this.currentCatalogRoot = payload;
      let temp = [];
      payload.children &&
        payload.children.forEach((item) => {
          this.metadata.catalogs.forEach((sub) => {
            if (item == sub.id) {
              if (this.list.some(iii => iii.category == sub.id)) {
                temp.push(sub);
              }
            }
          });
        });

      this.setSecondaryList(temp)
    },

    // 设置当前二级分组列表
    setSecondaryList(payload) {
      this.secondaryList = payload
      if (payload.length > 0) {
        this.setCurrentSecondary(payload[0])
      }
    },
  },
});

export default appStore;
