"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Elasticsearch = require("elasticsearch");
const settings_1 = require("./settings");
class Elastic {
    constructor(tag = "tag") {
        this.elastic = null;
        this.waitForStatus = false;
        this.config = settings_1.default.Instance.getElastic(tag);
        let statusInterval = this.config.statusCheckInterval;
        if (statusInterval == null || typeof statusInterval != "number" || statusInterval < 1) {
            statusInterval = 5000;
        }
        this.status = "";
        this.tag = tag;
        if (this.config.autoConnect) {
            this.connect();
        }
        this.statusCheck();
        this.interval = setInterval(this.statusCheck.bind(this), statusInterval);
    }
    statusCheck() {
        if (this.waitForStatus)
            return;
        if (this.elastic == null) {
            this.status = "disconnected";
            return;
        }
        this.waitForStatus = true;
        let self = this;
        this.elastic.ping({
            requestTimeout: 30000,
        }, function (error) {
            self.waitForStatus = false;
            self.setStatus(error);
        });
    }
    destroy() {
        clearInterval(this.interval);
        if (this.elastic == null)
            return;
        this.elastic.close();
        this.elastic = null;
    }
    connect() {
        this.elastic = new Elasticsearch.Client({
            host: this.config.host,
            log: this.config.log,
        });
    }
    setStatus(err) {
        if (err) {
            this.status = "error";
        }
        else {
            this.status = "connected";
        }
    }
    close() {
        if (this.elastic == null)
            return;
        this.elastic.close();
    }
    /**
     * return the status of the inner redis connection
     */
    getStatus() {
        return this.status;
    }
    isFunctional() {
        return this.getStatus() == "connected";
    }
    tryToConnect(response) {
        if (this.elastic == null) {
            if (this.config.autoConnect) {
                this.connect();
                return true;
            }
            else {
                response("Do not have a connection", null);
                return false;
            }
        }
        return true;
    }
    index(object, response) {
        if (!this.tryToConnect(response))
            return;
        if (this.elastic != null) {
            this.elastic.index(object, response);
        }
    }
    delete(object, response) {
        if (!this.tryToConnect(response))
            return;
        if (this.elastic != null) {
            this.elastic.delete(object, response);
        }
    }
    get(object, response) {
        if (!this.tryToConnect(response))
            return;
        if (this.elastic != null) {
            this.elastic.get(object, response);
        }
    }
    search(object, response) {
        if (!this.tryToConnect(response))
            return;
        if (this.elastic != null) {
            this.elastic.search(object, response);
        }
    }
}
exports.default = Elastic;
//# sourceMappingURL=elastic.js.map