"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "DesktopListBox", {
    enumerable: true,
    get: function() {
        return DesktopListBox;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _celldesktop = require("./cell.desktop");
const _orderContext = require("../orderContext");
const _types = require("../types");
const _reacttooltip = require("@radix-ui/react-tooltip");
const _provider = require("../../../provider");
const _text = require("../../../text");
const DesktopListBox = (props)=>{
    const { data, type } = props;
    const { depth } = (0, _react.useContext)(_orderContext.OrderBookContext);
    const { base, quote, base_dp, quote_dp } = (0, _react.useContext)(_provider.SymbolContext);
    const findMaxItem = (0, _react.useCallback)(()=>{
        if ((data?.length || 0) === 0) {
            return null;
        }
        if (type === _types.OrderBookCellType.ASK) {
            const index = data.findIndex((item)=>!Number.isNaN(item[0]));
            if (index != -1) {
                return data[index];
            }
            return null;
        } else {
            for(let index = data.length - 1; index >= 0; index--){
                const item = data[index];
                if (!Number.isNaN(item[0])) {
                    return item;
                }
            }
            return null;
        }
    }, [
        data,
        type
    ]);
    const priceDp = (0, _react.useMemo)(()=>{
        if (depth?.toString().includes(".")) {
            return depth.toString().split(".")[1].length;
        }
        return 0;
    }, [
        depth
    ]);
    const maxQty = (0, _react.useMemo)(()=>{
        return data.reduce((a, b)=>Math.max(a, b[1]), 0);
    }, [
        data
    ]);
    const [hoverIndex, setHoverIndex] = (0, _react.useState)(-1);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
        id: "orderly-order-book-list",
        className: "orderly-flex orderly-flex-col orderly-gap-[1px]",
        children: data.map((item, index)=>{
            return /*#__PURE__*/ (0, _jsxruntime.jsx)(Tip, {
                index: index,
                item: item,
                countQty: props.countQty,
                setHoverIndex: setHoverIndex,
                hoverIndex: hoverIndex,
                type: type,
                maxQty: maxQty,
                base: base,
                quote: quote,
                priceDp: priceDp,
                baseDp: base_dp,
                quoteDp: quote_dp,
                findMaxItem: findMaxItem
            }, index);
        })
    });
};
const Tip = (props)=>{
    const { index, item, setHoverIndex, type, maxQty, hoverIndex, base, quote, priceDp, baseDp, quoteDp } = props;
    const isHover = hoverIndex !== -1 ? type === _types.OrderBookCellType.ASK ? index >= hoverIndex : index <= hoverIndex : false;
    const [open, setOpen] = (0, _react.useState)(false);
    const calcHintInfo = (item)=>{
        if (item === null) {
            return {
                sumQty: 0,
                sumQtyAmount: 0,
                avgPrice: 0
            };
        }
        let totalInfo = {
            sumQty: 0,
            sumQtyAmount: 0
        };
        if (!Number.isNaN(item[2])) {
            totalInfo = {
                sumQty: item[2],
                sumQtyAmount: item[3]
            };
        }
        return {
            ...totalInfo,
            avgPrice: totalInfo.sumQtyAmount == 0 ? 0 : totalInfo.sumQtyAmount / totalInfo.sumQty
        };
    };
    let hintInfo = calcHintInfo(item);
    if (hintInfo.avgPrice === 0) {
        hintInfo = calcHintInfo(props.findMaxItem());
    }
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_reacttooltip.Tooltip, {
        open: open,
        onOpenChange: setOpen,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_reacttooltip.TooltipTrigger, {
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_celldesktop.DesktopOrderBookCell, {
                    background: "",
                    price: item[0],
                    quantity: item[1],
                    accumulated: item[2],
                    count: props.countQty,
                    type: props.type,
                    accumulatedAmount: item[3],
                    maxQty: maxQty,
                    isHover: isHover,
                    onMouseEnter: ()=>{
                        setHoverIndex(index);
                        setOpen(true);
                    },
                    onMouseLeave: ()=>{
                        setHoverIndex(-1);
                        setOpen(false);
                    }
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)(_reacttooltip.TooltipContent, {
                className: "orderly-max-w-[400px] orderly-w-full orderly-text-3xs orderly-shadow-md orderly-rounded orderly-p-3 orderly-bg-base-700 orderly-animate-in orderly-fade-in-0 orderly-zoom-in-95 data-[state=closed]:orderly-animate-out data-[state=closed]:orderly-fade-out-0 data-[state=closed]:orderly-zoom-out-95 data-[side=bottom]:orderly-slide-in-from-top-2 data-[side=left]:orderly-slide-in-from-right-2 data-[side=right]:orderly-slide-in-from-left-2 data-[side=top]:orderly-slide-in-from-bottom-2",
                align: "center",
                side: "left",
                sideOffset: 12,
                onPointerEnter: (e)=>e.preventDefault(),
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Row, {
                        title: "Avg. Price≈",
                        content: hintInfo.avgPrice,
                        contentDp: priceDp
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Row, {
                        title: `Sum (${base})`,
                        content: hintInfo.sumQty,
                        contentDp: baseDp
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Row, {
                        title: `Sum (${quote})`,
                        content: hintInfo.sumQtyAmount,
                        contentDp: quoteDp
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_reacttooltip.TooltipArrow, {
                        className: "orderly-fill-base-700"
                    })
                ]
            })
        ]
    });
};
const Row = (props)=>{
    const { title, content, contentDp } = props;
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: "orderly-flex orderly-flex-row orderly-justify-between orderly-gap-4",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-text-base-contrast-36",
                children: title
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-text-right",
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Numeral, {
                    precision: contentDp,
                    children: content
                })
            })
        ]
    });
};

//# sourceMappingURL=listBox.desktop.js.map