import { defineStore } from 'pinia';
import { type ResponseVue, type DataItem } from '@/components/mt-edit/store/types';

export interface DataMapState {
  dataMap: Record<string, DataItem>;
  isLoading: boolean;
  lastUpdate: Date | null;
}

export const useDataMapStore = defineStore('dataMap', {
  state: (): DataMapState => ({
    dataMap: {},
    isLoading: false,
    lastUpdate: null
  }),

  getters: {
    getDataMap: (state) => state.dataMap,
    isDataLoaded: (state) => Object.keys(state.dataMap).length > 0,
    getDataMapById: (state) => (id: string) => state.dataMap[id]
  },

  actions: {
    setDataMap(data: Record<string, DataItem>) {
      this.dataMap = data;
      this.lastUpdate = new Date();
    },

    updateDataMapEntry(id: string, dataItem: DataItem) {
      this.dataMap[id] = dataItem;
    },

    clearDataMap() {
      this.dataMap = {};
      this.lastUpdate = null;
    },

    setLoading(loading: boolean) {
      this.isLoading = loading;
    }
  }
});
