import IORedis from "ioredis";
import IORedisMock from "ioredis-mock";
import fs from "fs";
function getRandomPort() {
    return 3000 + Math.floor(Math.random() * 1000);
}
export function createRedisFactory(redisUrl, options = {}) {
    if (!redisUrl) {
        let redis = undefined;
        return () => {
            if (!redis) {
                redis = new IORedisMock({
                    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.existsSync(options.tlsCa.path)) {
                options.tls = {
                    ...options.tls,
                    ca: fs.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(redisUrl, options);
            return redis;
        }
        return redis.duplicate();
    };
}
//# sourceMappingURL=create-redis.js.map