import { URLExt } from "@jupyterlab/coreutils";

import { ServerConnection } from "@jupyterlab/services";

type UserInfo = {
  name: string;
  sub: string;
  display_name: string;
};

export async function userInfo(): Promise<UserInfo> {
  return requestAPI<UserInfo>("user");
}

export async function submitNotebook(json: string): Promise<any> {
  return requestAPI<any>("submit", {
    body: json,
    method: "POST",
  });
}

async function requestAPI<T>(
  endPoint = "",
  init: RequestInit = {},
): Promise<T> {
  // Make request to Jupyter API
  const settings = ServerConnection.makeSettings();
  const requestUrl = URLExt.join(
    settings.baseUrl,
    "urncjp", // API Namespace
    endPoint,
  );

  let response: Response;
  try {
    response = await ServerConnection.makeRequest(requestUrl, init, settings);
  } catch (error) {
    throw new ServerConnection.NetworkError(error as any);
  }

  let data: any = await response.text();

  if (data.length > 0) {
    try {
      data = JSON.parse(data);
    } catch (error) {
      console.log("Not a JSON response body.", response, error);
    }
  }

  if (!response.ok) {
    throw new ServerConnection.ResponseError(response, data.message || data);
  }

  return data;
}
