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

/**
 * @typedef APIAuthAssumeRoleResponse
 * @property {string} account_id
 * @property {string} account_internal_name
 * @property {string} principal_id
 * @property {string} principal_type
 * @property {boolean} livemode
 * @property {string} expires_at
 * @property {string} token
 * @property {boolean} password_is_weak
 */

/**
 * @param {string} host
 * @param {string} accountId
 * @param {string} token
 * @returns {Promise<string>}
 */
function assumeRole(host, accountId, token) {
  return axios
    .post(
      `https://api.${host}/v2/auth/assume_role`,
      {
        role_name: 'KASKO/KaskoWebappDeploymentRole',
        account_id: accountId,
      },
      {
        headers: {
          Authorization: `Bearer ${token}`,
          'Content-Type': 'application/json',
        },
      },
    )
    .then(
      /**
       * @param {{ data: APIAuthAssumeRoleResponse }} response
       */
      (response) => response?.data?.token,
    );
}

module.exports = assumeRole;
