import { defineStore } from "pinia";
import { ref } from "vue";
import type {
  DataResponse,
  DataRow,
} from "../interface/response/position/ListType";
import { useMainOptionDataStore } from "@/modules/01_metadata/stores/main";
import { useCounterMixin } from "@/stores/mixin";
const storeOption = useMainOptionDataStore();

const { date2Thai } = useCounterMixin();

export const usePositionDataStore = defineStore("PositionData", () => {
  const pathLocation = ref<string>("list_position");
  const row = ref<DataRow[]>([]);
  const rowMain = ref<DataRow[]>([]);

  function save(data: DataResponse[], posTypeName: string) {
    const list = data.map((e) => ({
      ...e,
      posTypes: undefined,
      posTypeId: e.posTypes?.id,
      posTypeName: posTypeName,
      posTypeRank: e.posTypes?.posTypeRank,
      createdAt: e.createdAt ? date2Thai(e.createdAt, false, true) : "-",
      lastUpdatedAt: e.lastUpdatedAt
        ? date2Thai(e.lastUpdatedAt, false, true)
        : "-",
      posLevelAuthority: e.posLevelAuthority
        ? storeOption.posLevelAuthorityConvert(e.posLevelAuthority)
        : "-",
    })) satisfies DataRow[];
    row.value = list;
    rowMain.value = list;
  }

  return {
    save,
    row,
    rowMain,
    pathLocation,
  };
});
