import Axios from "axios";

class Fetcher {
  constructor(url) {
    // this.url = url;
    // console.log("first request!!!");
    // return new Response("Working!!!" + url)
  }

  // Endpoints Here
  // GET /api/

  // POST /api/
  login(values) {
    return this.postAuxEnd("/auth/login", values);
  }

  //----------------------------------------  -----
  // Unified call below
  //----------------------------------------  -----

  async getAuxEnd(uri, reqData) {
    const endPoint =
      (process.env.AUX_ENDPOINT || "http://localhost:50016/api") + uri;
    console.log("Checking endpoint get request", endPoint);
    try {
      const response = await Axios.get(endPoint);
      console.log(response.data); // Log the response data if needed.
      return response.data;
    } catch (error) {
      this.handleAxiosError(error);
    }
  }

  async postAuxEnd(uri, reqData) {
    const endPoint =
      (process.env.AUX_ENDPOINT || "http://localhost:50016/api") + uri;
    console.log("Checking endpoint post request", endPoint);
    try {
      const response = await Axios.post(endPoint, reqData);
      console.log(response.data); // Log the response data if needed.
      return response.data;
    } catch (error) {
      this.handleAxiosError(error);
    }
  }

  handleAxiosError(error) {
    if (error.response) {
      // Response status is an error code.
      console.log(error.response.status);
    } else if (error.request) {
      // Response not received though the request was sent.
      console.log(error.request);
    } else {
      // An error occurred when setting up the request.
      console.log(error.message);
    }
  }
}

export default Fetcher;
