"use strict";
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSignerOrProvider = exports.useCallPoll = exports.useCallOnce = exports.useFallbackPoll = exports.useFallbackOnce = exports.useERC20 = exports.useContract = exports.DEFAULT_POLLING_INTERVAL = void 0;
var react_1 = require("react");
var web3_1 = require("./web3");
var utils_1 = require("../utils");
var async_1 = require("./async");
var abis_1 = require("../abis");
var useInterval_1 = require("./useInterval");
var SECONDS_IN_DAY = 86400000;
exports.DEFAULT_POLLING_INTERVAL = 5000;
function doAllArgsExist(args) {
    var e_1, _a;
    var allArgsExist = true;
    try {
        for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
            var arg = args_1_1.value;
            // Check to make sure all dependencies of ifTrue are true.
            if (arg == null || arg === '')
                allArgsExist = false;
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return allArgsExist;
}
function useContract(chain, nameOrAbi, address) {
    var signerOrProvider = (0, exports.useSignerOrProvider)(chain);
    var abi = nameOrAbi ? abis_1.contractAbis[nameOrAbi] : nameOrAbi;
    var contract = (0, react_1.useMemo)(function () {
        if (!signerOrProvider || !address || !abi)
            return;
        return (0, utils_1.getContract)(address, abi, signerOrProvider);
    }, [address, abi, signerOrProvider]);
    return contract;
}
exports.useContract = useContract;
var useERC20 = function (chain, address) {
    var signerOrProvider = (0, exports.useSignerOrProvider)(chain);
    return (0, react_1.useMemo)(function () {
        if (!signerOrProvider || !address)
            return;
        return (0, utils_1.getErc20)(address, signerOrProvider);
    }, [address, signerOrProvider]);
};
exports.useERC20 = useERC20;
function useFallbackOnce(condition, value, fallbackMethod) {
    var args = [];
    for (var _i = 3; _i < arguments.length; _i++) {
        args[_i - 3] = arguments[_i];
    }
    var fn = (0, react_1.useCallback)(
    // If condition is true, just return the value. Otherwise, call the method.
    !condition ? function () { return value; } : fallbackMethod !== null && fallbackMethod !== void 0 ? fallbackMethod : (function () { return undefined; }), [fallbackMethod, condition, value]);
    return useCallOnce.apply(void 0, __spreadArray([fn], __read(args), false));
}
exports.useFallbackOnce = useFallbackOnce;
function useFallbackPoll(seconds, condition, value, fallbackMethod) {
    var args = [];
    for (var _i = 4; _i < arguments.length; _i++) {
        args[_i - 4] = arguments[_i];
    }
    var fn = (0, react_1.useCallback)(
    // If condition is true, just return the value. Otherwise, call the method.
    !condition ? function () { return value; } : fallbackMethod !== null && fallbackMethod !== void 0 ? fallbackMethod : (function () { return undefined; }), [fallbackMethod, condition, value]);
    return useCallPoll.apply(void 0, __spreadArray([seconds, fn], __read(args), false));
}
exports.useFallbackPoll = useFallbackPoll;
function useCallOnce(method) {
    var args = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
    }
    var fn = (0, react_1.useCallback)(method !== null && method !== void 0 ? method : (function () { return undefined; }), [method]);
    var allArgsExist = doAllArgsExist(args);
    return (0, async_1.useAsync)(fn, args, {
        condition: !!method && allArgsExist,
        deps: [],
    });
}
exports.useCallOnce = useCallOnce;
function useCallPoll(seconds, method) {
    var args = [];
    for (var _i = 2; _i < arguments.length; _i++) {
        args[_i - 2] = arguments[_i];
    }
    var milliseconds = (0, react_1.useMemo)(function () { return (seconds <= 0 ? SECONDS_IN_DAY : Math.round(seconds * 1000)); }, [seconds]);
    var count = (0, useInterval_1.useInterval)(milliseconds).count;
    var fn = (0, react_1.useCallback)(method !== null && method !== void 0 ? method : (function () { return undefined; }), [method]);
    var allArgsExist = doAllArgsExist(args);
    return (0, async_1.useAsync)(fn, args, {
        condition: !!method && allArgsExist,
        deps: [],
        interval: count,
    });
}
exports.useCallPoll = useCallPoll;
var useSignerOrProvider = function (desiredChain) {
    var _a = __read((0, react_1.useState)(), 2), signer = _a[0], setSigner = _a[1];
    var _b = __read((0, react_1.useState)(), 2), provider = _b[0], setProvider = _b[1];
    var _c = (0, web3_1.useWeb3React)(), web3ReactProvider = _c.provider, chainId = _c.chainId, isActivating = _c.isActivating;
    var isCorrectNetwork = (0, react_1.useMemo)(function () { return chainId != null && utils_1.chainIdToNetwork[chainId] === desiredChain; }, [desiredChain, chainId]);
    var fallbackProvider = (0, async_1.useAsync)(utils_1.getProvider, [desiredChain], {
        condition: !!desiredChain,
        deps: [desiredChain],
    }).res;
    (0, react_1.useEffect)(function () {
        if (isActivating)
            return;
        // Get signer.
        if (!desiredChain || !isCorrectNetwork || !(web3ReactProvider === null || web3ReactProvider === void 0 ? void 0 : web3ReactProvider.getSigner)) {
            // Includes handling edgecase where signer is disconnected.
            setSigner(undefined);
            return;
        }
        setSigner(web3ReactProvider === null || web3ReactProvider === void 0 ? void 0 : web3ReactProvider.getSigner());
    }, [desiredChain, isCorrectNetwork, web3ReactProvider, isActivating]);
    (0, react_1.useEffect)(function () {
        if (isActivating)
            return;
        // Get external provider.
        if (!desiredChain) {
            setProvider(undefined);
            return;
        }
        else if (web3ReactProvider && isCorrectNetwork) {
            setProvider(web3ReactProvider);
        }
        else {
            if (fallbackProvider)
                setProvider(fallbackProvider);
        }
    }, [
        desiredChain,
        isCorrectNetwork,
        web3ReactProvider,
        isActivating,
        fallbackProvider,
    ]);
    return signer || provider;
};
exports.useSignerOrProvider = useSignerOrProvider;
//# sourceMappingURL=common.js.map