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

//Added the token we got from the first authorization and set it as the auth token, and that allowed us to hit the request

const API_ENDPOINT = process.env.REACT_APP_OTA_SERVICE_URL;

const supersetAPI = {
  getGuestToken: async(token) => {
    const u = addQueryParams(`${API_ENDPOINT}/dashboard/guest-token`);
    let res = await fetch(u, {
      method: "GET",
      headers: Object.assign(
        //{ "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
    let r = await res.json()
    let q = r["token"]
    return q
  },
  getEmbeddedDashboards: async(token) => {
    const u = addQueryParams(`${API_ENDPOINT}/dashboard/embedded-dashboards`);
    let res = await fetch(u, {
      method: "GET",
      headers: Object.assign(
        getAuthHeaderOptions(token)
      ),
    })
    if(res.status !== 200){
      return [{title: "dashboard", embedded_id: GetSupersetDashboardID()}]
    }
    let r = await res.json()
    return r
  },
  SupersetDashboardURL: () => {
    const SUPERSET_BASE_URL = process.env.REACT_APP_SUPERSET_URL;
    return SUPERSET_BASE_URL
  },
  SupersetDashboardID: () => {
    return GetSupersetDashboardID()
  }
}

const GetSupersetDashboardID = () => {
  const SUPERSET_BASE_ID = process.env.REACT_APP_SUPERSET_KEYS_LIST;
  return SUPERSET_BASE_ID
}

export default supersetAPI;