import axios from "axios";

export function postAuxEnd(uri: string, reqData: any): Promise<any> {
  const endPoint = import.meta.env.VITE_USERS_ENDPOINT + uri;
  const formData = new FormData();
  for (let value in reqData) {
    if (typeof reqData[value] === "object") {
      // for (let innerValue in reqData[value]) {
      //   let innerReqData = reqData[value]
      //   console.log('SAMPLE',innerReqData)
      //   formData.append(reqData[value][innerValue], reqData[value][innerValue]);
      // }
      // formData.append(value, JSON.stringify(reqData[value]));
      formData.append(value, reqData[value]);
    } else {
      formData.append(value, reqData[value]);
    }
  }
  return axios
    .post(endPoint, formData)
    .then((response: {}) => {
      //   if (response.data.internal_return == "-9999") {
      //     localStorage.clear();
      //     window.location.href = `/login?sessionExpired=true`;
      //   }
      return response;
    })
    .catch((error: any) => {
      if (error.response) {
        //response status is an error code
        console.log(
          "ERROR-------------------------------------------------------"
        );
        console.log(error.response.status);
        console.log(
          "ERROR-------------------------------------------------------"
        );
      } else if (error.request) {
        //response not received though the request was sent
        console.log(
          "ERROR2-------------------------------------------------------"
        );
        console.log(error?.request);
        console.log(
          "ERROR2-------------------------------------------------------"
        );
      } else {
        //an error occurred when setting up the request
        console.log(
          "ERROR3-------------------------------------------------------"
        );
        console.log(error);
        console.log(
          "ERROR3-------------------------------------------------------"
        );
      }
    });
}


export function getAuxEnd(uri: string, reqData: any): Promise<any> {
  const endPoint = import.meta.env.VITE_USERS_ENDPOINT + uri;
  const formData = new FormData();
  for (let value in reqData) {
    formData.append(value, reqData[value]);
  }
  return axios
    .get(endPoint, reqData)
    .then((response: {}) => {
      //   if (response.data.internal_return == "-9999") {
      //     localStorage.clear();
      //     window.location.href = `/login?sessionExpired=true`;
      //   }
      return response;
    })
    .catch((error: any) => {
      console.log(
        "ERROR3-------------------------------------------------------", error
      );
    });
}
