"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ConsulType = require("consul");
const settings_1 = require("./settings");
class Observer {
    constructor(consul, service) {
        this.services = [];
        this.error = null;
        this.listener = null;
        let self = this;
        this.watcher = consul.watch({
            method: function (data, callback) {
                data.service = service;
                consul.catalog.service.nodes(data, callback);
            }
        });
        this.watcher.on('change', function (data, res) {
            self.error = null;
            self.services = data;
            if (self.listener != null) {
                self.listener(data, self.error);
            }
        });
        this.watcher.on('error', function (err, res) {
            self.services = [];
            self.error = err;
            if (self.listener != null) {
                self.listener(self.services, err);
            }
        });
    }
    getServices() {
        return this.services;
    }
    getError(clean = false) {
        let error = this.error;
        if (clean) {
            this.error = null;
        }
        return error;
    }
    setListener(listener) {
        this.listener = listener;
    }
    destroy() {
        this.error = null;
        this.listener = null;
        this.services = [];
        this.watcher.end();
    }
}
class Consul {
    constructor(tag) {
        this.observers = {};
        this.settings = settings_1.default.Instance.getConsul(tag);
        this.consul = new ConsulType({
            host: this.settings.host,
            port: String(this.settings.port),
            secure: this.settings.secure
        });
    }
    newObserver(name, error) {
        if (this.observers == null)
            this.observers = {};
        if (this.observers[name] != null) {
            if (error != undefined) {
                error(new Error("This observer already exists; Returned the existing one!"));
            }
            return this.observers[name];
        }
        let observer = new Observer(this.consul, name);
        this.observers[name] = observer;
        return this.observers[name];
    }
    deleteObserver(name) {
        if (this.observers != null && this.observers[name] != null) {
            if (typeof this.observers[name].destroy === "function") {
                this.observers[name].destroy();
            }
            delete this.observers[name];
        }
    }
    observer(name) {
        if (this.observers == null)
            return null;
        return this.observers[name];
    }
    destroy() {
        if (this.observers != null) {
            for (var key in this.observers) {
                let observer = this.observers[key];
                if (typeof observer.destroy === "function") {
                    observer.destroy();
                }
            }
        }
        this.observers = {};
    }
    ;
    getStatus() {
        return "";
    }
    ;
    isFunctional() {
        return true;
    }
    ;
}
exports.default = Consul;
//# sourceMappingURL=consul.js.map