"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "AccountInfo", {
    enumerable: true,
    get: function() {
        return AccountInfo;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _avatar = require("../../../avatar");
const _button = /*#__PURE__*/ _interop_require_wildcard(require("../../../button"));
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _text = require("../../../text");
const _hooks = require("@orderly.network/hooks");
const _toast = require("../../../toast");
const _modal = require("../../../modal");
const _types = require("@orderly.network/types");
const _walletConnect = require("../../../block/walletConnect");
const _icon = require("../../../icon");
const _useGetChains = require("./useGetChains");
const _divider = require("../../../divider");
const _statistic = require("../../../statistic");
const _provider = require("../../../provider");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const AccountInfo = (props)=>{
    const { onDisconnect } = props;
    const { account, state } = (0, _hooks.useAccount)();
    // const [loading,setLoading] = React.useState(false);
    const config = (0, _hooks.useConfig)();
    const [getTestUSDC, { isMutating }] = (0, _hooks.useMutation)(`${config.get("operatorUrl")}/v1/faucet/usdc`);
    const chainName = (0, _useGetChains.useGetChains)();
    const onCopy = (0, _react.useCallback)(()=>{
        navigator.clipboard.writeText(state.address).then(()=>{
            _toast.toast.success("Copied to clipboard");
        });
    }, [
        state
    ]);
    const onGetClick = (0, _react.useCallback)(()=>{
        if (state.status < _types.AccountStatusEnum.EnableTrading) {
            return _modal.modal.show(_walletConnect.WalletConnectSheet, {
                status: state.status
            });
        }
        return getTestUSDC({
            chain_id: account.wallet?.chainId.toString(),
            user_address: state.address,
            broker_id: config.get("brokerId")
        }).then((res)=>{
            if (res.success) {
                props.close?.();
                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
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        id: "orderly-account-info",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-flex orderly-py-6",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: "orderly-flex-1 orderly-flex orderly-items-center orderly-gap-2",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_avatar.Blockie, {
                                address: state.address
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                className: "orderly-flex orderly-flex-col",
                                children: [
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                                        className: "orderly-text-xs",
                                        rule: "address",
                                        children: account.address
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                        className: "orderly-text-4xs orderly-text-base-contrast-80",
                                        children: chainName
                                    })
                                ]
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-flex orderly-items-center orderly-gap-2",
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.IconButton, {
                            type: "button",
                            onClick: onCopy,
                            className: "orderly-px-0",
                            children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.CopyIcon, {
                                size: 40
                            })
                        })
                    })
                ]
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(ReferralInfo, {}),
            props.showGetTestUSDC ? /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-py-4 orderly-grid orderly-grid-cols-2 orderly-gap-3",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                        variant: "outlined",
                        onClick: onGetClick,
                        disabled: isMutating,
                        loading: isMutating,
                        className: "orderly-text-xs orderly-text-primary orderly-font-bold orderly-border-primary hover:orderly-bg-transparent hover:orderly-text-primary-light",
                        children: "Get test USDC"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                        variant: "outlined",
                        color: "danger",
                        fullWidth: true,
                        className: "orderly-text-xs orderly-font-bold orderly-text-danger-light orderly-border-danger-light hover:orderly-bg-transparent hover:orderly-text-danger-light",
                        onClick: ()=>{
                            onDisconnect?.();
                        },
                        children: "Disconnect"
                    })
                ]
            }) : /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-py-4 orderly-flex orderly-justify-center",
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                    variant: "outlined",
                    color: "danger",
                    fullWidth: true,
                    className: "orderly-text-xs orderly-font-bold orderly-text-danger-light orderly-border-danger-light hover:orderly-bg-transparent hover:orderly-text-danger-light",
                    onClick: ()=>{
                        onDisconnect?.();
                    },
                    children: "Disconnect"
                })
            })
        ]
    });
};
const ReferralInfo = ()=>{
    const { referral } = (0, _react.useContext)(_provider.OrderlyAppContext);
    const clickReferral = (0, _react.useCallback)(()=>{
        referral?.onClickReferral?.();
    }, [
        referral?.onClickReferral
    ]);
    const { data } = (0, _hooks.usePrivateQuery)("/v1/referral/info", {
        revalidateOnFocus: true
    });
    const commission = (0, _react.useMemo)(()=>{
        if (!data) return "-";
        return data.referee_info?.["30d_referee_rebate"] + data.referrer_info?.["30d_referrer_rebate"];
    }, [
        data
    ]);
    const { data: volumeStatistics } = (0, _hooks.usePrivateQuery)("/v1/volume/user/stats", {
        revalidateOnFocus: true
    });
    const vol = (0, _react.useMemo)(()=>{
        if (volumeStatistics && data) {
            return volumeStatistics?.perp_volume_last_30_days || 0 + data.referrer_info?.["30d_referee_volume"];
        }
        return "-";
    }, [
        data,
        volumeStatistics
    ]);
    if (referral?.saveRefCode !== true) {
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_jsxruntime.Fragment, {});
    }
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: "orderly-bg-base-600 orderly-rounded-lg orderly-p-3 orderly-mb-3",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-flex orderly-items-center orderly-cursor-pointer",
                onClick: clickReferral,
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-flex-1",
                        children: "Referral"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.ArrowRightIcon, {
                        size: 14,
                        fillOpacity: 1,
                        className: "orderly-fill-primary"
                    })
                ]
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_divider.Divider, {
                className: "orderly-py-3"
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-grid orderly-grid-cols-2",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_statistic.Statistic, {
                        labelClassName: "orderly-text-3xs orderly-text-base-contrast-36",
                        valueClassName: "orderly-mt-1 orderly-text-[16px]",
                        label: "30d commission",
                        value: commission,
                        precision: 2,
                        rule: "price"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_statistic.Statistic, {
                        labelClassName: "orderly-text-3xs orderly-text-base-contrast-36",
                        valueClassName: "orderly-mt-1 orderly-text-[16px]",
                        label: "30d vol.",
                        value: vol,
                        precision: 2,
                        rule: "price"
                    })
                ]
            })
        ]
    });
};

//# sourceMappingURL=accountInfo.js.map