//@ts-ignore
import BrowserOnly from "@docusaurus/BrowserOnly";
import { useEndpoint } from "@strata-foundation/react";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import { useWallet } from "@solana/wallet-adapter-react";
import { WalletModalProvider, WalletMultiButton } from "@solana/wallet-adapter-react-ui";
import { PublicKey } from "@solana/web3.js";
import { Spinner, useAssociatedAccount, useBondingPricing, useErrorHandler, useOwnedAmount, usePublicKey, useSolanaUnixTime, useStrataSdks, useTokenBonding } from "@strata-foundation/react";
import React, { useEffect, useState } from "react";
import { useAsyncCallback } from "react-async-hook";
//@ts-ignore
import styles from "./styles.module.css";
const TOKEN_BONDING = "BBZ6tFH5b6tWxWebUe7xyWLZ3PHVCLAdRArAEACuJKHe";
const MainnetGuard = ({ children = null }) => {
    const { endpoint, setClusterOrEndpoint } = useEndpoint();
    if (endpoint.includes("devnet")) {
        return (React.createElement("div", { className: styles.container },
            React.createElement("button", { onClick: () => {
                    setClusterOrEndpoint(WalletAdapterNetwork.Mainnet);
                }, className: "white button button--primary" }, "Switch to Mainnet")));
    }
    return children;
};
const roundToDecimals = (num, decimals) => Math.trunc(num * Math.pow(10, decimals)) / Math.pow(10, decimals);
async function exchange(tokenBondingSdk, amount) {
    await tokenBondingSdk.sell({
        tokenBonding: new PublicKey(TOKEN_BONDING),
        targetAmount: amount,
        slippage: 0.05
    });
}
export const ClaimO = () => {
    const [success, setSuccess] = useState(false);
    const [claimableOPEN, setClaimableOPEN] = useState();
    const tokenBondingKey = usePublicKey(TOKEN_BONDING);
    const { connected, publicKey } = useWallet();
    const { info: tokenBonding, loading: loadingBonding } = useTokenBonding(tokenBondingKey);
    const { tokenBondingSdk } = useStrataSdks();
    const { execute: swap, loading: swapping, error } = useAsyncCallback(exchange);
    const { handleErrors } = useErrorHandler();
    handleErrors(error);
    const { pricing, loading: loadingPricing } = useBondingPricing(tokenBondingKey);
    const ownedTarget = useOwnedAmount(tokenBonding?.targetMint);
    const { associatedAccount } = useAssociatedAccount(publicKey, tokenBonding?.targetMint);
    const unixTime = useSolanaUnixTime();
    useEffect(() => {
        if (ownedTarget && ownedTarget >= 0 && tokenBonding && pricing) {
            const claimable = pricing.swap(+ownedTarget, tokenBonding.targetMint, tokenBonding.baseMint, false, unixTime);
            setClaimableOPEN(ownedTarget == 0 ? 0 : roundToDecimals(claimable, 9));
        }
    }, [tokenBonding, pricing, ownedTarget, setClaimableOPEN]);
    const handleExchange = async () => {
        await swap(tokenBondingSdk, associatedAccount.amount);
        setSuccess(true);
    };
    const isLoading = loadingPricing || loadingBonding;
    const connectedNotLoading = connected && !isLoading;
    return (React.createElement("div", { className: styles.container },
        React.createElement("h3", null, "netbWUM to OPEN Exchange"),
        !connected && (React.createElement(WalletModalProvider, null,
            React.createElement(WalletMultiButton, null))),
        isLoading && React.createElement(Spinner, null),
        connectedNotLoading && !success && !ownedTarget && !claimableOPEN && (React.createElement("span", null, "No netbWUM Found. Make sure you have the wallet that participated in the Wum.bo beta connected, then refresh this page.")),
        success && React.createElement("span", null, "Successfully swapped netbWUM to OPEN!"),
        connectedNotLoading && !success && ownedTarget && claimableOPEN && (React.createElement("div", { style: { display: "flex", flexDirection: "column" } },
            React.createElement("p", null,
                "Exchange ",
                ownedTarget.toFixed(4),
                " netbWUM for",
                " ",
                claimableOPEN.toFixed(4),
                " OPEN"),
            React.createElement("button", { disabled: swapping, onClick: handleExchange, className: "white button button--primary" }, "Exchange")))));
};
export const ClaimOpen = () => {
    return (React.createElement(BrowserOnly, { fallback: React.createElement("div", null, "...") }, () => (React.createElement(MainnetGuard, null,
        React.createElement(ClaimO, null)))));
};
//# sourceMappingURL=index.js.map