var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { AppApiHostname } from "../EnvironmentConfig";
import ClusterKeyAuthorization from "../Middleware/ClusterKeyAuthorization";
import BaseService from "./BaseService";
import Protocol from "../../Types/API/Protocol";
import Route from "../../Types/API/Route";
import URL from "../../Types/API/URL";
import API from "../../Utils/API";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class MailService extends BaseService {
    async sendMail(mail, options) {
        var _a;
        const body = Object.assign(Object.assign({}, mail), { toEmail: mail.toEmail.toString() });
        if (options && options.mailServer) {
            body["SMTP_ID"] = (_a = options.mailServer.id) === null || _a === void 0 ? void 0 : _a.toString();
            /*
             * host/port/secure are optional on EmailServer because HTTP-API
             * transports (e.g. Microsoft Graph) don't use them. Only serialize the
             * SMTP-only fields when they're actually present.
             */
            body["SMTP_TRANSPORT_TYPE"] =
                options.mailServer.transportType || undefined;
            body["SMTP_USERNAME"] = options.mailServer.username || undefined;
            body["SMTP_EMAIL"] = options.mailServer.fromEmail.toString();
            body["SMTP_FROM_NAME"] = options.mailServer.fromName;
            body["SMTP_IS_SECURE"] =
                options.mailServer.secure === undefined
                    ? undefined
                    : options.mailServer.secure;
            body["SMTP_PORT"] = options.mailServer.port
                ? options.mailServer.port.toNumber()
                : undefined;
            body["SMTP_HOST"] = options.mailServer.host
                ? options.mailServer.host.toString()
                : undefined;
            body["SMTP_PASSWORD"] = options.mailServer.password || undefined;
            body["SMTP_AUTH_TYPE"] = options.mailServer.authType || undefined;
            body["SMTP_CLIENT_ID"] = options.mailServer.clientId || undefined;
            body["SMTP_CLIENT_SECRET"] = options.mailServer.clientSecret || undefined;
            body["SMTP_TOKEN_URL"] = options.mailServer.tokenUrl
                ? options.mailServer.tokenUrl.toString()
                : undefined;
            body["SMTP_SCOPE"] = options.mailServer.scope || undefined;
            body["SMTP_OAUTH_PROVIDER_TYPE"] =
                options.mailServer.oauthProviderType || undefined;
        }
        if (options === null || options === void 0 ? void 0 : options.userOnCallLogTimelineId) {
            body["userOnCallLogTimelineId"] =
                options.userOnCallLogTimelineId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.projectId) {
            body["projectId"] = options.projectId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.incidentId) {
            body["incidentId"] = options.incidentId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.alertId) {
            body["alertId"] = options.alertId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.alertEpisodeId) {
            body["alertEpisodeId"] = options.alertEpisodeId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.monitorId) {
            body["monitorId"] = options.monitorId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.scheduledMaintenanceId) {
            body["scheduledMaintenanceId"] =
                options.scheduledMaintenanceId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.statusPageId) {
            body["statusPageId"] = options.statusPageId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.statusPageAnnouncementId) {
            body["statusPageAnnouncementId"] =
                options.statusPageAnnouncementId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.userId) {
            body["userId"] = options.userId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.onCallPolicyId) {
            body["onCallPolicyId"] = options.onCallPolicyId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.onCallPolicyEscalationRuleId) {
            body["onCallPolicyEscalationRuleId"] =
                options.onCallPolicyEscalationRuleId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.onCallDutyPolicyExecutionLogTimelineId) {
            body["onCallDutyPolicyExecutionLogTimelineId"] =
                options.onCallDutyPolicyExecutionLogTimelineId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.onCallScheduleId) {
            body["onCallScheduleId"] = options.onCallScheduleId.toString();
        }
        if (options === null || options === void 0 ? void 0 : options.teamId) {
            body["teamId"] = options.teamId.toString();
        }
        return await API.post({
            url: new URL(Protocol.HTTP, AppApiHostname, new Route("/api/notification/email/send")),
            data: body,
            headers: Object.assign({}, ClusterKeyAuthorization.getClusterKeyHeaders()),
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], MailService.prototype, "sendMail", null);
export default new MailService();
//# sourceMappingURL=MailService.js.map