"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint @typescript-eslint/no-inferrable-types: "off" */
/**
 * Event Meditator
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Hook events and log them
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventMediator = void 0;
var ContextError_1 = require("../error/ContextError");
var EventMediator = /** @class */ (function () {
    /**
     * Initialize mediator
     *
     * @param _log     - A PSR-3 style logger
     * @param _emitter - An event emitter
     */
    function EventMediator(_log, _emitter) {
        var _this = this;
        this._log = _log;
        this._emitter = _emitter;
        this._emitter.on('delta-publish', function (msg) {
            return _this._log.notice('Published delta to exchange', msg);
        });
        this._emitter.on('document-processed', function (msg) {
            return _this._log.notice('Deltas on document processed successfully. Document has been ' +
                'marked as completely processed.', msg);
        });
        this._emitter.on('amqp-conn-warn', function (msg) {
            return _this._log.warning('AMQP Connection Error', msg);
        });
        this._emitter.on('amqp-reconnect', function () {
            return _this._log.warning('...attempting to re-establish AMQP connection');
        });
        this._emitter.on('amqp-reconnected', function () {
            return _this._log.warning('AMQP re-connected');
        });
        this._emitter.on('request-received', function (ctx) {
            if (ctx === void 0) { ctx = {}; }
            return _this._log.notice("Request Received", ctx);
        });
        this._emitter.on('response-sent', function (ctx) {
            if (ctx === void 0) { ctx = {}; }
            return _this._log.notice("Response Sent", ctx);
        });
        this._emitter.on('callback-sent', function (ctx) {
            if (ctx === void 0) { ctx = {}; }
            return _this._log.notice("Callback Sent", ctx);
        });
        this._emitter.on('callback-success', function (ctx) {
            if (ctx === void 0) { ctx = {}; }
            return _this._log.notice("Callback Success", ctx);
        });
        this._emitter.on('callback-failure', function (ctx) {
            if (ctx === void 0) { ctx = {}; }
            return _this._log.notice("Callback Failure", ctx);
        });
        this._emitter.on('rate-error', function (ctx) {
            if (ctx === void 0) { ctx = {}; }
            return _this._log.error("Rate Error", ctx);
        });
        this._emitter.on('error', function (arg) { return _this._handleError(arg); });
    }
    /**
     * Handle an error event
     *
     * @param e - any
     */
    EventMediator.prototype._handleError = function (e) {
        var msg = '';
        var context = {};
        if (e instanceof Error) {
            msg = e.message;
            if (ContextError_1.hasContext(e)) {
                context = e.context;
            }
            context.stack = e.stack;
        }
        this._log.error(msg, context);
    };
    return EventMediator;
}());
exports.EventMediator = EventMediator;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRXZlbnRNZWRpYXRvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIkV2ZW50TWVkaWF0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLG9EQUFvRDtBQUNwRCwwREFBMEQ7QUFDMUQ7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQXFCRzs7O0FBSUgsc0RBQWlEO0FBRWpEO0lBQ0U7Ozs7O09BS0c7SUFDSCx1QkFDbUIsSUFBZSxFQUNmLFFBQXNCO1FBRnpDLGlCQXFEQztRQXBEa0IsU0FBSSxHQUFKLElBQUksQ0FBVztRQUNmLGFBQVEsR0FBUixRQUFRLENBQWM7UUFFdkMsSUFBSSxDQUFDLFFBQVEsQ0FBQyxFQUFFLENBQUMsZUFBZSxFQUFFLFVBQUEsR0FBRztZQUNuQyxPQUFBLEtBQUksQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDLDZCQUE2QixFQUFFLEdBQUcsQ0FBQztRQUFwRCxDQUFvRCxDQUNyRCxDQUFDO1FBRUYsSUFBSSxDQ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