"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvUtil = void 0;
var fs_1 = require("fs");
var EnvNames;
(function (EnvNames) {
    EnvNames["APPLICATION_ID"] = "APPLICATION_ID";
    EnvNames["PROJECT_ID"] = "PROJECT_ID";
    EnvNames["MASTER_KEY"] = "MASTER_KEY";
    EnvNames["PRODUCTION"] = "PRODUCTION";
    EnvNames["S3_BUCKET"] = "S3_BUCKET";
    EnvNames["S3_ACCESS_KEY"] = "S3_ACCESS_KEY";
    EnvNames["S3_ENDPOINT"] = "S3_ENDPOINT";
    EnvNames["NPM_TAR"] = "NPM_TAR";
    EnvNames["GIT_CLONE_URL"] = "GIT_CLONE_URL";
    EnvNames["MOUNT_PATH"] = "MOUNT_PATH";
    EnvNames["S3_SECRET_KEY"] = "S3_SECRET_KEY";
    EnvNames["RSA_PUBLIC_KEY"] = "RSA_PUBLIC_KEY";
    EnvNames["RSA_KEY"] = "RSA_KEY";
    EnvNames["MONGO_URL"] = "MONGO_URL";
    EnvNames["LOGS"] = "LOGS";
    EnvNames["TAARIFA_TOKEN"] = "TAARIFA_TOKEN";
    EnvNames["WEB_3_TOKEN"] = "WEB_3_TOKEN";
    EnvNames["USE_LOCAL_IPFS"] = "USE_LOCAL_IPFS";
    EnvNames["S3_REGION"] = "S3_REGION";
    EnvNames["PORT"] = "PORT";
})(EnvNames || (EnvNames = {}));
var EnvUtil = (function () {
    function EnvUtil() {
    }
    EnvUtil.prototype.getEnv = function (name) {
        if (name && name.toString() !== 'undefined' && name.toString() !== 'null') {
            var isFile = void 0;
            try {
                var fileStats = fs_1.statSync(name);
                isFile = fileStats.isFile();
            }
            catch (_) {
                isFile = false;
            }
            if ((name === null || name === void 0 ? void 0 : name.startsWith('/')) === true && isFile === true) {
                try {
                    var value = fs_1.readFileSync(name, { encoding: 'utf8' });
                    return EnvUtil.tryStringToObject(value);
                }
                catch (_) {
                    return EnvUtil.tryStringToObject(name);
                }
            }
            else {
                return EnvUtil.tryStringToObject(name);
            }
        }
        else {
            return undefined;
        }
    };
    EnvUtil.prototype.loadEnv = function () {
        var _a, _b;
        var isS3Configured = true;
        var s3Bucket = this.getEnv(process.env[EnvNames.S3_BUCKET.toString()]);
        var s3AccessKey = this.getEnv(process.env[EnvNames.S3_ACCESS_KEY.toString()]);
        var s3SecretKey = this.getEnv(process.env[EnvNames.S3_SECRET_KEY.toString()]);
        var s3Endpoint = this.getEnv(process.env[EnvNames.S3_ENDPOINT.toString()]);
        var s3Region = this.getEnv(process.env[EnvNames.S3_REGION.toString()]);
        var checker = [];
        checker.push(s3Bucket, s3AccessKey, s3SecretKey, s3Endpoint, s3Region);
        checker = checker.filter(function (x) {
            if (!x) {
                return false;
            }
            else if (x.toString() === 'null') {
                return false;
            }
            else if (x.toString() === 'undefined') {
                return false;
            }
            else
                return x.toString() !== '';
        });
        if (checker.length === 0) {
            isS3Configured = false;
        }
        else {
            checker.forEach(function (value) {
                if (!value) {
                    isS3Configured = false;
                }
                else if (value.toString() === 'null') {
                    isS3Configured = false;
                }
                else if (value.toString() === 'undefined') {
                    isS3Configured = false;
                }
                else if (value.toString() === '') {
                    isS3Configured = false;
                }
            });
        }
        var options = {};
        options.useLocalIpfs = ((_b = (_a = this.getEnv(process.env[EnvNames.USE_LOCAL_IPFS])) === null || _a === void 0 ? void 0 : _a.toString()) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'true';
        options.applicationId = this.getEnv(process.env[EnvNames.APPLICATION_ID]);
        options.projectId = this.getEnv(process.env[EnvNames.PROJECT_ID]);
        options.port = this.getEnv(process.env[EnvNames.PORT]);
        options.masterKey = this.getEnv(process.env[EnvNames.MASTER_KEY]);
        options.web3Token = this.getEnv(process.env[EnvNames.WEB_3_TOKEN]);
        options.logs = this.getEnv(process.env[EnvNames.LOGS]) === '1';
        options.mongoDbUri = this.getEnv(process.env[EnvNames.MONGO_URL]);
        options.taarifaToken = this.getEnv(process.env[EnvNames.TAARIFA_TOKEN]);
        options.rsaPublicKeyInJson = this.getEnv(process.env[EnvNames.RSA_PUBLIC_KEY]);
        options.rsaKeyPairInJson = this.getEnv(process.env[EnvNames.RSA_KEY]);
        options.adapters = {
            s3Storage: isS3Configured ? {
                bucket: s3Bucket,
                endPoint: s3Endpoint,
                secretKey: s3SecretKey,
                accessKey: s3AccessKey,
                region: s3Region
            } : undefined,
        };
        return options;
    };
    EnvUtil.tryStringToObject = function (value) {
        value = value.trim();
        if (value.startsWith('{') && value.endsWith('}')) {
            try {
                return JSON.parse(value);
            }
            catch (e) {
                return value;
            }
        }
        return value;
    };
    return EnvUtil;
}());
exports.EnvUtil = EnvUtil;
