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

import { useCounterMixin } from "@/stores/mixin";

import type {
  DataResponse,
  DataRow,
} from "../interface/response/position/ListType";

const { date2Thai } = useCounterMixin();

export const usePositionTypeDataStore = defineStore("PositionTypeData", () => {
  const row = ref<DataRow[]>([]);
  const rowMain = ref<DataRow[]>([]);

  /**
   * บันทึกข้อมูลลงใน  row.value
   * @param data ข้อมูลรายการจาก API
   */
  async function save(data: DataResponse[]) {
    const list = data.map((e) => ({
      ...e,
      posTypes: undefined,
      posTypeId: e.posTypes?.id,
      createdAt: e.createdAt ? date2Thai(e.createdAt, false, true) : "-",
      lastUpdatedAt: e.lastUpdatedAt
        ? date2Thai(e.lastUpdatedAt, false, true)
        : "-",
    })) satisfies DataRow[];
    row.value = list;
    rowMain.value = list;
  }
  return {
    save,
    row,
    rowMain,
  };
});
