import { ref, computed } from "vue";
import { defineStore } from "pinia";
import type {
  DataOptionInsignia,
  DataOption,
  InsigniasType,
  InsigniasTypeSub,
} from "@/modules/04_registryPerson/interface/index/Main";
import type { ResponseObject as Insignia } from "@/modules/07_insignia/interface/response/Main";

export const useInsigniaDataStore = defineStore("insigniaDataStore", () => {
  const insigniaOption = ref<InsigniasType[]>([]);

  const insigniaTypeOp = ref<InsigniasType[]>([]);
  const insigniaTypeOpMain = ref<InsigniasType[]>([]);
  const allNameInsignia = ref<InsigniasTypeSub[]>([]);

  function mapInsigniaOption(resData: InsigniasType[]) {
    insigniaTypeOpMain.value = resData;
    insigniaTypeOp.value = resData;

    allNameInsignia.value = resData
      .flatMap((item: InsigniasType) => item.insignias)
      .map((i: InsigniasTypeSub) => ({
        ...i,
        name: `${i.name} (${i.shortName})`,
      }));
  }

  return {
    insigniaOption,
    mapInsigniaOption,
    insigniaTypeOp,
    insigniaTypeOpMain,
    allNameInsignia,
  };
});
