import { defineStore } from "pinia";
import { ref } from "vue";
import type {
  DataResponse,
  DataRow,
} from "../interface/response/insignia/Insignia";
import { useCounterMixin } from "@/stores/mixin";

const { date2Thai, onSearchDataTable } = useCounterMixin();

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

  async function fetchData(data: DataResponse[], insigniaType?: string) {
    const list = data.map((e) => ({
      ...e,
      insigniaType: insigniaType,
      createdAt: e.createdAt ? date2Thai(e.createdAt, false, true) : "-",
      lastUpdatedAt: e.lastUpdatedAt
        ? date2Thai(e.lastUpdatedAt, false, true)
        : "-",
    }));

    row.value = list;
    rowMain.value = list;
  }

  function onSearchData(keyword: string, columns: any = []) {
    row.value = onSearchDataTable(keyword, rowMain.value, columns);
  }

  return {
    fetchData,
    row,
    onSearchData,
  };
});
