"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmqpConnection = void 0;
/**
 * Connection to AMQP exchange
 */
var AmqpConnection = /** @class */ (function () {
    /**
     * Amqp Connection
     *
     * @param _conf    - amqp library
     * @param _conf    - amqp configuration
     * @param _emitter - event emitter instance
     */
    function AmqpConnection(_amqp, _conf, _emitter) {
        this._amqp = _amqp;
        this._conf = _conf;
        this._emitter = _emitter;
    }
    /**
     * Initialize connection
     */
    AmqpConnection.prototype.connect = function () {
        var _this = this;
        return this._amqp
            .connect(this._conf)
            .then(function (conn) {
            _this._conn = conn;
            /** If there is an error, attempt to reconnect
             *  Only hook this once because it will be re-hooked on each
             *  successive successful connection
             */
            _this._conn.once('error', function (e) {
                _this._emitter.emit('amqp-conn-warn', e);
                _this._reconnect();
            });
            return _this._conn.createChannel();
        })
            .then(function (ch) {
            _this._channel = ch;
            _this._channel.on('close', function () { return _this._reconnect(); });
            return _this._channel.assertExchange(_this._conf.exchange, 'fanout', {
                durable: true,
            });
        })
            .then(function (_) { });
    };
    /**
     * Attempt to re-establish the connection
     *
     * @param retry_count - the number of retries attempted
     */
    AmqpConnection.prototype._reconnect = function (retry_count) {
        var _this = this;
        if (retry_count === void 0) { retry_count = 0; }
        if (retry_count >= this._conf.retries) {
            throw new Error('Could not re-establish AMQP connection.');
        }
        this._emitter.emit('amqp-reconnect');
        this.connect()
            .then(function (_) {
            _this._emitter.emit('amqp-reconnected');
        })
            .catch(function (_) {
            var wait_ms = _this._conf.retry_wait;
            setTimeout(function () { return _this._reconnect(++retry_count); }, wait_ms);
        });
    };
    /**
     * Returns the exchange to publish to
     *
     * @return exchange name
     */
    AmqpConnection.prototype.getExchangeName = function () {
        return this._conf.exchange;
    };
    /**
     * Returns the amqp channel
     *
     * @return exchange name
     */
    AmqpConnection.prototype.getAmqpChannel = function () {
        if (!this._channel) {
            this._reconnect();
        }
        return this._channel;
    };
    /**
     * Close the amqp conenction
     */
    AmqpConnection.prototype.close = function () {
        if (this._conn) {
            this._conn.close.bind(this._conn);
        }
    };
    return AmqpConnection;
}());
exports.AmqpConnection = AmqpConnection;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQW1xcENvbm5lY3Rpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJBbXFwQ29ubmVjdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUEwQkE7O0dBRUc7QUFDSDtJQU9FOzs7Ozs7T0FNRztJQUNILHdCQUNtQixLQUFxQixFQUNyQixLQUFpQixFQUNqQixRQUFzQjtRQUZ0QixVQUFLLEdBQUwsS0FBSyxDQUFnQjtRQUNyQixVQUFLLEdBQUwsS0FBSyxDQUFZO1FBQ2pCLGFBQVEsR0FBUixRQUFRLENBQWM7SUFDdEMsQ0FBQztJQUVKOztPQUVHO0lBQ0gsZ0NBQU8sR0FBUDtRQUFBLGlCQTJCQztRQTFCQyxPQUFPLElBQUksQ0FBQyxLQUFLO2FBQ2QsT0FBTyxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUM7YUFDbkIsSUFBSSxDQUFDLFVBQUEsSUFBSTtZQUNSLEtBQUksQ0FBQyxLQUFLLEdBQUcsSUFBSSxDQUFDO1lBRWxCOzs7ZUFHRztZQUNILEtBQUksQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLE9BQU8sRUFBRSxVQUFBLENBQUM7Z0JBQ3hCLEtBQUksQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLGdCQUFnQixFQUFFLENBQUMsQ0FBQyxDQUFDO2dCQUN4QyxLQUFJLENBQUMsVUFBVSxFQUFFLENBQUM7WUFDcEIsQ0FBQyxDQUFDLENBQUM7WUFFSCxPQUFPLEtBQUksQ0FBQyxLQUFLLENBQUMsYUFBYSxFQUFFLENBQUM7UUFDcEMsQ0FBQyxDQUFDO2FBQ0QsSUFBSSxDQUFDLFVBQUMsRUFBbUI7WUFDeEIsS0FBSSxDQUFDLFF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