import {
  addQueryParams, errorHandler, fetchRespHandler, getAuthHeaderOptions
} from "../utils/http";

const API_ENDPOINT = process.env.REACT_APP_OTA_SERVICE_URL;

const issuesAPI = {
  deleteIssue: async (id, token) =>
    fetch(`${API_ENDPOINT}/issues/${id}`, {
      method: "DELETE",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getIssue: async (id, token) =>
    fetch(`${API_ENDPOINT}/issues/${id}`, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getIssues: async (search, token) => {
    const u = addQueryParams(`${API_ENDPOINT}/issues`, search);
    return fetch(u, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler);
  },

};

export default issuesAPI;
