"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageBuilder = void 0;
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const Attachment_1 = require("./Attachment");
const MessageHeader_1 = require("./MessageHeader");
// tslint:disable-next-line: no-var-requires
let bindings;
try {
    bindings = require(path.join(__dirname, "../../lib/binding/trusted_mailcore2.node"));
}
catch (e) {
    bindings = require(path.join(__dirname, "../../lib/binding/trusted_mailcore2_no_crypto.node"));
}
// tslint:disable-next-line
const { MessageBuilder: _MessageBuilder } = bindings;
/**
 * MessageBuilder Class
 *
 * @public
 */
class MessageBuilder extends bindings.MessageBuilder {
    constructor() {
        super();
        const handle = new _MessageBuilder();
        this.handle = handle;
    }
    /**
     * Message header getter
     *
     *
     * @type {MessageHeader}
     * @memberof MessageBuilder
     */
    get header() {
        if (undefined === this.handle.header) {
            return undefined;
        }
        const value = new MessageHeader_1.MessageHeader();
        value.handle = this.handle.header;
        return value;
    }
    /**
     * Message header setter
     *
     *
     * @param {MessageHeader}  msgHeader
     * @memberof MessageBuilder
     */
    set header(msgHeader) {
        this.handle.header = msgHeader ? msgHeader.handle : undefined;
    }
    /**
     * HTML body getter
     *
     *
     * @type {string}
     * @memberof MessageBuilder
     */
    get htmlBody() {
        return this.handle.htmlBody;
    }
    /**
     * HTML body setter
     *
     *
     * @type {string | Buffer}
     * @memberof MessageBuilder
     */
    set htmlBody(bodyVal) {
        this.handle.htmlBody = bodyVal;
    }
    /**
     * Text body getter
     *
     *
     * @type {string}
     * @memberof MessageBuilder
     */
    get textBody() {
        return this.handle.textBody;
    }
    /**
     * Text body setter
     *
     *
     * @type {string}
     * @memberof MessageBuilder
     */
    set textBody(bodyVal) {
        this.handle.textBody = bodyVal;
    }
    /**
     * Attachments getter
     *
     *
     * @type {Attachment[]}
     * @memberof MessageBuilder
     */
    get attachments() {
        const attaches = this.handle.attachments;
        const result = [];
        if (undefined === attaches) {
            return result;
        }
        attaches.forEach((item) => {
            const value = new Attachment_1.Attachment();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    /**
     * Attachments setter
     *
     *
     * @type {Attachment[]}
     * @memberof MessageBuilder
     */
    set attachments(attaches) {
        const result = [];
        attaches.forEach((item) => {
            result.push(item.handle);
        });
        this.handle.attachments = result;
    }
    /**
     * Method to add attachments
     *
     *
     * @param {Attachment[]} attaches attachments to add
     * @memberof MessageBuilder
     */
    addAttachments(attaches) {
        const result = [];
        attaches.forEach((item) => {
            result.push(item.handle);
        });
        this.handle.addAttachments(result);
    }
    /**
     * Related attachments getter
     *
     *
     * @type {Attachment[]}
     * @memberof MessageBuilder
     */
    get relatedAttachments() {
        const attaches = this.handle.relatedAttachments;
        const result = [];
        if (undefined === attaches) {
            return result;
        }
        attaches.forEach((item) => {
            const value = new Attachment_1.Attachment();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    /**
     * related Attachments setter
     *
     *
     * @type {Attachment[]}
     * @memberof MessageBuilder
     */
    set relatedAttachments(attaches) {
        const result = [];
        attaches.forEach((item) => {
            result.push(item.handle);
        });
        this.handle.relatedAttachments = result;
    }
    /**
     * Method to add related attachments
     *
     *
     * @param {Attachment[]} attaches attachments to add as related to body
     * @memberof MessageBuilder
     */
    addRelatedAttachments(attaches) {
        const result = [];
        attaches.forEach((item) => {
            result.push(item.handle);
        });
        this.handle.addRelatedAttachments(result);
    }
    /**
     * Boundary prefix getter
     *
     *
     * @type {string}
     * @memberof MessageBuilder
     */
    get boundaryPrefix() {
        return this.handle.boundaryPrefix;
    }
    /**
     * Boundary prefix setter
     *
     *
     * @type {string}
     * @memberof MessageBuilder
     */
    set boundaryPrefix(prefVal) {
        this.handle.boundaryPrefix = prefVal;
    }
    /**
     * Builded message data getter
     *
     *
     * @type {Buffer}
     * @memberof MessageBuilder
     */
    get data() {
        return this.handle.data;
    }
    /**
     * Asynchronous method to get message data
     *
     *
     * @type {Buffer}
     * @memberof MessageBuilder
     */
    getDataAsync() {
        return new Promise((resolve, reject) => {
            this.handle.getDataAsync((error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Method to write message data
     *
     *
     * @param {string} fileName file path to save data
     * @memberof MessageBuilder
     */
    Write(fileName) {
        this.handle.Write(fileName);
    }
    /**
     * Method to write message data asynchronously
     *
     *
     * @param {string} fileName file path to save data
     * @memberof MessageBuilder
     */
    WriteAsync(fileName) {
        return new Promise((resolve, reject) => {
            this.handle.WriteAsync(fileName, (error) => {
                if (error) {
                    reject(error);
                }
                resolve();
            });
        });
    }
    /**
     * Method to set signature certificate
     *
     *
     * @param {string} certData Signer's certificate blob
     * @param {string} signerMail Signer's mail address (not a very useful parameter)
     * @memberof MessageBuilder
     */
    SetSignerCert(certData, signerMail) {
        this.handle.SetSignerCert(certData, signerMail);
    }
    /**
     * Method to set recipients certificates array
     *
     *
     * @param {string[]} certData Array of recipients certificate blobs
     * @memberof MessageBuilder
     */
    SetRecipientsCerts(certsData) {
        this.handle.SetRecipientsCerts(certsData);
    }
    /**
     * Key usage policy getter
     *
     *
     * @type {"kupNone" | "kupCheckKeyEnc" | "kupCheckKeyAgree" | "kupCheckOneOf"}
     * @memberof MessageBuilder
     */
    get keyUsagePolicy() {
        return this.handle.keyUsagePolicy;
    }
    /**
     * Key usage policy setter
     *
     *
     * @type {"kupNone" | "kupCheckKeyEnc" | "kupCheckKeyAgree" | "kupCheckOneOf"}
     * @memberof MessageBuilder
     */
    set keyUsagePolicy(policyVal) {
        this.handle.keyUsagePolicy = policyVal;
    }
    /**
     * Method to read data from memory
     *
     *
     * @param {Buffer | string} data memory buffer to read data
     * @memberof MessageBuilder
     */
    SetTempFolder(tmpFolderPath) {
        this.handle.SetTempFolder(tmpFolderPath);
    }
}
exports.MessageBuilder = MessageBuilder;
//# sourceMappingURL=MessageBuilder.js.map