import { u64FromCurrency } from "@equilab/api/eq-next/util";
import { useCallback, useMemo } from "react";
import { useAsyncMemo } from "../../hooks";
import { createSimpleContext } from "../simple";
import { useApi } from "./api";
const useAssetsHook = ({ filterCb = () => true, }) => {
    const { api } = useApi();
    const native = useMemo(() => "Eq", [api?._type]); // TODO use eqAssets pallete when assets are in mainnet
    const stable = useMemo(() => (api?._type === "Eq" ? "Usd" : "Eqd"), [api]);
    const tokens = useAsyncMemo(async () => api &&
        (await api.derive.assets.names())
            .filter((token) => typeof token === "string" &&
            !["Unknown", "UNKNOWN"].includes(token))
            .filter(filterCb), [api]);
    const assets = useMemo(() => tokens.data?.map(u64FromCurrency) || undefined, [tokens]);
    /**
     * @deprecated do not use as we do not rely on chainDecimals anymore
     */
    const getDecimals = useCallback((token) => {
        if (api?._api) {
            const chainTokenId = api._api.registry.chainTokens.findIndex((e) => e === token);
            const chainDecimals = api._api.registry.chainDecimals.find((_, i) => i === chainTokenId);
            return chainDecimals;
        }
    }, [api?._api]);
    const availableTokens = useMemo(() => {
        return tokens.data;
    }, [tokens.data]);
    const currencies = useMemo(() => {
        return tokens.data;
    }, [tokens.data]);
    return {
        assets,
        tokens: tokens.data,
        availableTokens,
        native,
        stable,
        /**
         * @deprecated
         * */
        getDecimals,
        /**
         * @deprecated use tokens instead
         * */
        currencies,
    };
};
const Context = createSimpleContext("eq-assets", {
    assets: undefined,
    currencies: undefined,
    tokens: undefined,
    availableTokens: undefined,
    native: "Eq",
    stable: "Usd",
    getDecimals: (_) => {
        throw "Not implemented";
    },
}, useAssetsHook);
export const AssetsProvider = Context.Provider;
/** @deprecated please use only on Eq network */
export const useAssets = Context.useContext;
//# sourceMappingURL=assets.js.map