"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "OrderCell", {
    enumerable: true,
    get: function() {
        return OrderCell;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _button = /*#__PURE__*/ _interop_require_default(require("../../button"));
const _tag = require("../../tag");
const _react = require("react");
const _text = require("../../text");
const _orderListContext = require("./shared/orderListContext");
const _provider = require("../../provider");
const _types = require("@orderly.network/types");
const _tpslOrderRowContext = require("../tp_sl/tpslOrderRowContext");
const _orderTPSLTag = require("./orderTPSLTag");
const _cellregular = require("./cell.regular");
const _celltpsl = require("./cell.tpsl");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const OrderCell = (props)=>{
    const { order } = props;
    const [loading, setLoading] = (0, _react.useState)(false);
    const { onCancelOrder, onEditOrder } = (0, _react.useContext)(_orderListContext.OrderListContext);
    const { quote, quote_dp, base, base_dp } = (0, _react.useContext)(_provider.SymbolContext);
    const { position } = (0, _tpslOrderRowContext.useTPSLOrderRowContext)();
    const typeTag = (0, _react.useMemo)(()=>{
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_tag.Tag, {
            color: order.side === "BUY" ? "buy" : "sell",
            children: order.side === "BUY" ? "Buy" : "Sell"
        });
    }, [
        order
    ]);
    const cancelOrder = (0, _react.useCallback)(()=>{
        if (loading) return;
        setLoading(true);
        onCancelOrder(order).finally(()=>setLoading(false));
    }, [
        loading,
        order
    ]);
    const onSymbol = ()=>{
        props.onSymbolChange?.({
            symbol: order.symbol
        });
        // go to the top of page
        window.scrollTo(0, 0);
    };
    const infoEle = (0, _react.useMemo)(()=>{
        if ("algo_type" in order && (order.algo_type === _types.AlgoOrderRootType.TP_SL || order.algo_type === _types.AlgoOrderRootType.POSITIONAL_TP_SL)) {
            return /*#__PURE__*/ (0, _jsxruntime.jsx)(_celltpsl.TPSLCell, {
                order: order,
                base_dp: base_dp,
                quote_dp: quote_dp
            });
        }
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_cellregular.RegularCell, {
            order: order,
            base_dp: base_dp,
            quote_dp: quote_dp
        });
    }, [
        order,
        base_dp,
        quote_dp
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: "orderly-px-4 orderly-py-3",
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-mb-1 orderly-flex orderly-items-end orderly-justify-between",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: "orderly-flex-col",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                className: "orderly-flex orderly-items-center orderly-gap-2 ",
                                children: [
                                    typeTag,
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                        className: "orderly-flex-1 orderly-text-2xs",
                                        onClick: onSymbol,
                                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                                            rule: "symbol",
                                            children: order.symbol
                                        })
                                    })
                                ]
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_orderTPSLTag.TPSLOrderTag, {
                                order: order
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-text-4xs orderly-text-base-contrast-36",
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                            rule: "date",
                            children: order.created_time
                        })
                    })
                ]
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-grid orderly-grid-cols-3 orderly-gap-2 orderly-py-1",
                children: infoEle
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-flex orderly-gap-3 orderly-text-4xs orderly-justify-end orderly-mt-2",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                        id: "orderly-pending-cell-edit-button",
                        variant: "outlined",
                        size: "small",
                        color: "tertiary",
                        className: "orderly-w-[120px] orderly-text-4xs",
                        onClick: ()=>onEditOrder(order, position),
                        children: "Edit"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                        id: "orderly-pending-cell-cancel-button",
                        variant: "outlined",
                        color: "tertiary",
                        size: "small",
                        className: "orderly-w-[120px] orderly-text-4xs",
                        loading: loading,
                        onClick: cancelOrder,
                        children: "Cancel"
                    })
                ]
            })
        ]
    });
};

//# sourceMappingURL=cell.js.map