import { defineStore } from "pinia";

export const useLocalStore = defineStore(
  "local",
  () => {
    const locals = ref<Local[]>([
      { name: "探学楼", x: 0, y: 0 },
      { name: "探真楼", x: 0, y: 0 },
      { name: "探问楼", x: 0, y: 0 },
      { name: "探理楼", x: 0, y: 0 },
    ]);
    const initialized = computed(() => {
      let total = 0;
      locals.value.forEach((item) => {
        if (item.x !== 0) {
          total += 1;
        }
        if (item.y !== 0) {
          total += 1;
        }
      });

      return total === locals.value.length * 2;
    });

    // 设置x,y坐标
    const setLocal = (newlocal: Local) => {
      locals.value.forEach((local, i) => {
        if (local.name === newlocal.name) {
          locals.value[i] = { ...newlocal };
        }
      });
    };
    return {
      locals,
      initialized,
      setLocal,
    };
  },
  {
    persist: true,
  }
);
