const axios = require("axios");

function WopiAPI() {}

WopiAPI.prototype.getPassports = function(fileID) {
  const {
    publicKey,
    uuid,
    token,
    deviceHash
  } = window.viamApi.getConfig().headers;
  const requestConfig = {
    url: `${window.WOPI_URL}getPassports`,
    method: "POST",
    headers: {
      publicKey,
      uuid,
      token,
      deviceHash,
      fileID: encodeURI(fileID)
    }
  };

  return axios(requestConfig);
};

WopiAPI.prototype.putDocument = function(path, accessToken, file) {
  const {
    publicKey,
    uuid,
    token,
    deviceHash
  } = window.viamApi.getConfig().headers;
  path = path[0] === "/" ? path : `/${path}`;
  path = encodeURI(path);
  const requestConfig = {
    url: `${window.WOPI_URL}files${path}/contents`,
    method: "POST",
    headers: {
      publicKey,
      uuid,
      token,
      deviceHash
    },
    params: {
      access_token: accessToken
    },
    data: file
  };

  return axios(requestConfig);
};

module.exports = WopiAPI;