"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IMAPSession = void 0;
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const IMAPFolder_1 = require("./IMAPFolder");
const IMAPFolderStatus_1 = require("./IMAPFolderStatus");
const IMAPMessage_1 = require("./IMAPMessage");
const Data_1 = require("../basetypes/Data");
const IMAPSyncResult_1 = require("./IMAPSyncResult");
// 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 { IMAPSession: _IMAPSession } = bindings;
/**
 * IMAPSession Class
 *
 * @public
 */
class IMAPSession extends bindings.IMAPSession {
    constructor() {
        super();
        const handle = new _IMAPSession();
        this.handle = handle;
    }
    /**
     * Set hostname
     *
     * @memberof IMAPSession
     */
    set hostname(hostname) {
        this.handle.hostname = hostname;
    }
    /**
     * Get hostname
     *
     * @type {string}
     * @memberof IMAPSession
     */
    get hostname() {
        return this.handle.hostname;
    }
    /**
     * Set port
     *
     * @memberof IMAPSession
     */
    set port(port) {
        this.handle.port = port;
    }
    /**
     * Get port
     *
     * @type {number}
     * @memberof IMAPSession
     */
    get port() {
        return this.handle.port;
    }
    /**
     * Set string
     *
     * @memberof IMAPSession
     */
    set username(username) {
        this.handle.username = username;
    }
    /**
     * Get string
     *
     * @type {string}
     * @memberof IMAPSession
     */
    get username() {
        return this.handle.username;
    }
    /**
     * Set password
     *
     * @memberof IMAPSession
     */
    set password(password) {
        this.handle.password = password;
    }
    /**
     * Get password
     *
     * @type {string}
     * @memberof IMAPSession
     */
    get password() {
        return this.handle.password;
    }
    /**
     * Set checkCertificateEnabled
     *
     * @memberof IMAPSession
     */
    set checkCertificateEnabled(isEnabled) {
        this.handle.checkCertificateEnabled = isEnabled;
    }
    /**
     * Get checkCertificateEnabled
     *
     * @type {boolean}
     * @memberof IMAPSession
     */
    get checkCertificateEnabled() {
        return this.handle.checkCertificateEnabled;
    }
    /**
     * Set OAuth2Token
     *
     * @memberof IMAPSession
     */
    set OAuth2Token(OAuth2Token) {
        this.handle.OAuth2Token = OAuth2Token;
    }
    /**
     * Get OAuth2Token
     *
     * @type {string}
     * @memberof IMAPSession
     */
    get OAuth2Token() {
        return this.handle.OAuth2Token;
    }
    /**
     * Set authType
     *
     * @memberof IMAPSession
     */
    set authType(authType) {
        this.handle.authType = authType;
    }
    /**
     * Get authType
     *
     * @type {AuthType}
     * @memberof IMAPSession
     */
    get authType() {
        return this.handle.authType;
    }
    /**
     * Set connectionType
     *
     * @memberof IMAPSession
     */
    set connectionType(connectionType) {
        this.handle.connectionType = connectionType;
    }
    /**
     * Get connectionType
     *
     * @type {ConnectionType}
     * @memberof IMAPSession
     */
    get connectionType() {
        return this.handle.connectionType;
    }
    /**
     * Set strictGost flag (to force using GOST for TLS)
     *
     * @memberof IMAPSession
     */
    set strictGost(value) {
        this.handle.strictGost = value;
    }
    /**
     * Get strictGost flag (to force using GOST for TLS)
     *
     * @type {Boolean}
     * @memberof IMAPSession
     */
    get strictGost() {
        return this.handle.strictGost;
    }
    /**
     * Validate exist account
     *
     * @param {string} from address
     * @returns {number} 0 - success, other - failure
     *
     * @memberof IMAPSession
     */
    async checkAccount(from) {
        return this.handle.checkAccount(from);
    }
    /**
     * Disconnect connection
     *
     * @memberof IMAPSession
     */
    disconnect() {
        this.handle.disconnect();
    }
    /**
     * Disconnect connection asynchronously
     *
     * @memberof IMAPSession
     */
    disconnectAsync() {
        return new Promise((resolve, reject) => {
            this.handle.disconnectAsync((error) => {
                if (error) {
                    reject(error);
                    return;
                }
                resolve();
            });
        });
    }
    /**
     * Validate connection status
     *
     * @returns {boolean}
     *
     * @memberof IMAPSession
     */
    isDisconnected() {
        return this.handle.isDisconnected();
    }
    /**
     * Login if needed to session
     *
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    loginIfNeeded() {
        return this.handle.loginIfNeeded();
    }
    /**
     * Asynchronously login if needed to session
     *
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    loginIfNeededAsync() {
        return new Promise((resolve, reject) => {
            this.handle.loginIfNeededAsync((error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Connect if needed to session
     *
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    connectIfNeeded() {
        return this.handle.connectIfNeeded();
    }
    /**
     * Asynchronously connect if needed to session
     *
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    connectIfNeededAsync() {
        return new Promise((resolve, reject) => {
            this.handle.connectIfNeededAsync((error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Create IMAP folder
     *
     * @param {string} folder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    createFolder(folder) {
        return this.handle.createFolder(folder);
    }
    /**
    * Create IMAP folder
    *
    * @param {string} folder
    * @param {string} flag
    * @return {*}  {number}
    * @memberof IMAPSession
    */
    createSpecialFolder(folder, flag) {
        return this.handle.createSpecialFolder(folder, flag);
    }
    /**
     * Delete IMAP folder
     *
     * @param {string} folder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    deleteFolder(folder) {
        return this.handle.deleteFolder(folder);
    }
    /**
     * Rename folder
     *
     * @param {string} folder
     * @param {string} folderOther
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    renameFolder(folder, folderOther) {
        return this.handle.renameFolder(folder, folderOther);
    }
    /**
     * Subscribe to folder
     *
     * @param {string} folder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    subscribeFolder(folder) {
        return this.handle.subscribeFolder(folder);
    }
    /**
     * Unsubscribe to folder
     *
     * @param {string} folder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    unsubscribeFolder(folder) {
        return this.handle.unsubscribeFolder(folder);
    }
    /**
     * Fetch all subscribed folders
     *
     * @return {*}  {IMAPFolder[]}
     * @memberof IMAPSession
     */
    fetchSubscribedFolders() {
        const folders = this.handle.fetchSubscribedFolders();
        const result = [];
        folders === null || folders === void 0 ? void 0 : folders.forEach((item) => {
            const value = new IMAPFolder_1.IMAPFolder();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    /**
     * Fetch all folders
     *
     * @return {*}  {IMAPFolder[]}
     * @memberof IMAPSession
     */
    fetchAllFolders() {
        const folders = this.handle.fetchAllFolders();
        const result = [];
        folders.forEach((item) => {
            const value = new IMAPFolder_1.IMAPFolder();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    /**
     * Fetch all folders asynchronously
     *
     * @return {*}  {IMAPFolder[]}
     * @memberof IMAPSession
     */
    fetchAllFoldersAsync() {
        return new Promise((resolve, reject) => {
            this.handle.fetchAllFoldersAsync((error, folders) => {
                if (error) {
                    return reject(error);
                }
                const result = [];
                folders.forEach((item) => {
                    const value = new IMAPFolder_1.IMAPFolder();
                    value.handle = item;
                    result.push(value);
                });
                resolve(result);
            });
        });
    }
    /**
     * Get folder status
     *
     * @param {string} folder Folder to fetch status
     * @return {IMAPFolderStatus} Folder status object
     * @memberof IMAPSession
     */
    folderStatus(folder) {
        const status = this.handle.folderStatus(folder);
        const result = new IMAPFolderStatus_1.IMAPFolderStatus();
        result.handle = status;
        return result;
    }
    /**
     * Get folder status asynchronously
     *
     * @param {string} folder Folder to fetch status
     * @return {IMAPFolderStatus} Folder status object
     * @memberof IMAPSession
     */
    folderStatusAsync(folder) {
        return new Promise((resolve, reject) => {
            this.handle.folderStatusAsync(folder, (error, status) => {
                if (error) {
                    return reject(error);
                }
                const result = new IMAPFolderStatus_1.IMAPFolderStatus();
                result.handle = status;
                resolve(result);
            });
        });
    }
    /**
     * Move messages
     *
     * @param {string} folder
     * @param {number} uidSet
     * @param {string} destFolder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    moveMessages(folder, uidSet, destFolder) {
        return this.handle.moveMessages(folder, uidSet, destFolder);
    }
    /**
     * Move messages asynchronously
     *
     * @param {string} folder
     * @param {number} uidSet
     * @param {string} destFolder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    moveMessagesAsync(folder, uidSet, destFolder) {
        return new Promise((resolve, reject) => {
            this.handle.moveMessagesAsync(folder, uidSet, destFolder, (error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Fetch messages by UID
     *
     * @param {string} folder foder to fetch messages from
     * @param {number} rangeStart Start of UIDs range, 0 by default
     * @param {number} rangeEnd End of UIDs range, "infinity" by default
     * @return {IMAPMessage[]} Array of fetched messages info
     * @memberof IMAPSession
     */
    fetchMessagesByUID(folder, rangeStart, rangeEnd) {
        const messages = this.handle.fetchMessagesByUID(folder, rangeStart, rangeEnd);
        const result = [];
        messages.forEach((item) => {
            const value = new IMAPMessage_1.IMAPMessage();
            value.handle = item;
            result.push(value);
        });
        return result;
    }
    /**
     * Fetch messages by UID asynchronously
     *
     * @param {string} folder foder to fetch messages from
     * @param {number} rangeStart Start of UIDs range, 0 by default
     * @param {number} rangeEnd End of UIDs range, "infinity" by default
     * @return {Promise<IMAPMessage[]>} Promise to return array of fetched messages info
     * @memberof IMAPSession
     */
    fetchMessagesByUIDAsync(folder, rangeStart, rangeEnd) {
        return new Promise((resolve, reject) => {
            this.handle.fetchMessagesByUIDAsync(folder, rangeStart, rangeEnd, (error, messages) => {
                if (error) {
                    reject(error);
                    return;
                }
                const result = [];
                messages.forEach((item) => {
                    const value = new IMAPMessage_1.IMAPMessage();
                    value.handle = item;
                    result.push(value);
                });
                resolve(result);
            });
        });
    }
    /**
     * Fetch messsage by uid
     *
     * @param {string} folder
     * @param {number} uid
     * @return {Data} Data object with message data
     * @memberof IMAPSession
     */
    fetchMessageByUID(folder, uid) {
        const hdata = this.handle.fetchMessageByUID(folder, uid);
        const data = new Data_1.Data();
        data.handle = hdata;
        return data;
    }
    /**
     * Fetch messsage by uid asynchonously
     *
     * @param {string} folder
     * @param {number} uid
     * @return {Promise<Data>} Promise with Data object with message data
     * @memberof IMAPSession
     */
    fetchMessageByUIDAsync(folder, uid) {
        return new Promise((resolve, reject) => {
            try {
                this.handle.fetchMessageByUIDAsync(folder, uid, (error, hdata) => {
                    if (error || !hdata.length()) {
                        reject(error);
                        return;
                    }
                    const data = new Data_1.Data();
                    if (hdata) {
                        data.handle = hdata;
                    }
                    resolve(data);
                });
            }
            catch (e) {
                reject(e);
            }
        });
    }
    /**
     * Fetch messsage by number
     *
     * @param {string} folder
     * @param {number} messageNumber
     * @return {Data}
     * @memberof IMAPSession
     */
    fetchMessageByNumber(folder, messageNumber) {
        const hdata = this.handle.fetchMessageByNumber(folder, messageNumber);
        const data = new Data_1.Data();
        data.handle = hdata;
        return data;
    }
    /**
     * Get isQResyncEnabled
     *
     * @type {boolean}
     * @memberof IMAPSession
     */
    get isQResyncEnabled() {
        return this.handle.isQResyncEnabled;
    }
    /**
     * Quick messsages synchronization by uid (with QRESYNC/CONDSTORE)
     *
     * @param {string} folder
     * @param {number} rangeStart? Start of UID range. Default value is 1
     * @param {number} rangeEnd? End of UID range. Default value is "int max" (to sync all messages)
     * @return {IMAPSyncResult | undefined}
     * @memberof IMAPSession
     */
    syncMessagesByUID(folder, modseq, rangeStart, rangeEnd) {
        const syncResultNative = this.handle.syncMessagesByUID(folder, modseq, rangeStart, rangeEnd);
        if (!syncResultNative) {
            return undefined;
        }
        const syncResult = new IMAPSyncResult_1.IMAPSyncResult();
        syncResult.handle = syncResultNative;
        return syncResult;
    }
    /**
     * Asynchronous quick messsages synchronization by uid (with QRESYNC/CONDSTORE)
     *
     * @param {string} folder
     * @param {number} rangeStart? Start of UID range. Default value is 1
     * @param {number} rangeEnd? End of UID range. Default value is "int max" (to sync all messages)
     * @return {IMAPSyncResult | undefined}
     * @memberof IMAPSession
     */
    syncMessagesByUIDAsync(folder, modseq) {
        return new Promise((resolve, reject) => {
            this.handle.syncMessagesByUIDAsync(folder, modseq, (error, syncResultNative) => {
                if (error) {
                    reject(error);
                }
                if (!syncResultNative) {
                    return resolve(undefined);
                }
                const syncResult = new IMAPSyncResult_1.IMAPSyncResult();
                syncResult.handle = syncResultNative;
                return resolve(syncResult);
            });
        });
    }
    /**
     * Store flags by number
     *
     * @param {string} folder
     * @param {number} uidSet
     * @param {IMAPStoreFlagsRequestKind} requestKind
     * @param {MessageFlag} flags
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    storeFlagsByUID(folder, uidSet, requestKind, flags) {
        return this.handle.storeFlagsByUID(folder, uidSet, requestKind, flags);
    }
    /**
     * Store flags by number
     *
     * @param {string} folder
     * @param {number} uidSet
     * @param {IMAPStoreFlagsRequestKind} requestKind
     * @param {MessageFlag} flags
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    storeFlagsByUIDAsync(folder, uidSet, requestKind, flags) {
        return new Promise((resolve, reject) => {
            this.handle.storeFlagsByUIDAsync(folder, uidSet, requestKind, flags, (error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Store flags by number
     *
     * @param {string} folder
     * @param {number} mnuber
     * @param {IMAPStoreFlagsRequestKind} requestKind
     * @param {MessageFlag} flags
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    storeFlagsByNumber(folder, mnuber, requestKind, flags) {
        return this.handle.storeFlagsByNumber(folder, mnuber, requestKind, flags);
    }
    /**
     * Append message
     *
     * @param {string} folder
     * @param {Data} mdata
     * @param {MessageFlag} flags
     * @return {*}  {number} Created UID
     * @memberof IMAPSession
     */
    appendMessage(folder, mdata, flags) {
        return this.handle.appendMessage(folder, mdata.handle, flags);
    }
    /**
     * Append message asynchronously
     *
     * @param {string} folder
     * @param {Data} mdata
     * @param {MessageFlag} flags
     * @return {*}  {Promise<number>} Created UID
     * @memberof IMAPSession
     */
    appendMessageAsync(folder, mdata, flags) {
        return new Promise((resolve, reject) => {
            this.handle.appendMessageAsync(folder, mdata.handle, flags, (error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Copy messages
     *
     * @param {string} folder
     * @param {string} destFolder
     * @param {number} locationStart
     * @param {number} [length]
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    copyMessages(folder, destFolder, locationStart, length) {
        return this.handle.copyMessages(folder, destFolder, locationStart, length);
    }
    /**
     * Expunge folder
     *
     * @param {string} folder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    expunge(folder) {
        return this.handle.expunge(folder);
    }
    /**
     * Expunge folder asynchronously
     *
     * @param {string} folder
     * @return {*}  {number}
     * @memberof IMAPSession
     */
    expungeAsync(folder) {
        return new Promise((resolve, reject) => {
            this.handle.expungeAsync(folder, (error, result) => {
                if (error) {
                    reject(error);
                }
                resolve(result);
            });
        });
    }
    /**
     * Get capabilityMove
     *
     * @type {string}
     * @memberof IMAPSession
     */
    get capabilityMove() {
        return this.handle.capabilityMove;
    }
    /**
     * Get capabilityCreateSpecialUse
     *
     * @type {string}
     * @memberof IMAPSession
     */
    get capabilityCreateSpecialUse() {
        return this.handle.capabilityCreateSpecialUse;
    }
}
exports.IMAPSession = IMAPSession;
//# sourceMappingURL=IMAPSession.js.map