"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoFactory = void 0;
var MongoServerDao_1 = require("./MongoServerDao");
/* eslint-disable @typescript-eslint/no-var-requires */
var _a = require('mongodb/lib/mongodb'), Db = _a.Db, Server = _a.Server, Connection = _a.Connection, ReplSetServers = _a.ReplSetServers;
/* eslint-enable @typescript-eslint/no-var-requires */
var MongoFactory = /** @class */ (function () {
    function MongoFactory(_env, _logger, _ts_ctor) {
        this._env = _env;
        this._logger = _logger;
        this._ts_ctor = _ts_ctor;
    }
    //TODO
    MongoFactory.prototype.createDao = function (db) {
        return new MongoServerDao_1.MongoServerDao(db !== null && db !== void 0 ? db : this.createDatabase(), this._env.NODE_ENV, this._ts_ctor);
    };
    MongoFactory.prototype.createDatabase = function () {
        var config = {
            native_parser: false,
            safe: false,
            logger: this._logger,
        };
        return new Db('program', this.createServers(), config);
    };
    MongoFactory.prototype.createServers = function () {
        if (!this.isHighAvailability) {
            return new Server(process.env.MONGODB_HOST || '127.0.0.1', +(process.env.MONGODB_PORT || Connection.DEFAULT_PORT), { auto_reconnect: true });
        }
        var mongodbPort = process.env.MONGODB_PORT || Connection.DEFAULT_PORT;
        var mongodbReplSet = process.env.LIZA_MONGODB_REPLSET || 'rs0';
        return new ReplSetServers([
            new Server(process.env.LIZA_MONGODB_HOST_A, +(process.env.LIZA_MONGODB_PORT_A || mongodbPort)),
            new Server(process.env.LIZA_MONGODB_HOST_B, +(process.env.LIZA_MONGODB_PORT_B || mongodbPort)),
        ], { rs_name: mongodbReplSet, auto_reconnect: true });
    };
    Object.defineProperty(MongoFactory.prototype, "isHighAvailability", {
        get: function () {
            return this._env.LIZA_MONGODB_HA === '1';
        },
        enumerable: false,
        configurable: true
    });
    return MongoFactory;
}());
exports.MongoFactory = MongoFactory;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTW9uZ29GYWN0b3J5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiTW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