import { baseAPI } from "@/shared/api/baseAPI";
import { IFetch } from "@/shared/types/fetch-type";
import { IList } from "@/shared/types/list-type";
import { IReport } from "@/shared/types/report-type";
import axios from "axios";
import { create } from "zustand";

interface IFetchReports extends IList {
  results: IReport[];
}

interface IMapStore extends IFetch {
  data: IFetchReports;
  getReports: (categories: string) => Promise<void>;
}

export const useMapStore = create<IMapStore>((set) => ({
  data: {
    count: 0,
    previous: null,
    next: null,
    results: [],
  },
  isLoading: false,
  error: "",
  getReports: async (categories: string = "1,2,3,4,5,6") => {
    try {
      set({ isLoading: true });

      const response = await axios.get<IFetchReports>(
        `${baseAPI}/report/?category=${categories}`
      );

      set({ data: response.data });
    } catch (error: any) {
      set({ error: error.message });
    } finally {
      set({ isLoading: false });
    }
  },
}));
