"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Assets", {
    enumerable: true,
    get: function() {
        return Assets;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _text = require("../../../text");
const _progress = require("../../../progress");
const _lucidereact = require("lucide-react");
const _hooks = require("@orderly.network/hooks");
const _collapsible = require("../../../collapsible/collapsible");
const _toast = require("../../../toast");
const _leverage = require("../../../block/accountStatus/full/leverage");
const _assetsDetail = require("../../../block/accountStatus/full/assetsDetail");
const _button = /*#__PURE__*/ _interop_require_default(require("../../../button"));
const _icon = require("../../../icon");
const _types = require("@orderly.network/types");
const _walletConnect = require("../../../block/walletConnect");
const _modal = require("../../../modal");
const _utils = require("@orderly.network/utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const KEY = "ORDERLY_WEB_ASSETS_COLLAPSED";
const Assets = (props)=>{
    // const [expand, { toggle }] = useBoolean(false);
    const [collapsed, setCollapsed] = (0, _hooks.useLocalStorage)(KEY, 1);
    const { totalCollateral, freeCollateral, totalValue, availableBalance } = (0, _hooks.useCollateral)({
        dp: 2
    });
    const [disableGetTestUSDC, setDisableGetTestUSDC] = (0, _react.useState)(false);
    const { connectedChain } = (0, _hooks.useWalletConnector)();
    const config = (0, _hooks.useConfig)();
    const { account, state } = (0, _hooks.useAccount)();
    const showGetTestUSDC = (0, _react.useMemo)(()=>{
        const chainId = connectedChain?.id;
        if (chainId === undefined) {
            return false;
        }
        return state.status === _types.AccountStatusEnum.EnableTrading && // @ts-ignore
        (0, _utils.isTestnet)(parseInt(chainId));
    }, [
        state.status,
        connectedChain
    ]);
    const [getTestUSDC, { isMutating }] = (0, _hooks.useMutation)(`${config.get("operatorUrl")}/v1/faucet/usdc`);
    const onGetClick = (0, _react.useCallback)(()=>{
        if (state.status < _types.AccountStatusEnum.EnableTrading) {
            return _modal.modal.show(_walletConnect.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.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.toast.error(res.message);
        // return Promise.reject(res);
        }, (error)=>{
            _toast.toast.error(error.message);
        });
    }, [
        state
    ]);
    const [{ aggregated }, positionsInfo] = (0, _hooks.usePositionStream)();
    const { marginRatio } = (0, _hooks.useMarginRatio)();
    const marginRatioVal = (0, _react.useMemo)(()=>{
        return Math.min(10, aggregated.notional === 0 ? positionsInfo["margin_ratio"](10) : marginRatio);
    }, [
        marginRatio,
        aggregated
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_collapsible.Collapsible, {
        open: collapsed > 0,
        onOpenChange: (value)=>{
            setCollapsed(value ? 1 : 0);
        },
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-py-3 orderly-flex orderly-justify-between orderly-items-center orderly-tabular-nums",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: "orderly-flex-1",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                className: "orderly-text-3xs orderly-text-base-contrast-54",
                                children: "Total balance"
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Numeral, {
                                    surfix: /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                                        className: "orderly-text-base-contrast-36 orderly-font-medium",
                                        children: "USDC"
                                    }),
                                    className: "desktop:orderly-font-semibold",
                                    children: availableBalance
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_collapsible.CollapsibleTrigger, {
                        asChild: true,
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                            className: "orderly-p-1 orderly-rounded hover:orderly-bg-base-900 data-[state=open]:orderly-rotate-180 orderly-transition-transform",
                            children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_lucidereact.ChevronDown, {
                                size: 18,
                                className: "orderly-text-base-contrast-54 orderly-z-0"
                            })
                        })
                    })
                ]
            }),
            showGetTestUSDC && /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-mb-3 orderly-w-full",
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_button.default, {
                    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__*/ (0, _jsxruntime.jsx)(_icon.NetworkImage, {
                            type: "token",
                            name: "USDC",
                            size: 16,
                            rounded: true
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                            className: "orderly-text-base-contrast orderly-text-3xs",
                            children: "Get 1,000 test USDC"
                        })
                    ]
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_collapsible.CollapsibleContent, {
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_assetsDetail.MemorizedAssetsDetail, {})
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-pb-4",
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_progress.Progress, {
                    value: marginRatioVal * 10,
                    variant: "gradient"
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_leverage.MemorizedLeverage, {})
        ]
    });
};

//# sourceMappingURL=assets.js.map