"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "MyTradingView", {
    enumerable: true,
    get: function() {
        return MyTradingView;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _tradingview = require("@orderly.network/trading-view");
const _popover = require("../../../popover");
const _react = require("react");
const _symbolProvider = require("../../../provider/symbolProvider");
const _toast = require("../../../toast");
const _hooks = require("@orderly.network/hooks");
const _marketConfirmDialog = require("../../../block/positions/full/marketConfirmDialog");
const _types = require("@orderly.network/types");
const _utils = require("@orderly.network/utils");
const _spinner = require("../../../spinner");
const MyTradingView = ({ symbol, tradingViewConfig })=>{
    const [open, setOpen] = (0, _react.useState)(false);
    const [side, setSide] = (0, _react.useState)(_types.OrderSide.BUY);
    const { helper, onSubmit, submitting } = (0, _hooks.useOrderEntry)(symbol, side, true);
    const symbolInfo = (0, _hooks.useSymbolsInfo)()[symbol];
    const [orderData, setOrderData] = (0, _react.useState)();
    const closePositionConfirmCallback = (data)=>{
        const side = new _utils.Decimal(data.balance).greaterThan(0) ? _types.OrderSide.SELL : _types.OrderSide.BUY;
        const order = {
            //   order_price: undefined,
            order_quantity: data.balance,
            symbol: symbol,
            order_type: _types.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.toast.error(error);
                return;
            }
            _toast.toast.error(error.message);
        });
    };
    const onClose = ()=>{
        setOpen(false);
    };
    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
        style: {
            position: "relative",
            width: "100%",
            height: "100%"
        },
        children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_symbolProvider.SymbolProvider, {
            symbol: symbol,
            children: [
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_tradingview.TradingView, {
                    symbol: symbol,
                    libraryPath: tradingViewConfig?.library_path,
                    tradingViewScriptSrc: tradingViewConfig?.scriptSRC,
                    tradingViewCustomCssUrl: tradingViewConfig?.customCssUrl,
                    overrides: tradingViewConfig?.overrides,
                    studiesOverrides: tradingViewConfig?.studiesOverrides,
                    closePositionConfirmCallback: closePositionConfirmCallback,
                    onToast: _toast.toast,
                    loadingElement: /*#__PURE__*/ (0, _jsxruntime.jsx)(_spinner.Spinner, {})
                }),
                /*#__PURE__*/ (0, _jsxruntime.jsxs)(_popover.Popover, {
                    onOpenChange: setOpen,
                    open: open,
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverTrigger, {
                            asChild: true,
                            children: /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                style: {
                                    position: "absolute",
                                    width: "0",
                                    height: "0",
                                    left: "50%",
                                    top: "50%"
                                }
                            })
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverContent, {
                            align: "start",
                            side: "top",
                            className: "orderly-w-[340px]",
                            children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_marketConfirmDialog.CloseBaseConfirm, {
                                base: symbolInfo("base"),
                                quantity: orderData?.order_quantity ?? 0,
                                onClose: onClose,
                                onConfirm: onConfirm,
                                submitting: submitting
                            })
                        })
                    ]
                })
            ]
        })
    });
};

//# sourceMappingURL=myTradingview.js.map