"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRedisFactory = void 0;
const ioredis_1 = __importDefault(require("ioredis"));
const ioredis_mock_1 = __importDefault(require("ioredis-mock"));
const fs_1 = __importDefault(require("fs"));
function getRandomPort() {
    return 3000 + Math.floor(Math.random() * 1000);
}
function createRedisFactory(redisUrl, options = {}) {
    if (!redisUrl) {
        let redis = undefined;
        return () => {
            if (!redis) {
                redis = new ioredis_mock_1.default({
                    port: getRandomPort(),
                });
                return redis;
            }
            return redis.duplicate();
        };
    }
    let redis = undefined;
    return () => {
        var _a, _b, _c;
        if (!redis) {
            if (((_a = options.tlsCa) === null || _a === void 0 ? void 0 : _a.path) && fs_1.default.existsSync(options.tlsCa.path)) {
                options.tls = {
                    ...options.tls,
                    ca: fs_1.default.readFileSync(options.tlsCa.path, "utf8"),
                };
            }
            if ((_b = options.tlsCa) === null || _b === void 0 ? void 0 : _b.base64) {
                options.tls = {
                    ...options.tls,
                    ca: Buffer.from((_c = options.tlsCa) === null || _c === void 0 ? void 0 : _c.base64, "base64").toString("ascii"),
                };
            }
            redis = new ioredis_1.default(redisUrl, options);
            return redis;
        }
        return redis.duplicate();
    };
}
exports.createRedisFactory = createRedisFactory;
//# sourceMappingURL=create-redis.js.map