const fs = require('fs');

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

/**
 * @param {string} host
 * @param {string} productId
 * @param {string} integrationId
 * @param {string} integrationPath
 * @param {string} token
 * @returns {Promise<*>}
 */
async function updateIntegration(host, productId, integrationId, integrationPath, token) {
  let data;

  try {
    data = JSON.parse(fs.readFileSync(integrationPath, 'utf-8'));
  } catch (e) {
    // ignore
    return Promise.resolve();
  }

  return axios.patch(
    `https://api.${host}/v2/products/${productId}/integrations/${integrationId}`,
    data,
    {
      headers: {
        Authorization: `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
    },
  );
}

module.exports = updateIntegration;
