"use strict";
/**
 * Delta Publisher
 *
 *  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/>.
 *
 * Publish delta message to a queue
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeltaPublisher = void 0;
var ContextError_1 = require("../error/ContextError");
var AmqpError_1 = require("../error/AmqpError");
var DeltaPublisher = /** @class */ (function () {
    /**
     * Delta publisher
     *
     * @param _emitter - event emitter instance
     * @param _ts_ctor - a timestamp constructor
     * @param _conn    - the amqp connection
     * @param _writer  - message writer
     */
    function DeltaPublisher(_emitter, _ts_ctor, _conn, _writer) {
        this._emitter = _emitter;
        this._ts_ctor = _ts_ctor;
        this._conn = _conn;
        this._writer = _writer;
    }
    /**
     * Publish quote message to exchange post-rating
     *
     * @param meta     - document meta data
     * @param delta    - delta
     * @param bucket   - bucket
     * @param ratedata - rate data bucket
     */
    DeltaPublisher.prototype.publish = function (meta, delta, bucket, ratedata) {
        var _this = this;
        var ts = this._ts_ctor();
        var headers = { version: 1, created: ts };
        return this._writer
            .write(ts, meta, delta, bucket, ratedata)
            .then(function (avro_buffer) {
            var channel = _this._conn.getAmqpChannel();
            if (!channel) {
                throw ContextError_1.context(new AmqpError_1.AmqpError('Error sending message: No channel'), {
                    doc_id: meta.id,
                    quote_id: meta.id,
                    delta_type: delta.type,
                    delta_ts: delta.timestamp,
                });
            }
            // we don't use a routing key; fanout exchange
            var published_successfully = channel.publish(_this._conn.getExchangeName(), '', avro_buffer, { headers: headers });
            if (!published_successfully) {
                throw ContextError_1.context(new Error('Delta publish failed'), {
                    doc_id: meta.id,
                    quote_id: meta.id,
                    delta_type: delta.type,
                    delta_ts: delta.timestamp,
                });
            }
        })
            .then(function (_) {
            _this._emitter.emit('delta-publish', {
                doc_id: meta.id,
                quote_id: meta.id,
                type: delta.type,
                delta_ts: delta.timestamp,
                exchange: _this._conn.getExchangeName(),
            });
        });
    };
    return DeltaPublisher;
}());
exports.DeltaPublisher = DeltaPublisher;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRGVsdGFQdWJsaXNoZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJEZWx0YVB1Ymxpc2hlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQXFCRzs7O0FBTUgsc0RBQThDO0FBQzlDLGdEQUE2QztBQUs3QztJQUNFOzs7Ozs7O09BT0c7SUFDSCx3QkFDbUIsUUFBc0IsRUFDdEIsUUFBNkIsRUFDN0IsS0FBcUIsRUFDckIsT0FBc0I7UUFIdEIsYUFBUSxHQUFSLFFBQVEsQ0FBYztRQUN0QixhQUFRLEdBQVIsUUFBUSxDQUFxQjtRQUM3QixVQUFLLEdBQUwsS0FBSyxDQUFnQjtRQUNyQixZQUFPLEdBQVAsT0FBTyxDQUFlO0lBQ3RDLENBQUM7SUFFSjs7Ozs7OztPQU9HO0lBQ0gsZ0NBQU8sR0FBUCxVQUNFLElBQWtCLEVBQ2xCLEtBQWlCLEVBQ2pCLE1BQTJCLEVBQzNCLFFBQTZCO1FBSi9CLGlCQWlEQztRQTNDQyxJQUFNLEVBQUUsR0FBRyxJQUFJLENBQUMsUUFBUSxFQUFFLENBQUM7UUFDM0IsSUFBTSxPQUFPLEdBQUcsRUFBQyxPQUFPLEVBQUUsQ0FBQyxFQUFFLE9BQU8sRUFBRSxFQUFFLEVBQUMsQ0FBQztRQUUxQyxPQUFPLElB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