"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CloseButton", {
    enumerable: true,
    get: function() {
        return CloseButton;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _positionRowContext = require("./positionRowContext");
const _popover = require("../../../popover");
const _types = require("@orderly.network/types");
const _symbolProvider = require("../../../provider/symbolProvider");
const _marketConfirmDialog = require("./marketConfirmDialog");
const _css = require("../../../utils/css");
const _limitConfirmDialog = require("./limitConfirmDialog");
const _toast = require("../../../toast");
const CloseButton = ()=>{
    const [open, setOpen] = (0, _react.useState)(false);
    const { onSubmit, price, quantity, closeOrderData, type, submitting } = (0, _positionRowContext.usePositionsRowContext)();
    const { base, quote } = (0, _symbolProvider.useSymbolContext)();
    const onConfirm = ()=>{
        return onSubmit().then((res)=>{
            setOpen(false);
        }, (error)=>{
            if (typeof error === 'string') {
                _toast.toast.error(error);
            } else {
                _toast.toast.error(error.message);
            }
        });
    };
    const onClose = ()=>{
        setOpen(false);
    };
    const disabled = (0, _react.useMemo)(()=>{
        if (type === _types.OrderType.MARKET) {
            if (!quantity) {
                return true;
            }
            return false;
        }
        return !price || !quantity;
    }, [
        price,
        quantity,
        type
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_popover.Popover, {
        onOpenChange: setOpen,
        open: open,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverTrigger, {
                asChild: true,
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
                    disabled: disabled,
                    className: (0, _css.cn)("orderly-border orderly-border-base-contrast-36 orderly-text-base-contrast-54 orderly-rounded orderly-w-full orderly-h-[28px] hover:orderly-bg-base-contrast/10 disabled:orderly-opacity-50 disabled:orderly-cursor-not-allowed", {
                        "orderly-text-base-contrast-80 orderly-border-base-contrast-80": open
                    }),
                    children: "Close"
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverContent, {
                align: "end",
                side: "top",
                className: "orderly-w-[340px]",
                children: type === _types.OrderType.MARKET ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_marketConfirmDialog.CloseBaseConfirm, {
                    base: base,
                    quantity: quantity,
                    onClose: onClose,
                    onConfirm: onConfirm,
                    submitting: submitting
                }) : /*#__PURE__*/ (0, _jsxruntime.jsx)(_limitConfirmDialog.LimitConfirmDialog, {
                    order: closeOrderData,
                    base: base,
                    quote: quote,
                    quantity: quantity,
                    onClose: onClose,
                    onConfirm: onConfirm,
                    submitting: submitting
                })
            })
        ]
    });
};

//# sourceMappingURL=closeButton.js.map