"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMongoCollection = exports.createMongoDB = exports.createMongoConfig = void 0;
var DaoError_1 = require("../../error/DaoError");
var _a = require('mongodb'), MongoDb = _a.Db, MongoServer = _a.Server, ReplSetServers = _a.ReplSetServers;
/**
 * Create a mongodb configuration from the environment
 *
 * @param env - the environment variables
 *
 * @return the mongo configuration
 */
function createMongoConfig(env) {
    return {
        port: +(env.MONGODB_PORT || 0),
        ha: +(env.LIZA_MONGODB_HA || 0) == 1,
        replset: env.LIZA_MONGODB_REPLSET,
        host: env.MONGODB_HOST,
        host_a: env.LIZA_MONGODB_HOST_A,
        port_a: +(env.LIZA_MONGODB_PORT_A || 0),
        host_b: env.LIZA_MONGODB_HOST_B,
        port_b: +(env.LIZA_MONGODB_PORT_B || 0),
        collection: 'quotes',
    };
}
exports.createMongoConfig = createMongoConfig;
/**
 * Create the database connection
 *
 * @param conf - the configuration from the environment
 *
 * @return the mongodb connection
 */
function createMongoDB(conf) {
    if (conf.ha) {
        var mongodbPort = conf.port || 27017;
        var mongodbReplSet = conf.replset || 'rs0';
        var dbServers = new ReplSetServers([
            new MongoServer(conf.host_a, conf.port_a || mongodbPort),
            new MongoServer(conf.host_b, conf.port_b || mongodbPort),
        ], { rs_name: mongodbReplSet, auto_reconnect: true });
    }
    else {
        var dbServers = new MongoServer(conf.host || '127.0.0.1', conf.port || 27017, { auto_reconnect: true });
    }
    var db = new MongoDb('program', dbServers, {
        native_parser: false,
        safe: false,
        w: 'majority',
    });
    return db;
}
exports.createMongoDB = createMongoDB;
/**
 * Attempts to connect to the database and retrieve the collection
 *
 * connectError event will be emitted on failure.
 *
 * @param db   - the mongo database
 * @param conf - the mongo configuration
 *
 * @return the collection
 */
function getMongoCollection(db, conf) {
    return new Promise(function (resolve, reject) {
        // attempt to connect to the database
        db.open(function (e, db) {
            // if there was an error, don't bother with anything else
            if (e) {
                // in some circumstances, it may just be telling us that
                // we're already connected (even though the connection may
                // have been broken)
                if (e.errno !== undefined) {
                    reject(new Error('Error opening mongo connection: ' + e));
                    return;
                }
            }
            else if (db == null) {
                reject(new DaoError_1.DaoError('No database connection'));
                return;
            }
            // quotes collection
            db.collection(conf.collection, function (e, collection) {
                if (e) {
                    reject(new DaoError_1.DaoError('Error creating collection: ' + e));
                    return;
                }
                var createdCount = 0;
                var checkAllCreated = function () {
                    if (createdCount >= 3) {
                        resolve(collection);
                    }
                };
                var cb = function (e, _index) {
                    if (e) {
                        reject(new DaoError_1.DaoError('Error creating index: ' + e));
                        return;
                    }
                    createdCount++;
                    checkAllCreated();
                };
                // initialize indexes
                collection.createIndex([['published', 1]], false, cb);
                collection.createIndex([['deltaError', 1]], false, cb);
                collection.createIndex([
                    ['published', 1],
                    ['deltaError', 1],
                ], false, cb);
            });
        });
    });
}
exports.getMongoCollection = getMongoCollection;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTW9uZ29GYWN0b3J5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiTW9uZ29GYWN0b3J5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQTZCQSxpREFBOEM7QUFFeEMsSUFBQSxLQUlGLE9BQU8sQ0FBQyxTQUFTLENBQUMsRUFIaEIsT0FBTyxRQUFBLEVBQ0gsV0FBVyxZQUFBLEVBQ0gsY0FBYyxvQkFDVixDQUFDO0FBRXZCOzs7Ozs7R0FNRztBQUNILFNBQWdCLGlCQUFpQixDQUFDLEdBQXNCO0lBQ3RELE9BQXNCO1FBQ3BCLElBQUksRUFBRSxDQUFDLENBQUMsR0FBRyxDQUFDLFlBQVksSUFBSSxDQUFDLENBQUM7UUFDOUIsRUFBRSxFQUFFLENBQUMsQ0FBQyxHQUFHLENBQUMsZUFBZSxJQUFJLENBQUMsQ0FBQyxJQUFJLENBQUM7UUFDcEMsT0FBTyxFQUFFLEdBQUcsQ0FBQyxvQkFBb0I7UUFDakMsSUFBSSxFQUFFLEd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