"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useListPlatformCurrencies = exports.usePlatformCurrencies = exports.useListPlatformAccounts = exports.usePlatformAccounts = exports.usePlatformUrl = void 0;
const react_1 = require("react");
const converters_1 = require("./converters");
const filters_1 = require("./filters");
const helpers_1 = require("./helpers");
const account_1 = require("../account");
const currencies_1 = require("../currencies");
/**
 * TODO: we might want to use "searchParams.append" instead of "searchParams.set"
 * to handle duplicated query params (example: "?foo=bar&foo=baz")
 *
 * We can also use the stringify method of qs (https://github.com/ljharb/qs#stringifying)
 */
function usePlatformUrl(manifest, inputs) {
    return (0, react_1.useMemo)(() => {
        const url = new URL(manifest.url.toString());
        if (inputs) {
            for (const key in inputs) {
                const value = inputs[key];
                if (Object.prototype.hasOwnProperty.call(inputs, key) && value !== undefined) {
                    url.searchParams.set(key, value);
                }
            }
        }
        if (manifest.params) {
            url.searchParams.set("params", JSON.stringify(manifest.params));
        }
        return url;
    }, [manifest.url, manifest.params, inputs]);
}
exports.usePlatformUrl = usePlatformUrl;
function usePlatformAccounts(walletState, accounts) {
    return (0, react_1.useMemo)(() => {
        return accounts.map(account => {
            const parentAccount = (0, account_1.getParentAccount)(account, accounts);
            return (0, converters_1.accountToPlatformAccount)(walletState, account, parentAccount);
        });
    }, [walletState, accounts]);
}
exports.usePlatformAccounts = usePlatformAccounts;
function useListPlatformAccounts(walletState, accounts) {
    const platformAccounts = usePlatformAccounts(walletState, accounts);
    return (0, react_1.useCallback)((filters = {}) => {
        return (0, filters_1.filterPlatformAccounts)(platformAccounts, filters);
    }, [platformAccounts]);
}
exports.useListPlatformAccounts = useListPlatformAccounts;
function usePlatformCurrencies() {
    return (0, react_1.useMemo)(() => {
        return (0, currencies_1.listCurrencies)(true).reduce((filtered, currency) => {
            if ((0, helpers_1.isPlatformSupportedCurrency)(currency)) {
                filtered.push((0, converters_1.currencyToPlatformCurrency)(currency));
            }
            return filtered;
        }, []);
    }, []);
}
exports.usePlatformCurrencies = usePlatformCurrencies;
function useListPlatformCurrencies() {
    const currencies = usePlatformCurrencies();
    return (0, react_1.useCallback)((filters) => {
        return (0, filters_1.filterPlatformCurrencies)(currencies, filters || {});
    }, [currencies]);
}
exports.useListPlatformCurrencies = useListPlatformCurrencies;
//# sourceMappingURL=react.js.map