"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.rabbitmq = void 0;
const Config_1 = require("./Config");
const url = require("url");
const openflow_api_1 = require("@openiap/openflow-api");
const Logger_1 = require("./Logger");
const got = require("got");
class rabbitmq {
    static parseurl(amqp_url) {
        const q = url.parse(amqp_url, true);
        if (q.port == null || q.port == "") {
            q.port = "15672";
        }
        if (q.auth != null && q.auth != "") {
            const arr = q.auth.split(':');
            q.username = arr[0];
            q.password = arr[1];
        }
        else {
            q.username = Config_1.Config.amqp_username;
            q.password = Config_1.Config.amqp_password;
        }
        q.protocol = 'http://';
        return q;
    }
    // This will crash the channel, that does not seem scalable
    async checkQueue(queuename) {
        if (Config_1.Config.amqp_check_for_consumer) {
            let test = null;
            try {
                if (Config_1.Config.amqp_check_for_consumer_count) {
                    return this.checkQueueConsumerCount(queuename);
                }
                test = await rabbitmq.getqueue(Config_1.Config.amqp_url, '/', queuename);
                if (test == null) {
                    return false;
                }
            }
            catch (error) {
                test = null;
            }
            if (test == null || test.consumerCount == 0) {
                return false;
            }
        }
        return true;
    }
    async checkQueueConsumerCount(queuename) {
        let result = false;
        try {
            result = await (0, Logger_1.promiseRetry)(async () => {
                const queue = await rabbitmq.getqueue(Config_1.Config.amqp_url, '/', queuename);
                // const queue = await amqpwrapper.getqueue(queuename);
                let hasConsumers = false;
                if (queue.consumers > 0) {
                    hasConsumers = true;
                }
                if (!hasConsumers) {
                    if (queue.consumer_details != null && queue.consumer_details.length > 0) {
                        hasConsumers = true;
                    }
                    else {
                        hasConsumers = false;
                    }
                }
                if (hasConsumers == false) {
                    hasConsumers = false;
                    throw new Error("No consumer listening at " + queuename);
                    // return bail();
                }
                return hasConsumers;
            }, 10, 1000);
        }
        catch (error) {
            Logger_1.Logger.instanse.error("rabbitmq", "checkQueueConsumerCount", error);
        }
        if (result == true) {
            return result;
        }
        return false;
    }
    static async getvhosts(amqp_url) {
        const q = this.parseurl(amqp_url);
        const options = {
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            },
            username: q.username,
            password: q.password
        };
        const _url = 'http://' + q.host + ':' + q.port + '/api/vhosts';
        const response = await got.get(_url, options);
        const payload = JSON.parse(response.body);
        return payload;
    }
    static async getqueues(amqp_url, vhost = null) {
        const q = this.parseurl(amqp_url);
        const options = {
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            },
            username: q.username,
            password: q.password
        };
        let _url = 'http://' + q.host + ':' + q.port + '/api/queues';
        if (!openflow_api_1.NoderedUtil.IsNullEmpty(vhost))
            _url += '/' + encodeURIComponent(vhost);
        const response = await got.get(_url, options);
        const payload = JSON.parse(response.body);
        return payload;
    }
    static async getqueue(amqp_url, vhost, queuename) {
        const q = this.parseurl(amqp_url);
        const options = {
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            },
            username: q.username,
            password: q.password,
            timeout: 500, retry: 1
        };
        const _url = 'http://' + q.host + ':' + q.port + '/api/queues/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(queuename);
        const response = await got.get(_url, options);
        const payload = JSON.parse(response.body);
        return payload;
    }
    static async deletequeue(amqp_url, vhost, queuename) {
        const q = this.parseurl(amqp_url);
        const options = {
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            },
            username: q.username,
            password: q.password,
            timeout: 500, retry: 1
        };
        const _url = 'http://' + q.host + ':' + q.port + '/api/queues/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(queuename);
        const response = await got.delete(_url, options);
        const payload = JSON.parse(response.body);
        return payload;
    }
}
exports.rabbitmq = rabbitmq;
//# sourceMappingURL=rabbitmq.js.map