"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "LeverageDialog", {
    enumerable: true,
    get: function() {
        return LeverageDialog;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _dialog = require("../../../dialog");
const _leverageEditor = require("../sections/leverageEditor");
const _button = /*#__PURE__*/ _interop_require_default(require("../../../button"));
const _hooks = require("@orderly.network/hooks");
const _toast = require("../../../toast");
const _numeral = require("../../../text/numeral");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const LeverageDialog = (props)=>{
    const [open, setOpen] = (0, _react.useState)(false);
    const { currentLeverage } = (0, _hooks.useMarginRatio)();
    const [maxLeverage, { update, config: leverageLevers, isMutating }] = (0, _hooks.useLeverage)();
    const nextLeverage = (0, _react.useRef)(maxLeverage ?? 0);
    const onSave = (value)=>{
        return Promise.resolve().then(()=>{
            //   console.log("value", value);
            nextLeverage.current = value.leverage;
        });
    };
    const onSubmit = ()=>{
        if (nextLeverage.current === maxLeverage) return;
        update({
            leverage: nextLeverage.current
        }).then((res)=>{
            setOpen(false);
            _toast.toast.success("Leverage updated");
        }, (err)=>{
            ;
            _toast.toast.error(err.message);
        });
    };
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.Dialog, {
        open: open,
        onOpenChange: setOpen,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogTrigger, {
                asChild: true,
                children: props.children
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogContent, {
                onOpenAutoFocus: (e)=>e.preventDefault(),
                closable: true,
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogHeader, {
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_dialog.DialogTitle, {
                            children: "Account Leverage"
                        })
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogBody, {
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                className: "orderly-flex orderly-text-base-contrast-54 orderly-py-5 desktop:orderly-text-xs",
                                children: [
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                        className: "orderly-flex-1",
                                        children: "Max account leverage"
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                        className: "orderly-flex orderly-gap-1",
                                        children: [
                                            /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                                                children: "Current:"
                                            }),
                                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_numeral.Numeral, {
                                                className: "orderly-text-base-contrast",
                                                surfix: "x",
                                                children: currentLeverage
                                            })
                                        ]
                                    })
                                ]
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                className: "ordelry-my-5 orderly-h-[80px]",
                                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_leverageEditor.LeverageEditor, {
                                    maxLeverage: maxLeverage,
                                    leverageLevers: leverageLevers,
                                    onSave: onSave
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dialog.DialogFooter, {
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                fullWidth: true,
                                color: "tertiary",
                                onClick: ()=>{
                                    setOpen(false);
                                },
                                children: "Cancel"
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                fullWidth: true,
                                onClick: ()=>onSubmit(),
                                loading: isMutating,
                                children: "Save"
                            })
                        ]
                    })
                ]
            })
        ]
    });
};

//# sourceMappingURL=leverageDialog.js.map