const axios = require('../../axios');

/**
 * @param {string} host
 * @param {string | null} webappId
 * @param {string} fileId
 * @param {string} token
 * @returns {Promise<*>}
 */
function deployWebapp(host, webappId, fileId, token) {
  if (!webappId) {
    return axios.post(
      `https://api.${host}/webapps`,
      {
        file_id: fileId,
      },
      {
        headers: {
          Authorization: `Bearer ${token}`,
          'Content-Type': 'application/json',
        },
      },
    );
  }

  return axios.patch(
    `https://api.${host}/webapps/${webappId}`,
    {
      file_id: fileId,
    },
    {
      headers: {
        Authorization: `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
    },
  );
}

module.exports = deployWebapp;
