<template>
  <BContainer id="container" :toast="{ root: true }" class="pageNationGeneral bg0">
    <TopBackBar :title="title" />
    <GeneralList :list="generalList" />
    <BottomBar />
  </BContainer>
</template>

<script lang="ts">
/*declare const staticValues: {
    serverNick: string,
    mapName: string,
    unitSet: string,
};*/
</script>
<script lang="ts" setup>
import TopBackBar from "@/components/TopBackBar.vue";
import BottomBar from "@/components/BottomBar.vue";
import { BContainer } from "bootstrap-vue-3";
import { onMounted, ref } from "vue";
import { SammoAPI } from "./SammoAPI";
import { merge2DArrToObjectArr } from "./util/merge2DArrToObjectArr";
import type { GeneralListItem } from "./defs/API/Nation";
import GeneralList from "./components/GeneralList.vue";

const generalList = ref(new Map<number, GeneralListItem>());

const title = "세력 장수";

onMounted(async () => {
  try {
    const { column, list, permission } = await SammoAPI.Nation.GeneralList();
    //XXX: 로직상 똑같은데....
    if (permission == 0) {
      const rawGeneralList = merge2DArrToObjectArr(column, list);
      generalList.value = new Map(rawGeneralList.map((v) => [v.no, { permission, ...v }]));
    } else if (permission == 1) {
      const rawGeneralList = merge2DArrToObjectArr(column, list);
      generalList.value = new Map(rawGeneralList.map((v) => [v.no, { permission, ...v }]));
    } else if ([2, 3, 4].includes(permission)) {
      const rawGeneralList = merge2DArrToObjectArr(column, list);
      generalList.value = new Map(rawGeneralList.map((v) => [v.no, { permission, ...v }]));
    } else {
      throw `?? ${permission}`;
    }

  } catch (e) {
    console.error(e);
    throw e;
  }
});
</script>
