import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { usePositionsRowContext } from "./positionRowContext";
import { Popover, PopoverContent, PopoverTrigger } from "../../../popover";
import { OrderType } from "@orderly.network/types";
import { useSymbolContext } from "../../../provider/symbolProvider";
import { CloseBaseConfirm } from "./marketConfirmDialog";
import { cn } from "../../../utils/css";
import { LimitConfirmDialog } from "./limitConfirmDialog";
import { toast } from "../../../toast";
import { useDebouncedCallback } from "@orderly.network/hooks";
export const CloseButton = ()=>{
    const [open, setOpen] = useState(false);
    const [submitting, setSubmitting] = useState(false);
    // const submittingRef = useRef<boolean>(false);
    const { onSubmit, price, quantity, closeOrderData, type, submitting: orderProcessing } = usePositionsRowContext();
    const { base, quote } = useSymbolContext();
    const onConfirm = useDebouncedCallback(()=>{
        setSubmitting(true);
        return onSubmit().then((res)=>{
            setOpen(false);
        }, (error)=>{
            if (typeof error === "string") {
                toast.error(error);
            } else {
                toast.error(error.message);
            }
        }).finally(()=>{
            setSubmitting(false);
        // submittingRef.current = false;
        });
    }, 300, {
        leading: true
    });
    const onClose = ()=>{
        setOpen(false);
    };
    const disabled = useMemo(()=>{
        if (type === OrderType.MARKET) {
            if (!quantity) {
                return true;
            }
            return false;
        }
        return !price || !quantity;
    }, [
        price,
        quantity,
        type
    ]);
    return /*#__PURE__*/ _jsxs(Popover, {
        onOpenChange: setOpen,
        open: open,
        children: [
            /*#__PURE__*/ _jsx(PopoverTrigger, {
                asChild: true,
                children: /*#__PURE__*/ _jsx("button", {
                    disabled: disabled,
                    className: 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__*/ _jsx(PopoverContent, {
                align: "end",
                side: "top",
                className: "orderly-w-[340px]",
                children: type === OrderType.MARKET ? /*#__PURE__*/ _jsx(CloseBaseConfirm, {
                    base: base,
                    quantity: quantity,
                    onClose: onClose,
                    onConfirm: onConfirm,
                    submitting: submitting
                }) : /*#__PURE__*/ _jsx(LimitConfirmDialog, {
                    order: closeOrderData,
                    base: base,
                    quote: quote,
                    quantity: quantity,
                    onClose: onClose,
                    onConfirm: onConfirm,
                    submitting: submitting
                })
            })
        ]
    });
};

//# sourceMappingURL=closeButton.js.map