import { useCallback, useEffect, useMemo, useState } from "react";
import { usePolkadot } from "../contexts/polkadot";
export const useEqActions = () => {
    const { getAccount, api } = usePolkadot();
    const account = useMemo(() => getAccount(), [getAccount]);
    const transfer = useCallback((addressOrAccount, amount) => {
        if (!addressOrAccount || !account || !api) {
            throw new Error("something is not defined");
        }
        return new Promise((resolve, reject) => {
            try {
                api._api.tx.balances // TODO old method
                    .transfer(typeof addressOrAccount === "string"
                    ? addressOrAccount
                    : addressOrAccount.address, amount)
                    .signAndSend(account.address, (status) => resolve(status));
            }
            catch (error) {
                reject(error);
            }
        });
    }, [account, api]);
    return { transfer };
};
export function useAsyncValue(handler, dependencies, sendError) {
    const [value, setValue] = useState();
    const handleAsync = useCallback(async () => {
        try {
            const result = await handler();
            setValue(result);
        }
        catch (err) {
            if (err instanceof Error) {
                console.error(err);
                sendError && sendError(err.message);
            }
        }
    }, [handler]);
    useEffect(() => {
        if (!dependencies ||
            dependencies.some((dep) => dep !== undefined && dep !== null)) {
            (async () => handleAsync())();
        }
    }, [...(dependencies ?? [])]);
    return value;
}
//# sourceMappingURL=actions.js.map