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

interface INews extends IList {
  results: IResult[];
}

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

class NewsStore {
  error: string;
  constructor() {
    this.error = "";
  }

  async getNews() {
    try {
      const response = await axios.get<INews>(`${baseAPI}/news/`);

      return response.data;
    } catch (error: unknown) {
      if (error instanceof AxiosError) {
        this.error = error.message;
      } else {
        this.error = "An error occurred.";
      }
    }
  }
}

export const newsStore = new NewsStore();
