const { defaultProvider } = require("@aws-sdk/credential-provider-node");
const { InstanceRegion, InstanceIsLocalStackEnabled, InstanceLocalStackEndpoint } = require("../utils/instanceMgr");
const aws = require("@aws-sdk/client-ses");
const nodemailer = require("nodemailer");
const logger = require("../utils/logger");

const sesConfig = {
  apiVersion: "latest",
  credentials: defaultProvider(),
  region: InstanceRegion()
};

if (InstanceIsLocalStackEnabled()) {
  sesConfig.endpoint = InstanceLocalStackEndpoint();
  logger.logger.debug(`SES Mailer set to LocalStack end point: ${sesConfig.endpoint}`);
}

const ses = new aws.SES(sesConfig);

let transporter = nodemailer.createTransport({
  SES: { ses, aws }
});

module.exports = transporter;
