"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageParser = 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 { MessageParser: _MessageParser } = bindings;
/**
 * MessageParser Class
 *
 * @public
 */
class MessageParser extends bindings.MessageParser {
    /**
     * MessageParser class constructor
     *
     *
     * @param {string | Buffer} data Message data to parse
     * @param KeyUsagePolicy kuPolicy Key usage check policy
     * @memberof MessageParser
     */
    constructor(data, kuPolicy) {
        super();
        const handle = new _MessageParser(data, kuPolicy);
        this.handle = handle;
    }
    /**
     * Key usage policy getter
     *
     *
     * @type boolean
     * @memberof MessageParser
     */
    get doNotDecrypt() {
        return this.handle.doNotDecrypt;
    }
    /**
     * Key usage policy setter
     *
     *
     * @type boolean
     * @memberof MessageParser
     */
    set doNotDecrypt(decryptVal) {
        this.handle.doNotDecrypt = decryptVal;
    }
    /**
     * Key usage policy getter
     *
     *
     * @type KeyUsagePolicy
     * @memberof MessageParser
     */
    get keyUsagePolicy() {
        return this.handle.keyUsagePolicy;
    }
    /**
     * Key usage policy setter
     *
     *
     * @type KeyUsagePolicy
     * @memberof MessageParser
     */
    set keyUsagePolicy(policyVal) {
        this.handle.keyUsagePolicy = policyVal;
    }
    /**
     * Method to read data from file asynchronously
     *
     *
     * @param {string} fileName path to file with message to read
     * @memberof MessageParser
     */
    ReadAsync(fileName) {
        return new Promise((resolve, reject) => {
            this.handle.ReadAsync(fileName, ((error) => {
                if (error) {
                    reject(error);
                }
                resolve();
            }));
        });
    }
    /**
     * Method to read data from file synchronously
     *
     *
     * @param {string} fileName path to file with message to read
     * @memberof MessageParser
     */
    Read(fileName) {
        this.handle.Read(fileName);
    }
    /**
     * Method to set folder for temporary files
     *
     *
     * @param {Buffer | string} data memory buffer to read data
     * @memberof MessageParser
     */
    SetTempFolder(tmpFolderPath) {
        this.handle.SetTempFolder(tmpFolderPath);
    }
    /**
     * Method to read data from memory asynchronously
     *
     *
     * @param {Buffer | string} data memory buffer to read data
     * @memberof MessageParser
     */
    ImportAsync(data) {
        return new Promise((resolve, reject) => {
            this.handle.ImportAsync(data, (error) => {
                if (error) {
                    reject(error);
                }
                resolve();
            });
        });
    }
    /**
     * Method to read data from memory
     *
     *
     * @param {Buffer | string} data memory buffer to read data
     * @memberof MessageParser
     */
    Import(data) {
        this.handle.Import(data);
    }
    /**
     * Message header getter
     *
     *
     * @type {MessageHeader}
     * @memberof MessageParser
     */
    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 MessageParser
    //  */
    // set header(msgHeader: MessageHeader) {
    //     this.handle.header = msgHeader.handle;
    // }
    /**
     * Method to get rendered html from message
     *
     *
     * @type {string}
     * @memberof MessageParser
     */
    HtmlRendering() {
        return this.handle.HtmlRendering();
    }
    /**
     * Method to get rendered html body from message
     *
     *
     * @type {string}
     * @memberof MessageParser
     */
    HtmlBodyRendering() {
        return this.handle.HtmlBodyRendering();
    }
    /**
     * Method to get rendered plain text from message
     *
     *
     * @type {string}
     * @memberof MessageParser
     */
    PlainTextRendering() {
        return this.handle.PlainTextRendering();
    }
    /**
     * Method to get rendered plain text body from message
     *
     *
     * @type {string}
     * @memberof MessageParser
     */
    PlainTextBodyRendering() {
        return this.handle.PlainTextBodyRendering();
    }
    // /**
    //  * Main part getter
    //  *
    //  *
    //  * @type {MessagePart}
    //  * @memberof MessageParser
    //  */
    // get mainPart(): MessagePart {
    //     const value = new MessagePart();
    //     value.handle = this.handle.mainPart;
    //     return value;
    // }
    /**
     * Attachments getter
     *
     *
     * @type {Attachment[]}
     * @memberof MessageParser
     */
    get attachments() {
        const attaches = this.handle.attachments;
        const result = [];
        attaches.forEach((item) => {
            const value = new Attachment_1.Attachment();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    /**
     * HTML inline attachments getter
     *
     *
     * @type {Attachment[]}
     * @memberof MessageParser
     */
    get htmlInlineAttachments() {
        const attaches = this.handle.htmlInlineAttachments;
        const result = [];
        attaches.forEach((item) => {
            const value = new Attachment_1.Attachment();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    // /**
    //  * Required parts for rendering getter
    //  *
    //  *
    //  * @type {Part[]}
    //  * @memberof MessageParser
    //  */
    // get requiredPartsForRendering(): Part[] {
    //     const attaches: PartNativeBinding[] = this.handle.requiredPartsForRendering;
    //     const result: Part[] = [];
    //     attaches.forEach((item: PartNativeBinding) => {
    //         const value = new Part();
    //         value.handle = item;
    //         result.push(value);
    //     })
    //     return result;
    // }
    /**
     * Message is signed getter
     *
     *
     * @type {boolean}
     * @memberof MessageParser
     */
    get isSigned() {
        return this.handle.isSigned;
    }
    /**
     * Message is encrypted getter
     *
     *
     * @type {boolean}
     * @memberof MessageParser
     */
    get isEncrypted() {
        return this.handle.isEncrypted;
    }
    /**
     * Signed message verify result getter
     *
     *
     * @type {boolean}
     * @memberof MessageParser
     */
    get verifyResult() {
        return this.handle.verifyResult;
    }
    /**
     * Sertificate value getter (for signed messages)
     *
     *
     * @type {boolean}
     * @memberof MessageParser
     */
    get signerCertificate() {
        return this.handle.signerCertificate;
    }
    /**
     * Signature extended information getter
     *
     *
     * @type {boolean}
     * @memberof MessageParser
     */
    get extendedSignInfo() {
        return this.handle.extendedSignInfo;
    }
    /**
     * Encrypted message decrypt result getter
     *
     *
     * @type {boolean}
     * @memberof MessageParser
     */
    get decryptResult() {
        return this.handle.decryptResult;
    }
    /**
     * Encrypted message description of decrypt result getter
     *
     *
     * @type {string}
     * @memberof MessageParser
     */
    get decryptMessage() {
        return this.handle.decryptMessage;
    }
    /**
     * Clears retained message data (and closes opened file)
     *
     *
     * @type {string}
     * @memberof MessageParser
     */
    clear() {
        return this.handle.clear();
    }
    /**
    * Method to set license for portable version
    *
    *
    * @param {string} data license
    * @memberof MessageParser
    */
    static SetLicense(lic) {
        bindings.MessageParser.SetLicense(lic);
    }
}
exports.MessageParser = MessageParser;
//# sourceMappingURL=MessageParser.js.map