"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigInjectionToken = exports.redactConfig = exports.validateConfig = exports.createStrictConfig = exports.STREAMR_STORAGE_NODE_GERMANY = exports.DEFAULT_ENVIRONMENT = exports.NetworkNodeType = void 0;
require("reflect-metadata");
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
const utils_1 = require("@streamr/utils");
const utils_2 = require("./utils/utils");
const validateConfig_1 = __importDefault(require("./generated/validateConfig"));
const config_1 = require("@streamr/config");
var NetworkNodeType;
(function (NetworkNodeType) {
    NetworkNodeType["NODEJS"] = "nodejs";
    NetworkNodeType["BROWSER"] = "browser";
})(NetworkNodeType || (exports.NetworkNodeType = NetworkNodeType = {}));
exports.DEFAULT_ENVIRONMENT = 'polygon';
exports.STREAMR_STORAGE_NODE_GERMANY = '0x31546eEA76F2B2b3C5cC06B1c93601dc35c9D916';
const createStrictConfig = (input = {}) => {
    // TODO is it good to cloneDeep the input object as it may have object references (e.g. auth.ethereum)?
    let config = (0, cloneDeep_1.default)(input);
    const environment = config.environment ?? exports.DEFAULT_ENVIRONMENT;
    config = applyEnvironmentDefaults(environment, config);
    const strictConfig = (0, exports.validateConfig)(config);
    strictConfig.id ??= (0, utils_2.generateClientId)();
    return strictConfig;
};
exports.createStrictConfig = createStrictConfig;
const applyEnvironmentDefaults = (environmentId, data) => {
    const defaults = config_1.config[environmentId];
    const config = (0, utils_1.merge)(data, {
        network: {
            ...data.network,
            controlLayer: {
                entryPoints: defaults.entryPoints,
                ...data.network?.controlLayer,
            }
        },
        contracts: {
            streamRegistryChainAddress: defaults.contracts.StreamRegistry,
            streamStorageRegistryChainAddress: defaults.contracts.StreamStorageRegistry,
            storageNodeRegistryChainAddress: defaults.contracts.StorageNodeRegistry,
            streamRegistryChainRPCs: {
                name: defaults.name,
                chainId: defaults.id,
                rpcs: defaults.rpcEndpoints
            },
            theGraphUrl: defaults.theGraphUrl,
            ...data.contracts,
        }
    });
    if (environmentId === 'polygon') {
        config.contracts.ethereumNetwork = {
            highGasPriceStrategy: true,
            ...config.contracts.ethereumNetwork
        };
    }
    if (environmentId === 'dev2') {
        // TODO config the 30s default for "dev2 in" @streamr/config and remove this explicit timeout
        const toNumber = (value) => {
            return (value !== undefined) ? Number(value) : undefined;
        };
        config.contracts.streamRegistryChainRPCs.rpcs.forEach((rpc) => {
            rpc.timeout = toNumber(process.env.TEST_TIMEOUT) ?? 30 * 1000;
        });
    }
    return config;
};
const validateConfig = (data) => {
    if (!(0, validateConfig_1.default)(data)) {
        throw new Error(validateConfig_1.default.errors.map((e) => {
            let text = e.instancePath + ' ' + e.message;
            if (e.params.additionalProperty) {
                text += `: ${e.params.additionalProperty}`;
            }
            return text;
        }).join('\n'));
    }
    return data;
};
exports.validateConfig = validateConfig;
const redactConfig = (config) => {
    if (config.auth?.privateKey !== undefined) {
        config.auth.privateKey = '(redacted)';
    }
};
exports.redactConfig = redactConfig;
exports.ConfigInjectionToken = Symbol('Config');
//# sourceMappingURL=Config.js.map