"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_ID = exports.ENVIRONMENT_IDS = 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");
const ConfigTest_1 = require("./ConfigTest");
var NetworkNodeType;
(function (NetworkNodeType) {
    NetworkNodeType["NODEJS"] = "nodejs";
    NetworkNodeType["BROWSER"] = "browser";
})(NetworkNodeType || (exports.NetworkNodeType = NetworkNodeType = {}));
exports.ENVIRONMENT_IDS = ['polygon', 'polygonAmoy', 'dev2'];
exports.DEFAULT_ENVIRONMENT_ID = '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_ID;
    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];
    let config = (0, utils_1.merge)({
        network: {
            controlLayer: {
                entryPoints: defaults.entryPoints
            }
        },
        contracts: {
            ethereumNetwork: {
                chainId: defaults.id
            },
            streamRegistryChainAddress: defaults.contracts.StreamRegistry,
            streamStorageRegistryChainAddress: defaults.contracts.StreamStorageRegistry,
            storageNodeRegistryChainAddress: defaults.contracts.StorageNodeRegistry,
            rpcs: defaults.rpcEndpoints,
            theGraphUrl: defaults.theGraphUrl
        }
    }, data);
    if (environmentId === 'polygon') {
        config.contracts.ethereumNetwork = {
            highGasPriceStrategy: true,
            ...config.contracts.ethereumNetwork
        };
    }
    else if (environmentId === 'dev2') {
        config = (0, utils_1.merge)(ConfigTest_1.CONFIG_TEST, config);
    }
    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) {
                // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
                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