//@ts-ignore
import BrowserOnly from "@docusaurus/BrowserOnly";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useWallet } from "@solana/wallet-adapter-react";
import { WalletModalProvider, WalletMultiButton, } from "@solana/wallet-adapter-react-ui";
import { useBondingPricing, useCollective, useErrorHandler, usePublicKey, useSolanaUnixTime, useStrataSdks, useUserTokensWithMeta, } from "@strata-foundation/react";
import { toNumber } from "@strata-foundation/spl-token-bonding";
import { useEndpoint } from "@strata-foundation/react";
//@ts-ignore
import styles from "./styles.module.css";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import { NATIVE_MINT } from "@solana/spl-token";
import { Box, Avatar, Stack, Text, SimpleGrid, Alert, AlertIcon, AlertDescription, AlertTitle, Button, } from "@chakra-ui/react";
import { closeOutWumboSubmit } from "./closeOutWumboSubmit";
const OPEN_COLLECTIVE = "3cYa5WvT2bgXSLxxu9XDJSHV3x5JZGM91Nc3B7jYhBL7";
const MainnetGuard = ({ children = null }) => {
    const { endpoint, setClusterOrEndpoint } = useEndpoint();
    if (endpoint.includes("devnet")) {
        return (React.createElement("div", { className: styles.container },
            React.createElement("h3", null, "Recoup SOL from Wumbo"),
            React.createElement("button", { onClick: () => {
                    setClusterOrEndpoint(WalletAdapterNetwork.Mainnet);
                }, className: "white button button--primary" }, "Switch to Mainnet")));
    }
    return children;
};
export const TokenHandler = React.memo(({ token, setAmountByToken }) => {
    const { publicKey: tokenBondingKey, targetMint } = token.tokenBonding;
    const unixTime = useSolanaUnixTime();
    const { pricing, loading: loadingPricing } = useBondingPricing(tokenBondingKey);
    const solAmount = pricing?.swap(toNumber(token.account.amount, token.mint), targetMint, NATIVE_MINT, true, unixTime);
    useEffect(() => {
        if (solAmount) {
            setAmountByToken(token, solAmount);
        }
    }, [solAmount, setAmountByToken]);
    return (React.createElement(Box, null,
        React.createElement(Stack, { direction: "row", spacing: 4, align: "center" },
            React.createElement(Avatar, { src: token.image, size: "sm" }),
            React.createElement(Stack, { direction: "column", spacing: 0, fontSize: "sm" },
                React.createElement(Text, { fontWeight: 600 }, token.metadata?.data?.name),
                React.createElement(Text, { color: "gray.500" },
                    solAmount,
                    " SOL")))));
});
export const Recoup = () => {
    const { connected, publicKey } = useWallet();
    const { tokenBondingSdk, loading: sdkLoading } = useStrataSdks();
    const { handleErrors } = useErrorHandler();
    const [amountsByToken, setAmountsByToken] = useState({});
    const [status, setStatus] = useState(null);
    const [isCalculatingTokens, setIsCalculatingTokens] = useState(true);
    const openCollectiveKey = usePublicKey(OPEN_COLLECTIVE);
    const { info: openCollective } = useCollective(openCollectiveKey);
    const { data: tokens, loading: loading1, error, } = useUserTokensWithMeta(publicKey || undefined);
    const setAmountByToken = useCallback((token, amount) => {
        if (token && amount) {
            setAmountsByToken((old) => ({
                ...old,
                [token.publicKey.toBase58()]: amount,
            }));
        }
    }, []);
    const open = useMemo(() => tokens.find(({ account }) => account.mint.equals(openCollective.mint)), [tokens]);
    const hasOpenAmount = useMemo(() => open && toNumber(open.account.amount, open.mint) > 0, [open]);
    const openCollectiveTokens = useMemo(() => tokens.filter((token) => !!token.tokenRef &&
        token.tokenRef.collective.equals(openCollectiveKey) &&
        toNumber(token.account.amount, token.mint) > 0), [tokens]);
    useEffect(() => {
        if (Object.values(amountsByToken).length ===
            openCollectiveTokens?.length + (hasOpenAmount ? 1 : 0)) {
            setIsCalculatingTokens(false);
        }
    }, [amountsByToken, hasOpenAmount, openCollectiveTokens]);
    handleErrors(error);
    const handleSubmit = useCallback(() => {
        closeOutWumboSubmit({
            tokenBondingSdk,
            tokens: hasOpenAmount
                ? [open, ...openCollectiveTokens]
                : openCollectiveTokens,
            expectedOutputAmountByToken: amountsByToken,
            setStatus,
        });
    }, [open, hasOpenAmount, openCollectiveTokens, amountsByToken]);
    const loading = sdkLoading || loading1 || isCalculatingTokens;
    const connectedLoading = connected && publicKey && loading;
    const connectedNotLoading = !loading && connected && publicKey;
    const isSuccessful = status == "successful";
    const isOrphaned = status == "orphaned";
    return (React.createElement("div", { className: styles.container },
        React.createElement("h3", null, "Recoup SOL from Wumbo"),
        !connected && (React.createElement(WalletModalProvider, null,
            React.createElement(WalletMultiButton, null))),
        connectedLoading && React.createElement("span", null, "Loading..."),
        connectedNotLoading &&
            !(hasOpenAmount || openCollectiveTokens.length) && (React.createElement("span", null, "No tokens relating to Wumbo/OPEN found. Make sure you have the wallet that you used with Wumbo/OPEN, then refresh this page.")),
        error && React.createElement("span", { style: { color: "red" } }, error.toString()),
        connectedNotLoading && !isSuccessful && !isOrphaned && (React.createElement(React.Fragment, null,
            React.createElement(SimpleGrid, { columns: { base: 1, md: 2 }, gap: 2 },
                hasOpenAmount && (React.createElement(TokenHandler, { token: open, setAmountByToken: setAmountByToken })),
                openCollectiveTokens.map((token) => (React.createElement(TokenHandler, { key: token.publicKey.toBase58(), token: token, setAmountByToken: setAmountByToken })))),
            React.createElement(Stack, { mt: 4, gap: 2 },
                React.createElement(Alert, { status: "info", variant: "subtle", flexDirection: "column", alignItems: "center", justifyContent: "center", textAlign: "center", height: "200px" },
                    React.createElement(AlertIcon, { boxSize: "30px", mr: 0 }),
                    React.createElement(AlertTitle, { mt: 4, mb: 1, fontSize: "lg" },
                        Object.values(amountsByToken).reduce((acc, amount) => acc + amount, 0),
                        " ",
                        "SOL"),
                    React.createElement(AlertDescription, { maxWidth: "sm" }, "Ready to be Recouped. Thanks for giving Wumbo a try. Our team greatly appreciated you and your early support!")),
                React.createElement(Button, { size: "lg", colorScheme: "orange", disabled: loading || status !== null, isLoading: status !== null, loadingText: status, onClick: handleSubmit, _hover: { cursor: "pointer" } }, "Recoup SOL")))),
        connectedNotLoading && isSuccessful && (React.createElement(Alert, { status: "success", variant: "subtle", flexDirection: "column", alignItems: "center", justifyContent: "center", textAlign: "center", height: "200px" },
            React.createElement(AlertIcon, { boxSize: "30px", mr: 0 }),
            React.createElement(AlertTitle, { mt: 4, mb: 1, fontSize: "lg" }, "SOL Recouped"),
            React.createElement(AlertDescription, { maxWidth: "sm" }, "Thanks for giving Wumbo a try. Our team greatly appreciated you and your early support!"))),
        connectedNotLoading && isOrphaned && (React.createElement(Alert, { status: "warning", variant: "subtle", flexDirection: "column", alignItems: "center", justifyContent: "center", textAlign: "center", height: "200px" },
            React.createElement(AlertIcon, { boxSize: "30px", mr: 0 }),
            React.createElement(AlertTitle, { mt: 4, mb: 1, fontSize: "lg" }, "Something went wrong"),
            React.createElement(AlertDescription, { maxWidth: "sm" }, "I looks like we were unable to recoup the full amount of SOL, one or multiple of the transactions may have failed. Please refresh and try again.")))));
};
export const CloseOutWumbo = () => (React.createElement(BrowserOnly, { fallback: React.createElement("div", null, "...") }, () => (React.createElement(MainnetGuard, null,
    React.createElement(Recoup, null)))));
//# sourceMappingURL=index.js.map