import { baseAPI } from "@/Shared/API/baseAPI";
import { IFetch, IList } from "@/Shared/types";
import axios from "axios";
import { StaticImageData } from "next/image";
import { create } from "zustand";

interface INews {
  results: IResult[];
}

interface IResult {
  id: number;
  image: StaticImageData;
  title: string;
  description: string;
  created_at: string;
}

interface INewsHomeStore extends IFetch {
  data: IResult[];
  getNews: () => Promise<void>;
}

export const useNewsHome = create<INewsHomeStore>((set) => ({
  data: [],
  loading: false,
  error: "",
  getNews: async () => {
    try {
      set({ loading: true });
      const response = await axios.get<INews>(`${baseAPI}/news/`);

      set({ data: response.data.results.slice(0, 4) });
    } catch (error: any) {
      set({ error: error.message });
    } finally {
      set({ loading: false });
    }
  },
}));
