"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFromState = exports.getAbandonSeedAddress = void 0;
const cryptoassets_1 = require("@ledgerhq/cryptoassets");
Object.defineProperty(exports, "getAbandonSeedAddress", { enumerable: true, get: function () { return cryptoassets_1.getAbandonSeedAddress; } });
const react_1 = require("react");
const _1 = require(".");
const account_1 = require("../../../account");
const debounce_1 = require("../utils/debounce");
const useFetchCurrencyFrom_1 = require("./v5/useFetchCurrencyFrom");
const useFromState = ({ accounts, defaultCurrency, defaultAccount, defaultParentAccount, bridgeTransaction, }) => {
    const { data: fromCurrencies } = (0, useFetchCurrencyFrom_1.useFetchCurrencyFrom)();
    const [fromState, setFromState] = (0, react_1.useState)({
        ..._1.selectorStateDefaultValues,
        currency: defaultCurrency ?? _1.selectorStateDefaultValues.currency,
        account: defaultAccount ?? _1.selectorStateDefaultValues.account,
        parentAccount: defaultParentAccount ?? _1.selectorStateDefaultValues.parentAccount,
    });
    /* UPDATE from account */
    const setFromAccount = (0, react_1.useCallback)(account => {
        const parentAccount = account?.type !== "Account" ? accounts?.find(a => a.id === account?.parentId) : undefined;
        const currency = (0, account_1.getAccountCurrency)(account);
        bridgeTransaction.setAccount(account, parentAccount);
        setFromState({
            ..._1.selectorStateDefaultValues,
            currency,
            account,
            parentAccount,
        });
        /* @DEV: That populates fake seed. This is required to use Transaction object */
        const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
        const mainCurrency = (0, account_1.getAccountCurrency)(mainAccount);
        const recipient = (0, cryptoassets_1.getAbandonSeedAddress)(mainCurrency.id);
        bridgeTransaction.updateTransaction(transaction => {
            return {
                ...transaction,
                recipient,
            };
        });
    }, 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [accounts, bridgeTransaction.updateTransaction]);
    const debouncedSetFromAmount = (0, react_1.useMemo)(() => (0, debounce_1.debounce)((amount) => {
        bridgeTransaction.updateTransaction(transaction => ({
            ...transaction,
            amount,
        }));
        setFromState(previousState => ({ ...previousState, amount: amount }));
    }, 400), 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [bridgeTransaction.updateTransaction]);
    const setFromAmount = (0, react_1.useCallback)(amount => debouncedSetFromAmount(amount), [debouncedSetFromAmount]);
    return {
        fromCurrencies: fromCurrencies ?? [],
        fromState,
        setFromAccount,
        setFromAmount,
    };
};
exports.useFromState = useFromState;
//# sourceMappingURL=useFromState.js.map