"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/* tslint:disable:no-var-requires */
/* tslint:disable-next-line:no-reference */
/// <reference path="../custom_typings/web3.d.ts" />
tslib_1.__exportStar(require("./accountService"), exports);
tslib_1.__exportStar(require("./avatarService"), exports);
tslib_1.__exportStar(require("./commonTypes"), exports);
tslib_1.__exportStar(require("./configService"), exports);
var wrapperService_1 = require("./wrapperService");
exports.ContractWrappers = wrapperService_1.ContractWrappers;
exports.ContractWrapperFactories = wrapperService_1.ContractWrapperFactories;
exports.ContractWrappersByType = wrapperService_1.ContractWrappersByType;
exports.ContractWrappersByAddress = wrapperService_1.ContractWrappersByAddress;
tslib_1.__exportStar(require("./wrapperService"), exports);
tslib_1.__exportStar(require("./wrappers/absoluteVote"), exports);
tslib_1.__exportStar(require("./wrappers/contributionReward"), exports);
tslib_1.__exportStar(require("./wrappers/daoCreator"), exports);
tslib_1.__exportStar(require("./wrappers/daoToken"), exports);
tslib_1.__exportStar(require("./wrappers/locking4Reputation"), exports);
tslib_1.__exportStar(require("./wrappers/lockingEth4Reputation"), exports);
tslib_1.__exportStar(require("./wrappers/lockingToken4Reputation"), exports);
tslib_1.__exportStar(require("./wrappers/fixedReputationAllocation"), exports);
tslib_1.__exportStar(require("./wrappers/externalLocking4Reputation"), exports);
tslib_1.__exportStar(require("./wrappers/auction4Reputation"), exports);
tslib_1.__exportStar(require("./wrappers/genesisProtocol"), exports);
tslib_1.__exportStar(require("./wrappers/globalConstraintRegistrar"), exports);
tslib_1.__exportStar(require("./wrappers/intVoteInterface"), exports);
tslib_1.__exportStar(require("./wrappers/redeemer"), exports);
tslib_1.__exportStar(require("./wrappers/reputation"), exports);
tslib_1.__exportStar(require("./wrappers/schemeRegistrar"), exports);
tslib_1.__exportStar(require("./wrappers/erc20"), exports);
tslib_1.__exportStar(require("./wrappers/tokenCapGC"), exports);
tslib_1.__exportStar(require("./wrappers/upgradeScheme"), exports);
tslib_1.__exportStar(require("./wrappers/vestingScheme"), exports);
tslib_1.__exportStar(require("./wrappers/voteInOrganizationScheme"), exports);
tslib_1.__exportStar(require("./iContractWrapperBase"), exports);
tslib_1.__exportStar(require("./dao"), exports);
tslib_1.__exportStar(require("./contractWrapperBase"), exports);
tslib_1.__exportStar(require("./schemeWrapperBase"), exports);
tslib_1.__exportStar(require("./uSchemeWrapperBase"), exports);
tslib_1.__exportStar(require("./contractWrapperFactory"), exports);
tslib_1.__exportStar(require("./pubSubEventService"), exports);
tslib_1.__exportStar(require("./web3EventService"), exports);
tslib_1.__exportStar(require("./proposalService"), exports);
tslib_1.__exportStar(require("./proposalGeneratorBase"), exports);
tslib_1.__exportStar(require("./loggingService"), exports);
tslib_1.__exportStar(require("./transactionService"), exports);
tslib_1.__exportStar(require("./utils"), exports);
const accountService_1 = require("./accountService");
const configService_1 = require("./configService");
const contractWrapperFactory_1 = require("./contractWrapperFactory");
const loggingService_1 = require("./loggingService");
const pubSubEventService_1 = require("./pubSubEventService");
const utils_1 = require("./utils");
const wrapperService_2 = require("./wrapperService");
const deployedContractAddresses = require("../migration.json");
/**
 * You must call `InitializeArcJs` before doing anything else with Arc.js.
 * Call it again whenever the current chain changes.
 * @returns Promise of the `Web3` object for the current chain.
 */
function InitializeArcJs(options = {}) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        loggingService_1.LoggingService.info("Initializing Arc.js");
        try {
            if (options.useWeb3) {
                // Utils.getWeb3 will pick this up
                global.web3 = options.useWeb3;
            }
            else if (typeof (options.useMetamaskEthereumWeb3Provider) === "undefined") {
                options.useMetamaskEthereumWeb3Provider = true;
            }
            configService_1.ConfigService.set("useMetamaskEthereumWeb3Provider", options.useMetamaskEthereumWeb3Provider);
            /**
             * simulate dependency injection, avoid circular dependencies
             */
            contractWrapperFactory_1.ContractWrapperFactory.setConfigService(configService_1.ConfigService);
            contractWrapperFactory_1.ContractWrapperFactory.clearContractCache();
            /**
             * Initialize LoggingService here to avoid circular dependency involving ConfigService and PubSubService
             */
            loggingService_1.LoggingService.logLevel = parseInt(configService_1.ConfigService.get("logLevel"), 10);
            /**
             * Watch for changes in logLevel via ConfigService.
             */
            pubSubEventService_1.PubSubEventService.subscribe(`ConfigService.settingChanged.logLevel`, (topics, value) => {
                loggingService_1.LoggingService.logLevel = parseInt(value, 10);
            });
            if (options.useNetworkDefaultsFor) {
                const networkDefaults = configService_1.ConfigService.get("networkDefaults")[options.useNetworkDefaultsFor];
                if (!networkDefaults) {
                    throw new Error(`truffle network defaults not found: ${options.useNetworkDefaultsFor}`);
                }
                configService_1.ConfigService.set("providerPort", networkDefaults.port);
                configService_1.ConfigService.set("providerUrl", `${networkDefaults.host}`);
            }
            /**
             * throws an exception if web3 cannot be initialized (no connection).
             */
            const web3 = yield utils_1.Utils.getWeb3(true);
            /**
             * Addresses are coming either from the DAOstack migration package or as passed-in by the
             * caller.  The caller may selectively override each contract address.
             */
            let networkName = (yield utils_1.Utils.getNetworkName()).toLowerCase();
            switch (networkName) {
                case "ganache":
                    networkName = "private";
                    break;
                case "live":
                    networkName = "main";
                    break;
            }
            if (options.watchForAccountChanges) {
                yield accountService_1.AccountService.initiateAccountWatch();
            }
            if (options.watchForNetworkChanges) {
                yield accountService_1.AccountService.initiateNetworkWatch();
            }
            const optionsDeployedContractAddresses = options.deployedContractAddresses || {};
            if (!deployedContractAddresses[networkName] && !optionsDeployedContractAddresses[networkName]) {
                throw new Error(`contracts have not been deployed to ${networkName}`);
            }
            const addresses = Object.assign({}, deployedContractAddresses[networkName] ? deployedContractAddresses[networkName].base : {}, optionsDeployedContractAddresses[networkName] ? optionsDeployedContractAddresses[networkName].base : {});
            utils_1.Utils.setDeployedAddresses(addresses);
            yield wrapperService_2.WrapperService.initialize(options);
            return web3;
        }
        catch (ex) {
            /* tslint:disable-next-line:no-bitwise */
            loggingService_1.LoggingService.message(`InitializeArcJs failed: ${ex}`, loggingService_1.LogLevel.info | loggingService_1.LogLevel.error);
            throw new Error(`InitializeArcJs failed: ${ex}`);
        }
    });
}
exports.InitializeArcJs = InitializeArcJs;
//# sourceMappingURL=index.js.map