"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const customCassandraPool_1 = require("./customCassandraPool");
const settings_1 = require("./settings");
class Cassandra {
    constructor(tag = "global") {
        this.cassandra = null;
        this.status = "";
        this.config = settings_1.default.Instance.getCassandra(tag);
        this.tag = tag;
        if (this.config.autoConnect) {
            this.connect();
        }
    }
    destroy() {
        if (this.cassandra == null)
            return;
        this.cassandra.shutdown();
        this.cassandra = null;
    }
    connect(onSuccess = null, onError = null) {
        let self = this;
        let cassandra = new customCassandraPool_1.default(this.config, function (err) {
            if (typeof onError == "function") {
                onError(err);
            }
        }, function () {
            if (typeof onSuccess == "function") {
                onSuccess();
            }
        }, function (err) {
            self.setStatus(err);
        });
        this.cassandra = cassandra.client;
    }
    setStatus(err) {
        if (err != null) {
            // self.handleError(err);
            this.status = "Error";
        }
        else {
            // self.logger.log('Cassandra connect successful', base.LOG_LEVEL.INFO);
            if (this.status == "") {
                this.status = "Connected";
            }
            else {
                this.status = "Reconnected";
            }
        }
    }
    tryToConnect(response) {
        if (this.cassandra == null) {
            if (this.config.autoConnect) {
                this.connect();
                return true;
            }
            else {
                if (response)
                    response(new Error("Do not have a connection"), null);
                return false;
            }
        }
        return true;
    }
    close() {
        if (this.cassandra == null)
            return;
        this.cassandra.shutdown();
    }
    execute(query, params, options, callback) {
        if (!this.tryToConnect(callback))
            return;
        if (this.cassandra != null) {
            this.cassandra.execute(query, params, options, callback);
        }
    }
    getControlConnection() {
        if (this.cassandra == null)
            return null;
        return this.cassandra.controlConnection;
    }
    /**
     * return the status of the inner redis connection
     */
    getStatus() {
        return this.status;
    }
    isFunctional() {
        return this.getStatus() == "Connected" || this.getStatus() == "Reconnected";
    }
    getCurrentKeyspace() {
        return this.config.keyspace;
    }
}
exports.default = Cassandra;
//# sourceMappingURL=cassandra.js.map