"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePickDefaultAccount = void 0;
const react_1 = require("react");
const hooks_1 = require("../../../currencies/hooks");
const currencies_1 = require("../../../currencies");
const utils_1 = require("../utils");
// Pick a default source account if none are selected.
const usePickDefaultAccount = (accounts, fromAccount, setFromAccount) => {
    const list = (0, currencies_1.listCryptoCurrencies)();
    const allCurrencies = (0, hooks_1.useCurrenciesByMarketcap)(list);
    (0, react_1.useEffect)(() => {
        if (!fromAccount && allCurrencies.length > 0) {
            const defaultAccount = allCurrencies
                .map(({ id }) => (0, utils_1.getAvailableAccountsById)(id, accounts).filter(account => account.balance.gt(0)))
                .flat(1)
                .find(Boolean);
            if (defaultAccount && defaultAccount?.id !== fromAccount?.["id"])
                setFromAccount(defaultAccount);
        }
    }, [accounts, allCurrencies, fromAccount, setFromAccount]);
};
exports.usePickDefaultAccount = usePickDefaultAccount;
//# sourceMappingURL=usePickDefaultAccount.js.map