import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { TradingView } from "@orderly.network/trading-view";
import { Popover, PopoverContent, PopoverTrigger } from "../../../popover";
import { useState } from "react";
import { SymbolProvider } from "../../../provider/symbolProvider";
import { toast } from "../../../toast";
import { useOrderEntry, useSymbolsInfo } from "@orderly.network/hooks";
import { CloseBaseConfirm } from "../../../block/positions/full/marketConfirmDialog";
import { OrderSide, OrderType } from "@orderly.network/types";
import { Decimal } from "@orderly.network/utils";
import { Spinner } from "../../../spinner";
export const MyTradingView = ({ symbol, tradingViewConfig })=>{
    const [open, setOpen] = useState(false);
    const [side, setSide] = useState(OrderSide.BUY);
    const { helper, onSubmit, submitting } = useOrderEntry(symbol, side, true);
    const symbolInfo = useSymbolsInfo()[symbol];
    const [orderData, setOrderData] = useState();
    const closePositionConfirmCallback = (data)=>{
        const side = new Decimal(data.balance).greaterThan(0) ? OrderSide.SELL : OrderSide.BUY;
        const order = {
            //   order_price: undefined,
            order_quantity: data.balance,
            symbol: symbol,
            order_type: OrderType.MARKET,
            side,
            reduce_only: true
        };
        setOrderData(order);
        setSide(side);
        setOpen(true);
    };
    const onConfirm = async ()=>{
        orderData.order_quantity = Math.abs(orderData.order_quantity);
        return onSubmit(orderData).then((res)=>{
            setOpen(false);
        }, (error)=>{
            if (typeof error === "string") {
                toast.error(error);
                return;
            }
            toast.error(error.message);
        });
    };
    const onClose = ()=>{
        setOpen(false);
    };
    return /*#__PURE__*/ _jsx("div", {
        style: {
            position: "relative",
            width: "100%",
            height: "100%"
        },
        children: /*#__PURE__*/ _jsxs(SymbolProvider, {
            symbol: symbol,
            children: [
                /*#__PURE__*/ _jsx(TradingView, {
                    symbol: symbol,
                    libraryPath: tradingViewConfig?.library_path,
                    tradingViewScriptSrc: tradingViewConfig?.scriptSRC,
                    tradingViewCustomCssUrl: tradingViewConfig?.customCssUrl,
                    overrides: tradingViewConfig?.overrides,
                    studiesOverrides: tradingViewConfig?.studiesOverrides,
                    closePositionConfirmCallback: closePositionConfirmCallback,
                    onToast: toast,
                    loadingElement: /*#__PURE__*/ _jsx(Spinner, {})
                }),
                /*#__PURE__*/ _jsxs(Popover, {
                    onOpenChange: setOpen,
                    open: open,
                    children: [
                        /*#__PURE__*/ _jsx(PopoverTrigger, {
                            asChild: true,
                            children: /*#__PURE__*/ _jsx("div", {
                                style: {
                                    position: "absolute",
                                    width: "0",
                                    height: "0",
                                    left: "50%",
                                    top: "50%"
                                }
                            })
                        }),
                        /*#__PURE__*/ _jsx(PopoverContent, {
                            align: "start",
                            side: "top",
                            className: "orderly-w-[340px]",
                            children: /*#__PURE__*/ _jsx(CloseBaseConfirm, {
                                base: symbolInfo("base"),
                                quantity: orderData?.order_quantity ?? 0,
                                onClose: onClose,
                                onConfirm: onConfirm,
                                submitting: submitting
                            })
                        })
                    ]
                })
            ]
        })
    });
};

//# sourceMappingURL=myTradingview.js.map