import { useCallback } from "react";
import { log } from "@ledgerhq/logs";
import { getEnv } from "@ledgerhq/live-env";
import { formatOperation, getMainAccount } from "../account/index";
import { getAccountBridge } from "../bridge/index";
import { execAndWaitAtLeast } from "../promise";
export const useBroadcast = ({ account, parentAccount, broadcastConfig }) => {
    const broadcast = useCallback(async (signedOperation) => {
        if (!account)
            throw new Error("account not present");
        const mainAccount = getMainAccount(account, parentAccount);
        const bridge = getAccountBridge(account, parentAccount);
        if (getEnv("DISABLE_TRANSACTION_BROADCAST")) {
            return Promise.resolve(signedOperation.operation);
        }
        return execAndWaitAtLeast(3000, async () => {
            const operation = await bridge.broadcast({
                account: mainAccount,
                signedOperation,
                broadcastConfig,
            });
            log("transaction-summary", `✔️ broadcasted! optimistic operation: ${formatOperation(mainAccount)(operation)}`);
            return operation;
        });
    }, [account, parentAccount, broadcastConfig]);
    return broadcast;
};
//# sourceMappingURL=useBroadcast.js.map