"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.amqpwrapper = exports.amqpexchange = exports.amqpqueue = exports.Deferred = void 0;
var retry = require("async-retry");
var amqplib = require("amqplib");
var Config_1 = require("./Config");
var Crypt_1 = require("./Crypt");
var url = require("url");
var openflow_api_1 = require("@openiap/openflow-api");
var WebSocketServer_1 = require("./WebSocketServer");
var got = require("got");
var Deferred = /** @class */ (function () {
    function Deferred() {
        var me = this;
        this.promise = new Promise(function (resolve, reject) {
            me.reject = reject;
            me.resolve = resolve;
        });
    }
    return Deferred;
}());
exports.Deferred = Deferred;
var amqpqueue = /** @class */ (function () {
    function amqpqueue() {
    }
    return amqpqueue;
}());
exports.amqpqueue = amqpqueue;
var amqpexchange = /** @class */ (function () {
    function amqpexchange() {
    }
    return amqpexchange;
}());
exports.amqpexchange = amqpexchange;
// tslint:disable-next-line: class-name
var amqpwrapper = /** @class */ (function () {
    function amqpwrapper(logger, connectionstring) {
        this.AssertExchangeOptions = { durable: false, confirm: true };
        this.AssertQueueOptions = { durable: true };
        this.activecalls = {};
        // public queues: IHashTable<amqpqueue> = {};
        // private exchanges: IHashTable<amqpexchange> = {};
        // public queues: amqpqueue[] = [];
        this.exchanges = [];
        this.timeout = null;
        this._logger = logger;
        this.connectionstring = connectionstring;
        if (!openflow_api_1.NoderedUtil.IsNullEmpty(Config_1.Config.amqp_dlx)) {
            this.AssertQueueOptions.arguments = {};
            this.AssertQueueOptions.arguments['x-dead-letter-exchange'] = Config_1.Config.amqp_dlx;
        }
    }
    amqpwrapper.Instance = function () {
        return this._instance;
    };
    amqpwrapper.SetInstance = function (instance) {
        this._instance = instance;
    };
    amqpwrapper.prototype.connect = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, _b, _c, error_1;
            var _this = this;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        _d.trys.push([0, 5, , 6]);
                        if (this.timeout != null) {
                            this.timeout = null;
                        }
                        if (!(this.conn == null)) return [3 /*break*/, 2];
                        _a = this;
                        return [4 /*yield*/, amqplib.connect(this.connectionstring)];
                    case 1:
                        _a.conn = _d.sent();
                        this.conn.on('error', function (error) {
                            if (error.code != 404) {
                                _this._logger.error(error);
                            }
                        });
                        this.conn.on("close", function () {
                            _this._logger.info("[AMQP] reconnecting");
                            _this.conn = null;
                            if (_this.timeout == null) {
                                _this.timeout = setTimeout(_this.connect.bind(_this), 1000);
                            }
                        });
                        _d.label = 2;
                    case 2:
                        _b = this;
                        return [4 /*yield*/, this.conn.createConfirmChannel()];
                    case 3:
                        _b.channel = _d.sent();
                        _c = this;
                        return [4 /*yield*/, this.AddQueueConsumer("", null, null, function (msg, options, ack, done) {
                                if (_this.replyqueue) {
                                    WebSocketServer_1.WebSocketServer.websocket_queue_message_count.inc();
                                    WebSocketServer_1.WebSocketServer.websocket_queue_message_count.labels(_this.replyqueue.queue).inc();
                                    if (!openflow_api_1.NoderedUtil.IsNullUndefinded(_this.activecalls[options.correlationId])) {
                                        _this.activecalls[options.correlationId].resolve(msg);
                                        _this.activecalls[options.correlationId] = null;
                                        delete _this.activecalls[options.correlationId];
                                    }
                                }
                                ack();
                                done();
                            })];
                    case 4:
                        _c.replyqueue = _d.sent();
                        this.channel.on('close', function (e) {
                            try {
                                if (_this.conn != null)
                                    _this.conn.close();
                            }
                            catch (error) {
                            }
                            _this.conn = null;
                            _this.channel = null;
                            if (_this.timeout == null) {
                                _this.timeout = setTimeout(_this.connect.bind(_this), 1000);
                            }
                        });
                        this.channel.on('error', function (error) {
                            if (error.code != 404) {
                                _this._logger.error(error);
                            }
                        });
                        return [3 /*break*/, 6];
                    case 5:
                        error_1 = _d.sent();
                        console.error(error_1);
                        this.timeout = setTimeout(this.connect.bind(this), 1000);
                        return [3 /*break*/, 6];
                    case 6: return [2 /*return*/];
                }
            });
        });
    };
    amqpwrapper.prototype.RemoveQueueConsumer = function (queue) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(queue != null)) return [3 /*break*/, 2];
                        this._logger.info("[AMQP] Remove queue consumer " + queue.queue + "/" + queue.consumerTag);
                        if (!(this.channel != null)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.channel.cancel(queue.consumerTag)];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2: return [2 /*return*/];
                }
            });
        });
    };
    amqpwrapper.prototype.AddQueueConsumer = function (queuename, QueueOptions, jwt, callback) {
        return __awaiter(this, void 0, void 0, function () {
            var queue, tuser, name_1, skip, isrole, arr, arr_1, arr_2, tuser, name_2, tuser, isrole, skip, arr, arr_3, arr_4, q, _a, consumeresult;
            var _this = this;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (this.channel == null || this.conn == null)
                            throw new Error("Cannot Add new Queue Consumer, not connected to rabbitmq");
                        queue = (openflow_api_1.NoderedUtil.IsNullEmpty(queuename) ? "" : queuename);
                        if (!(Config_1.Config.amqp_force_queue_prefix && !openflow_api_1.NoderedUtil.IsNullEmpty(jwt) && !openflow_api_1.NoderedUtil.IsNullEmpty(queue))) return [3 /*break*/, 10];
                        if (!(queue.length == 24)) return [3 /*break*/, 8];
                        tuser = Crypt_1.Crypt.verityToken(jwt);
                        name_1 = tuser.username.split("@").join("").split(".").join("");
                        name_1 = name_1.toLowerCase();
                        skip = false;
                        if (tuser._id == queue) {
                            // Queue is for me
                            skip = false;
                        }
                        else if (tuser.roles != null) {
                            isrole = tuser.roles.filter(function (x) { return x._id == queue; });
                            if (isrole.length > 0)
                                skip = false;
                        }
                        if (!skip) return [3 /*break*/, 6];
                        return [4 /*yield*/, Config_1.Config.db.query({ _id: queue }, { name: 1 }, 1, 0, null, "users", jwt)];
                    case 1:
                        arr = _b.sent();
                        if (arr.length == 0)
                            skip = true;
                        if (!!skip) return [3 /*break*/, 3];
                        return [4 /*yield*/, Config_1.Config.db.query({ _id: queue }, { name: 1 }, 1, 0, null, "openrpa", jwt)];
                    case 2:
                        arr_1 = _b.sent();
                        if (arr_1.length == 0)
                            skip = true;
                        _b.label = 3;
                    case 3:
                        if (!!skip) return [3 /*break*/, 5];
                        return [4 /*yield*/, Config_1.Config.db.query({ _id: queue }, { name: 1 }, 1, 0, null, "workflow", jwt)];
                    case 4:
                        arr_2 = _b.sent();
                        if (arr_2.length == 0)
                            skip = true;
                        _b.label = 5;
                    case 5:
                        if (!skip) {
                            queue = name_1 + queue;
                        }
                        else {
                            this._logger.info("[SKIP] skipped force prefix for " + queue);
                        }
                        return [3 /*break*/, 7];
                    case 6:
                        this._logger.info("[SKIP] skipped force prefix for " + queue);
                        _b.label = 7;
                    case 7: return [3 /*break*/, 9];
                    case 8:
                        tuser = Crypt_1.Crypt.verityToken(jwt);
                        name_2 = tuser.username.split("@").join("").split(".").join("");
                        name_2 = name_2.toLowerCase();
                        queue = name_2 + queue;
                        _b.label = 9;
                    case 9: return [3 /*break*/, 16];
                    case 10:
                        if (!(queue.length == 24)) return [3 /*break*/, 16];
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(jwt)) return [3 /*break*/, 16];
                        tuser = Crypt_1.Crypt.verityToken(jwt);
                        isrole = tuser.roles.filter(function (x) { return x._id == queue; });
                        if (!(isrole.length == 0 && tuser._id != queue)) return [3 /*break*/, 16];
                        skip = false;
                        return [4 /*yield*/, Config_1.Config.db.query({ _id: queue }, { name: 1 }, 1, 0, null, "users", jwt)];
                    case 11:
                        arr = _b.sent();
                        if (arr.length == 0)
                            skip = true;
                        if (!!skip) return [3 /*break*/, 13];
                        return [4 /*yield*/, Config_1.Config.db.query({ _id: queue }, { name: 1 }, 1, 0, null, "openrpa", jwt)];
                    case 12:
                        arr_3 = _b.sent();
                        if (arr_3.length == 0)
                            skip = true;
                        _b.label = 13;
                    case 13:
                        if (!!skip) return [3 /*break*/, 15];
                        return [4 /*yield*/, Config_1.Config.db.query({ _id: queue }, { name: 1 }, 1, 0, null, "workflow", jwt)];
                    case 14:
                        arr_4 = _b.sent();
                        if (arr_4.length == 0)
                            skip = true;
                        _b.label = 15;
                    case 15:
                        if (!skip) {
                            throw new Error("Access denied creating consumer for " + queue);
                        }
                        _b.label = 16;
                    case 16:
                        q = new amqpqueue();
                        q.callback = callback;
                        q.QueueOptions = Object.assign({}, (QueueOptions != null ? QueueOptions : this.AssertQueueOptions));
                        if (openflow_api_1.NoderedUtil.IsNullEmpty(queue))
                            queue = "";
                        if (queue.startsWith("amq."))
                            queue = "";
                        if (openflow_api_1.NoderedUtil.IsNullEmpty(queue))
                            q.QueueOptions.autoDelete = true;
                        _a = q;
                        return [4 /*yield*/, this.channel.assertQueue(queue, q.QueueOptions)];
                    case 17:
                        _a.ok = _b.sent();
                        if (!(q && q.ok)) return [3 /*break*/, 19];
                        q.queue = q.ok.queue;
                        q.queuename = queuename;
                        return [4 /*yield*/, this.channel.consume(q.ok.queue, function (msg) {
                                _this.OnMessage(q, msg, q.callback);
                            }, { noAck: false })];
                    case 18:
                        consumeresult = _b.sent();
                        q.consumerTag = consumeresult.consumerTag;
                        this._logger.info("[AMQP] Added queue consumer " + q.queue + "/" + q.consumerTag);
                        _b.label = 19;
                    case 19: return [2 /*return*/, q];
                }
            });
        });
    };
    amqpwrapper.prototype.AddExchangeConsumer = function (exchange, algorithm, routingkey, ExchangeOptions, jwt, callback) {
        return __awaiter(this, void 0, void 0, function () {
            var tuser, name_3, q, _ok, AssertQueueOptions, _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (this.channel == null || this.conn == null)
                            throw new Error("Cannot Add new Exchange Consumer, not connected to rabbitmq");
                        if (Config_1.Config.amqp_force_exchange_prefix && !openflow_api_1.NoderedUtil.IsNullEmpty(jwt)) {
                            tuser = Crypt_1.Crypt.verityToken(jwt);
                            name_3 = tuser.username.split("@").join("").split(".").join("");
                            name_3 = name_3.toLowerCase();
                            exchange = name_3 + exchange;
                        }
                        q = new amqpexchange();
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(q.queue)) {
                            this.RemoveQueueConsumer(q.queue);
                        }
                        // q.ExchangeOptions = new Object((ExchangeOptions != null ? ExchangeOptions : this.AssertExchangeOptions));
                        q.ExchangeOptions = Object.assign({}, (ExchangeOptions != null ? ExchangeOptions : this.AssertExchangeOptions));
                        q.exchange = exchange;
                        q.algorithm = algorithm;
                        q.routingkey = routingkey;
                        q.callback = callback;
                        return [4 /*yield*/, this.channel.assertExchange(q.exchange, q.algorithm, q.ExchangeOptions)];
                    case 1:
                        _ok = _b.sent();
                        AssertQueueOptions = null;
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(Config_1.Config.amqp_dlx) && exchange == Config_1.Config.amqp_dlx) {
                            AssertQueueOptions = Object.create(this.AssertQueueOptions);
                            delete AssertQueueOptions.arguments;
                        }
                        _a = q;
                        return [4 /*yield*/, this.AddQueueConsumer("", AssertQueueOptions, jwt, q.callback)];
                    case 2:
                        _a.queue = _b.sent();
                        if (q.queue) {
                            this.channel.bindQueue(q.queue.queue, q.exchange, q.routingkey);
                            this._logger.info("[AMQP] Added exchange consumer " + q.exchange + ' to queue ' + q.queue.queue);
                        }
                        this.exchanges.push(q);
                        return [2 /*return*/, q];
                }
            });
        });
    };
    amqpwrapper.prototype.OnMessage = function (sender, msg, callback) {
        var _this = this;
        // sender._logger.info("OnMessage " + msg.content.toString());
        try {
            var now = new Date();
            // const seconds = (now.getTime() - sender.cli.lastheartbeat.getTime()) / 1000;
            // if (seconds >= Config.client_heartbeat_timeout) {
            //     try {
            //         sender.cli._logger.info("amqpwrapper.OnMessage: receive message for inactive client, nack message and try and close");
            //         this.channel.nack(msg);
            //         sender.cli.Close();
            //     } catch (error) {
            //         console.error(error);
            //     }
            //     return;
            // }
            if (msg == null) {
                return;
            }
            var correlationId = msg.properties.correlationId;
            var replyTo = msg.properties.replyTo;
            var consumerTag = msg.fields.consumerTag;
            var routingkey = msg.fields.routingkey;
            var exchange = msg.fields.exchange;
            var options = {
                correlationId: correlationId,
                replyTo: replyTo,
                consumerTag: consumerTag,
                routingkey: routingkey,
                exchange: exchange
            };
            var data = msg.content.toString();
            callback(data, options, function (nack) {
                try {
                    if (nack == false) {
                        console.log("nack message");
                        _this.channel.nack(msg);
                        // this.channel.nack(msg, false, true);
                        msg = null;
                        return;
                    }
                    _this.channel.ack(msg);
                }
                catch (error) {
                    console.error(error);
                }
            }, function (result) {
                // ROLLBACK
                // if (msg != null && !NoderedUtil.IsNullEmpty(replyTo)) {
                //     try {
                //         this.channel.sendToQueue(replyTo, Buffer.from(result), { correlationId: msg.properties.correlationId });
                //     } catch (error) {
                //         console.error("Error sending response to " + replyTo + " " + JSON.stringify(error))
                //     }
                // }
            });
        }
        catch (error) {
            console.error(error);
        }
    };
    amqpwrapper.prototype.sendWithReply = function (exchange, queue, data, expiration, correlationId) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (openflow_api_1.NoderedUtil.IsNullEmpty(correlationId))
                            correlationId = this.generateUuid();
                        this.activecalls[correlationId] = new Deferred();
                        if (!this.replyqueue) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.sendWithReplyTo(exchange, queue, this.replyqueue.queue, data, expiration, correlationId)];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2: return [2 /*return*/, this.activecalls[correlationId].promise];
                }
            });
        });
    };
    amqpwrapper.prototype.sendWithReplyTo = function (exchange, queue, replyTo, data, expiration, correlationId) {
        return __awaiter(this, void 0, void 0, function () {
            var options;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.channel == null || this.conn == null) {
                            throw new Error("Cannot send message, when not connected");
                        }
                        if (typeof data !== 'string' && !(data instanceof String)) {
                            data = JSON.stringify(data);
                        }
                        this._logger.info("send to queue: " + queue + " exchange: " + exchange + " with reply to " + replyTo);
                        options = { mandatory: true };
                        options.replyTo = replyTo;
                        if (openflow_api_1.NoderedUtil.IsNullEmpty(correlationId))
                            correlationId = this.generateUuid();
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(correlationId))
                            options.correlationId = correlationId;
                        if (expiration < 1)
                            expiration = Config_1.Config.amqp_default_expiration;
                        options.expiration = expiration.toString();
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(exchange)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.checkQueue(queue)];
                    case 1:
                        if (!(_a.sent())) {
                            throw new Error("No consumer listening at " + queue);
                        }
                        if (!this.channel.sendToQueue(queue, Buffer.from(data), options, function (err, ok) {
                            // console.log(err ? 'nacked' : 'acked');
                            // console.log(err);
                            // console.log(ok);
                        })) {
                            throw new Error("No consumer listening at " + queue);
                        }
                        WebSocketServer_1.WebSocketServer.websocket_queue_message_count.inc();
                        WebSocketServer_1.WebSocketServer.websocket_queue_message_count.labels(queue).inc();
                        return [3 /*break*/, 3];
                    case 2:
                        this.channel.publish(exchange, "", Buffer.from(data), options);
                        _a.label = 3;
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    amqpwrapper.prototype.send = function (exchange, queue, data, expiration, correlationId) {
        return __awaiter(this, void 0, void 0, function () {
            var options;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.channel == null || this.conn == null) {
                            throw new Error("Cannot send message, when not connected");
                        }
                        if (typeof data !== 'string' && !(data instanceof String)) {
                            data = JSON.stringify(data);
                        }
                        if (openflow_api_1.NoderedUtil.IsNullEmpty(correlationId))
                            correlationId = this.generateUuid();
                        this._logger.info("send to queue: " + queue + " exchange: " + exchange);
                        options = { mandatory: true };
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(correlationId))
                            options.correlationId = correlationId;
                        if (expiration < 1)
                            expiration = Config_1.Config.amqp_default_expiration;
                        options.expiration = expiration.toString();
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(exchange)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.checkQueue(queue)];
                    case 1:
                        if (!(_a.sent())) {
                            throw new Error("No consumer listening at " + queue);
                        }
                        if (!this.channel.sendToQueue(queue, Buffer.from(data), options, function (err, ok) {
                            // console.log(err ? 'nacked' : 'acked');
                            // console.log(err);
                            // console.log(ok);
                        })) {
                            throw new Error("No consumer listening at " + queue);
                        }
                        WebSocketServer_1.WebSocketServer.websocket_queue_message_count.inc();
                        WebSocketServer_1.WebSocketServer.websocket_queue_message_count.labels(queue).inc();
                        return [3 /*break*/, 3];
                    case 2:
                        this.channel.publish(exchange, "", Buffer.from(data), options);
                        _a.label = 3;
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    amqpwrapper.prototype.generateUuid = function () {
        return Math.random().toString() +
            Math.random().toString() +
            Math.random().toString();
    };
    amqpwrapper.parseurl = function (amqp_url) {
        var q = url.parse(amqp_url, true);
        q.username = "guest";
        q.password = "guest";
        if (q.port == null || q.port == "") {
            q.port = "15672";
        }
        if (q.auth != null && q.auth != "") {
            var arr = q.auth.split(':');
            q.username = arr[0];
            q.password = arr[1];
        }
        q.protocol = 'http://';
        return q;
    };
    // This will crash the channel, that does not seem scalable
    amqpwrapper.prototype.checkQueue = function (queuename) {
        return __awaiter(this, void 0, void 0, function () {
            var test, error_2;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!Config_1.Config.amqp_check_for_consumer) return [3 /*break*/, 5];
                        test = null;
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        if (Config_1.Config.amqp_check_for_consumer_count) {
                            return [2 /*return*/, this.checkQueueConsumerCount(queuename)];
                        }
                        return [4 /*yield*/, amqpwrapper.getqueue(Config_1.Config.amqp_url, '/', queuename)];
                    case 2:
                        test = _a.sent();
                        if (test == null) {
                            return [2 /*return*/, false];
                        }
                        return [3 /*break*/, 4];
                    case 3:
                        error_2 = _a.sent();
                        test = null;
                        return [3 /*break*/, 4];
                    case 4:
                        if (test == null || test.consumerCount == 0) {
                            return [2 /*return*/, false];
                        }
                        _a.label = 5;
                    case 5: return [2 /*return*/, true];
                }
            });
        });
    };
    amqpwrapper.prototype.checkQueueConsumerCount = function (queuename) {
        return __awaiter(this, void 0, void 0, function () {
            var result, error_3;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        result = false;
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, retry(function (bail) { return __awaiter(_this, void 0, void 0, function () {
                                var queue, hasConsumers;
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0: return [4 /*yield*/, amqpwrapper.getqueue(Config_1.Config.amqp_url, '/', queuename)];
                                        case 1:
                                            queue = _a.sent();
                                            hasConsumers = false;
                                            if (queue.consumers > 0) {
                                                hasConsumers = true;
                                            }
                                            if (!hasConsumers) {
                                                if (queue.consumer_details != null && queue.consumer_details.length > 0) {
                                                    // console.log(queue.consumer_details[0]);
                                                    hasConsumers = true;
                                                }
                                                else {
                                                    hasConsumers = false;
                                                }
                                            }
                                            if (hasConsumers == false) {
                                                hasConsumers = false;
                                                throw new Error("No consumer listening at " + queuename);
                                                // return bail();
                                            }
                                            return [2 /*return*/, hasConsumers];
                                    }
                                });
                            }); }, {
                                retries: 10,
                                minTimeout: 500,
                                maxTimeout: 500,
                                onRetry: function (error, count) {
                                    result = false;
                                    console.log("retry " + count + " error " + error.message + " getting " + url);
                                }
                            })];
                    case 2:
                        result = _a.sent();
                        return [3 /*break*/, 4];
                    case 3:
                        error_3 = _a.sent();
                        this._logger.debug(error_3.message ? error_3.message : error_3);
                        return [3 /*break*/, 4];
                    case 4:
                        if (result == true) {
                            return [2 /*return*/, result];
                        }
                        return [2 /*return*/, false];
                }
            });
        });
    };
    amqpwrapper.getvhosts = function (amqp_url) {
        return __awaiter(this, void 0, void 0, function () {
            var q, options, _url, response, payload;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        q = this.parseurl(amqp_url);
                        options = {
                            headers: {
                                'Content-type': 'application/x-www-form-urlencoded'
                            },
                            username: q.username,
                            password: q.password
                        };
                        _url = 'http://' + q.host + ':' + q.port + '/api/vhosts';
                        return [4 /*yield*/, got.get(_url, options)];
                    case 1:
                        response = _a.sent();
                        payload = JSON.parse(response.body);
                        return [2 /*return*/, payload];
                }
            });
        });
    };
    amqpwrapper.getqueues = function (amqp_url, vhost) {
        if (vhost === void 0) { vhost = null; }
        return __awaiter(this, void 0, void 0, function () {
            var q, options, _url, response, payload;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        q = this.parseurl(amqp_url);
                        options = {
                            headers: {
                                'Content-type': 'application/x-www-form-urlencoded'
                            },
                            username: q.username,
                            password: q.password
                        };
                        _url = 'http://' + q.host + ':' + q.port + '/api/queues';
                        if (!openflow_api_1.NoderedUtil.IsNullEmpty(vhost))
                            _url += '/' + encodeURIComponent(vhost);
                        return [4 /*yield*/, got.get(_url, options)];
                    case 1:
                        response = _a.sent();
                        payload = JSON.parse(response.body);
                        return [2 /*return*/, payload];
                }
            });
        });
    };
    amqpwrapper.getqueue = function (amqp_url, vhost, queuename) {
        return __awaiter(this, void 0, void 0, function () {
            var q, options, _url, response, payload;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        q = this.parseurl(amqp_url);
                        options = {
                            headers: {
                                'Content-type': 'application/x-www-form-urlencoded'
                            },
                            username: q.username,
                            password: q.password,
                            timeout: 500, retry: 1
                        };
                        _url = 'http://' + q.host + ':' + q.port + '/api/queues/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(queuename);
                        return [4 /*yield*/, got.get(_url, options)];
                    case 1:
                        response = _a.sent();
                        payload = JSON.parse(response.body);
                        return [2 /*return*/, payload];
                }
            });
        });
    };
    amqpwrapper.deletequeue = function (amqp_url, vhost, queuename) {
        return __awaiter(this, void 0, void 0, function () {
            var q, options, _url, response, payload;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        q = this.parseurl(amqp_url);
                        options = {
                            headers: {
                                'Content-type': 'application/x-www-form-urlencoded'
                            },
                            username: q.username,
                            password: q.password,
                            timeout: 500, retry: 1
                        };
                        _url = 'http://' + q.host + ':' + q.port + '/api/queues/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(queuename);
                        return [4 /*yield*/, got.delete(_url, options)];
                    case 1:
                        response = _a.sent();
                        payload = JSON.parse(response.body);
                        return [2 /*return*/, payload];
                }
            });
        });
    };
    amqpwrapper._instance = null;
    return amqpwrapper;
}());
exports.amqpwrapper = amqpwrapper;
//# sourceMappingURL=amqpwrapper.js.map