import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useMemo, useState } from "react";
import { Numeral } from "../../../text";
import { Progress } from "../../../progress";
import { ChevronDown } from "lucide-react";
import { useLocalStorage, useCollateral, usePositionStream, useMarginRatio, useAccount, useConfig, useWalletConnector, useMutation } from "@orderly.network/hooks";
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from "../../../collapsible/collapsible";
import { toast } from "../../../toast";
import { MemorizedLeverage } from "../../../block/accountStatus/full/leverage";
import { MemorizedAssetsDetail } from "../../../block/accountStatus/full/assetsDetail";
import Button from "../../../button";
import { NetworkImage } from "../../../icon";
import { AccountStatusEnum } from "@orderly.network/types";
import { WalletConnectSheet } from "../../../block/walletConnect";
import { modal } from "../../../modal";
import { isTestnet } from "@orderly.network/utils";
const KEY = "ORDERLY_WEB_ASSETS_COLLAPSED";
export const Assets = (props)=>{
    // const [expand, { toggle }] = useBoolean(false);
    const [collapsed, setCollapsed] = useLocalStorage(KEY, 1);
    const { totalCollateral, freeCollateral, totalValue, availableBalance } = useCollateral({
        dp: 2
    });
    const [disableGetTestUSDC, setDisableGetTestUSDC] = useState(false);
    const { connectedChain } = useWalletConnector();
    const config = useConfig();
    const { account, state } = useAccount();
    const showGetTestUSDC = useMemo(()=>{
        const chainId = connectedChain?.id;
        if (chainId === undefined) {
            return false;
        }
        return state.status === AccountStatusEnum.EnableTrading && // @ts-ignore
        isTestnet(parseInt(chainId));
    }, [
        state.status,
        connectedChain
    ]);
    const [getTestUSDC, { isMutating }] = useMutation(`${config.get("operatorUrl")}/v1/faucet/usdc`);
    const onGetClick = useCallback(()=>{
        if (state.status < AccountStatusEnum.EnableTrading) {
            return modal.show(WalletConnectSheet, {
                status: state.status
            });
        }
        setDisableGetTestUSDC(true);
        return getTestUSDC({
            chain_id: account.wallet?.chainId.toString(),
            user_address: state.address,
            broker_id: config.get("brokerId")
        }).then((res)=>{
            setDisableGetTestUSDC(false);
            if (res.success) {
                return modal.confirm({
                    title: "Get test USDC",
                    content: "1,000 USDC will be added to your balance. Please note this may take up to 3 minutes. Please check back later.",
                    onOk: ()=>{
                        return Promise.resolve();
                    }
                });
            }
            res.message && toast.error(res.message);
        // return Promise.reject(res);
        }, (error)=>{
            toast.error(error.message);
        });
    }, [
        state
    ]);
    const [{ aggregated }, positionsInfo] = usePositionStream();
    const { marginRatio } = useMarginRatio();
    const marginRatioVal = useMemo(()=>{
        return Math.min(10, aggregated.notional === 0 ? positionsInfo["margin_ratio"](10) : marginRatio);
    }, [
        marginRatio,
        aggregated
    ]);
    return /*#__PURE__*/ _jsxs(Collapsible, {
        open: collapsed > 0,
        onOpenChange: (value)=>{
            setCollapsed(value ? 1 : 0);
        },
        children: [
            /*#__PURE__*/ _jsxs("div", {
                className: "orderly-py-3 orderly-flex orderly-justify-between orderly-items-center orderly-tabular-nums",
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: "orderly-flex-1",
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: "orderly-text-3xs orderly-text-base-contrast-54",
                                children: "Total balance"
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                children: /*#__PURE__*/ _jsx(Numeral, {
                                    surfix: /*#__PURE__*/ _jsx("span", {
                                        className: "orderly-text-base-contrast-36 orderly-font-medium",
                                        children: "USDC"
                                    }),
                                    className: "desktop:orderly-font-semibold",
                                    children: availableBalance
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx(CollapsibleTrigger, {
                        asChild: true,
                        children: /*#__PURE__*/ _jsx("button", {
                            className: "orderly-p-1 orderly-rounded hover:orderly-bg-base-900 data-[state=open]:orderly-rotate-180 orderly-transition-transform",
                            children: /*#__PURE__*/ _jsx(ChevronDown, {
                                size: 18,
                                className: "orderly-text-base-contrast-54 orderly-z-0"
                            })
                        })
                    })
                ]
            }),
            showGetTestUSDC && /*#__PURE__*/ _jsx("div", {
                className: "orderly-mb-3 orderly-w-full",
                children: /*#__PURE__*/ _jsxs(Button, {
                    id: "orderly-desktop-get-test-usdc",
                    variant: "outlined",
                    fullWidth: true,
                    size: "small",
                    className: "orderly-border-base-contrast-54 hover:orderly-bg-base-700 orderly-h-[28px] orderly-rounded-borderRadius-lg",
                    onClick: onGetClick,
                    disabled: disableGetTestUSDC,
                    children: [
                        /*#__PURE__*/ _jsx(NetworkImage, {
                            type: "token",
                            name: "USDC",
                            size: 16,
                            rounded: true
                        }),
                        /*#__PURE__*/ _jsx("span", {
                            className: "orderly-text-base-contrast orderly-text-3xs",
                            children: "Get 1,000 test USDC"
                        })
                    ]
                })
            }),
            /*#__PURE__*/ _jsx(CollapsibleContent, {
                children: /*#__PURE__*/ _jsx(MemorizedAssetsDetail, {})
            }),
            /*#__PURE__*/ _jsx("div", {
                className: "orderly-pb-4",
                children: /*#__PURE__*/ _jsx(Progress, {
                    value: marginRatioVal * 10,
                    variant: "gradient"
                })
            }),
            /*#__PURE__*/ _jsx(MemorizedLeverage, {})
        ]
    });
};

//# sourceMappingURL=assets.js.map