"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const R = __importStar(require("ramda"));
const constructEnvironment_1 = require("../../utils/environment/constructEnvironment");
const testLogger_1 = require("../utils/testLogger");
const Environment_1 = require("../../utils/environment/Environment");
const withDeployment_1 = require("../../utils/environment/withDeployment");
const withKeystoreSigner_1 = require("../../utils/environment/withKeystoreSigner");
const withPrivateKeySigner_1 = require("../../utils/environment/withPrivateKeySigner");
const getSystemTestEnvironment = (track = Environment_1.Tracks.TESTING) => __awaiter(this, void 0, void 0, function* () {
    const baseEnvironment = constructEnvironment_1.constructEnvironment({
        endpoint: process.env.JSON_RPC_ENDPOINT || 'http://localhost:8545',
        logger: testLogger_1.testLogger,
        track,
    });
    const environmentWithDeployment = yield withDeployment_1.withDeployment(baseEnvironment);
    const selectSigner = R.cond([
        [
            R.prop('KEYSTORE_FILE'),
            (env) => __awaiter(this, void 0, void 0, function* () {
                return yield withKeystoreSigner_1.withKeystoreSigner(environmentWithDeployment, {
                    keystore: require(path.join(process.cwd(), R.prop('KEYSTORE_FILE', env))),
                    password: R.prop('KEYSTORE_PASSWORD', env),
                });
            }),
        ],
        [
            R.prop('PRIVATE_KEY'),
            (env) => __awaiter(this, void 0, void 0, function* () {
                return yield withPrivateKeySigner_1.withPrivateKeySigner(environmentWithDeployment, R.prop('PRIVATE_KEY', env));
            }),
        ],
        [
            R.T,
            () => {
                throw new Error('Neither PRIVATE_KEY nor KEYSTORE_FILE found in env');
            },
        ],
    ]);
    const environment = yield selectSigner(process.env);
    return environment;
});
exports.getSystemTestEnvironment = getSystemTestEnvironment;
