//@ts-ignore
import BrowserOnly from "@docusaurus/BrowserOnly";
import { useWallet } from "@solana/wallet-adapter-react";
import { WalletModalProvider, WalletMultiButton, } from "@solana/wallet-adapter-react-ui";
import { useAssociatedAccount, useBondingPricing, useErrorHandler, useMint, usePublicKey, useSwap, useTokenBonding, } from "@strata-foundation/react";
import { toNumber } from "@strata-foundation/spl-token-bonding";
import React from "react";
import { useEndpoint } from "@strata-foundation/react";
//@ts-ignore
import styles from "./styles.module.css";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
const TOKEN_BONDING = "B8kzSwXLfmZMeLzekExz2e8vefoU1UqgGnZ8NZRYkeou";
const MainnetGuard = ({ children = null }) => {
    const { endpoint, setClusterOrEndpoint } = useEndpoint();
    if (endpoint.includes("devnet")) {
        return (React.createElement("div", { className: styles.container },
            React.createElement("h3", null, "Net bWUM Exchange"),
            React.createElement("button", { onClick: () => {
                    setClusterOrEndpoint(WalletAdapterNetwork.Mainnet);
                }, className: "white button button--primary" }, "Switch to Mainnet")));
    }
    return children;
};
export const Claim = React.memo(() => {
    const tokenBondingKey = usePublicKey(TOKEN_BONDING);
    const { connected, publicKey } = useWallet();
    const { info: tokenBonding, loading } = useTokenBonding(tokenBondingKey);
    const { pricing, loading: loadingPricing } = useBondingPricing(tokenBondingKey);
    const { associatedAccount: netBwumAccount, loading: assocAccountLoading } = useAssociatedAccount(publicKey, tokenBonding?.targetMint);
    const targetMint = useMint(tokenBonding?.targetMint);
    const netTotal = netBwumAccount &&
        targetMint &&
        toNumber(netBwumAccount.amount, targetMint) / Math.pow(10, 9);
    const solTotal = netTotal && pricing?.sellTargetAmount(netTotal);
    const connectedNotLoading = !(loading || loadingPricing || assocAccountLoading) && connected;
    const { execute, loading: executing, error } = useSwap();
    const { handleErrors } = useErrorHandler();
    handleErrors(error);
    return (React.createElement("div", { className: styles.container },
        React.createElement("h3", null, "Net bWUM Exchange"),
        !connected && (React.createElement(WalletModalProvider, null,
            React.createElement(WalletMultiButton, null))),
        (loadingPricing || loading || assocAccountLoading) && (React.createElement("span", null, "Loading...")),
        connectedNotLoading && !netTotal && !solTotal && (React.createElement("span", null, "No Net bWUM Found. Make sure you have the wallet that participated in the Wum.bo beta connected, then refresh this page.")),
        error && React.createElement("span", { style: { color: "red" } }, error.toString()),
        connectedNotLoading && netTotal && solTotal && (React.createElement("div", { style: { display: "flex", flexDirection: "column" } },
            React.createElement("p", null,
                "Exchange ",
                netTotal.toFixed(4),
                " Net bWUM for ",
                solTotal.toFixed(4),
                " ",
                "SOL"),
            React.createElement("button", { disabled: executing, onClick: () => {
                    if (tokenBonding) {
                        execute({
                            baseMint: tokenBonding.targetMint,
                            targetMint: tokenBonding.baseMint,
                            baseAmount: netTotal,
                            slippage: 0.05,
                        });
                    }
                }, className: "white button button--primary" }, "Exchange")))));
});
export const ClaimBwum = () => {
    return (React.createElement(BrowserOnly, { fallback: React.createElement("div", null, "...") }, () => (React.createElement(MainnetGuard, null,
        React.createElement(Claim, null)))));
};
//# sourceMappingURL=index.js.map