/* Xeslint-disable */
import store from "../store";

var stringifyPrimitive = function(v) {
  switch (typeof v) {
    case "string":
      return v;

    case "boolean":
      return v ? "true" : "false";

    case "number":
      return isFinite(v) ? v : "";

    default:
      return "";
  }
};

export default {
  status(response) {
    if (response.status >= 200 && response.status < 300) {
      return Promise.resolve(response);
    } else {
      store.commit("logItem", "API error: " + response.statusText);
      return Promise.reject(new Error(response.statusText));
    }
  },
  json(response) {
    return response.json();
  },
  patchAuthorizedHeaders() {
    return {
      //Accept: "application/json, text/plain, */*",
      Accept: "application/json",
      "Content-Type": "application/json-patch+json",
      Authorization: "Bearer " + store.state.apiToken
    };
  },
  postAuthorizedHeaders() {
    return {
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer " + store.state.apiToken
    };
  },
  postUnAuthorizedHeaders() {
    return {
      Accept: "application/json",
      "Content-Type": "application/json"
    };
  },
  fetchPostNoAuthOptions(data) {
    return {
      method: "post",
      mode: "cors",
      headers: this.postUnAuthorizedHeaders(),
      body: JSON.stringify(data)
    };
  },
  fetchPostOptions(data) {
    return {
      method: "post",
      mode: "cors",
      headers: this.postAuthorizedHeaders(),
      body: JSON.stringify(data)
    };
  },
  fetchGetOptions() {
    /* GET WITH AUTH */
    return {
      method: "get",
      mode: "cors",
      headers: this.postAuthorizedHeaders()
    };
  },
  APIUrl(apiPath) {
    if ("" == store.state.apiUrl) {
      //construct the api url and store it
      //development location?
      if (
        window.location.hostname == "localhost" &&
        window.location.port == "8080"
      ) {
        store.commit("setAPIURL", "http://localhost:7575/api/v8.0/");
        store.commit("setHelpURL", "http://localhost:7575/docs/");
        store.commit(
          "logItem",
          "apiutil::APIUrl -> setting to dev. mode: " + store.state.apiUrl
        );
      } else {
        //production location <protocol>//<hostname>:<port>/
        store.commit(
          "setHelpURL",
          window.location.protocol + "//" + window.location.host + "/docs/"
        );
        store.commit(
          "setAPIURL",
          window.location.protocol + "//" + window.location.host + "/api/v8.0/"
        );
        store.commit(
          "logItem",
          "apiutil::APIUrl -> setting to: " + store.state.apiUrl
        );
      }
    }
    return store.state.apiUrl + apiPath;
  },
  buildQuery(obj, sep, eq, name) {
    sep = sep || "&";
    eq = eq || "=";
    if (obj === null) {
      obj = undefined;
    }

    if (typeof obj === "object") {
      return Object.keys(obj)
        .map(function(k) {
          var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
          if (Array.isArray(obj[k])) {
            return obj[k]
              .map(function(v) {
                return ks + encodeURIComponent(stringifyPrimitive(v));
              })
              .join(sep);
          } else {
            return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
          }
        })
        .filter(Boolean)
        .join(sep);
    }

    if (!name) return "";
    return (
      encodeURIComponent(stringifyPrimitive(name)) +
      eq +
      encodeURIComponent(stringifyPrimitive(obj))
    );
  }
};
