import { defineStore } from "pinia";
import { ref, computed } from "vue";

const useBuildingStore = defineStore("buildingInfo", () => {
  // 所有棟別
  const buildings = ref([]);

  const selectedBuilding = ref(null);

  // 所有大小類系統
  const mainSubSys = ref([]);

  // 所有大類
  const mainSys = computed(() =>
    mainSubSys.value.map(({ main_system_tag, full_name }) => ({
      main_system_tag,
      full_name,
    }))
  );

  const subSys = computed(() => {
    let subPages = [];

    mainSubSys.value.forEach(({ main_system_tag, history_Sub_systems }) => {
      subPages = [
        ...subPages,
        ...history_Sub_systems.map((Sub) => ({
          ...Sub,
          main_system_tag,
          key: Sub.sub_system_tag,
        })),
      ];
    });

    return subPages;
  });

  return {
    buildings,
    selectedBuilding,
    mainSubSys,
    mainSys,
    subSys,
  };
});

export default useBuildingStore;
