"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "TPSLEditor", {
    enumerable: true,
    get: function() {
        return TPSLEditor;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _tpAndslForm = require("../../block/tp_sl/tpAndslForm");
const _hooks = require("@orderly.network/hooks");
const _types = require("@orderly.network/types");
const _toast = require("../../toast");
const _algoOrderConfirmView = require("./algoOrderConfirmView");
const _simpleDialog = require("../../dialog/simpleDialog");
const TPSLEditor = (props)=>{
    const { symbol, maxQty, quoteDp } = props;
    const [orderEntity, setOrderEntity] = (0, _react.useState)();
    const [open, setOpen] = (0, _react.useState)(false);
    const [order, { submit, setValue, validate, errors }] = (0, _hooks.useStopOrder)({
        symbol,
        position_qty: props.position.position_qty,
        average_open_price: props.position.average_open_price
    }, {
        defaultOrder: props.order
    });
    const [showError, setShowError] = (0, _react.useState)(false);
    const isTablet = (0, _hooks.useMediaQuery)(_types.MEDIA_TABLET);
    const [needConfirm, setNeedConfirm] = (0, _hooks.useLocalStorage)("orderly_order_confirm", true);
    const [mode, setMode] = (0, _react.useState)("delete");
    const title = (0, _react.useMemo)(()=>{
        if (mode === "delete") {
            return "Cancel order";
        } else if (mode === "create") {
            return "Confirm Order";
        } else {
            return "Edit Order";
        }
    }, [
        mode
    ]);
    const onSubmit = ()=>{
        return submit().then(()=>{
            setOpen(false);
            props.onSuccess?.();
        }, (error)=>{
            _toast.toast.error(error.message);
        });
    };
    (0, _react.useEffect)(()=>{
        let type;
        if (order.quantity === maxQty) {
            type = _types.AlgoOrderRootType.POSITIONAL_TP_SL;
        } else {
            type = _types.AlgoOrderRootType.TP_SL;
        }
        props.onTypeChange?.(type);
    }, [
        order.quantity,
        maxQty
    ]);
    const onConfirm = ()=>{
        // check the order status, if it is a new order, then create it, otherwise update it,
        // if the quantity is not equal to the position quantity, then create new TP/SL order
        return Promise.resolve().then(()=>{
            return validate();
        }).then((orderEntity)=>{
            // console.log("order", order, symbol, orderEntity);
            if (needConfirm) {
                const isRemove = !order.tp_trigger_price && !order.sl_trigger_price;
                if (isRemove) {
                    setMode("delete");
                } else if (!!order.algo_order_id && order.algo_type === orderEntity.algo_type) {
                    setMode("update");
                } else {
                    setMode("create");
                }
                setOrderEntity(orderEntity);
                setOpen(true);
                return false;
            }
            return true;
        }, (error)=>{
            // console.log(error);
            setShowError(true);
            return false;
        }).then((pass)=>{
            if (pass) {
                return onSubmit();
            }
        });
    };
    (0, _react.useEffect)(()=>{
        return ()=>{
            setOpen(false);
        };
    }, []);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, {
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_tpAndslForm.TPSLForm, {
                className: open ? "orderly-hidden" : "",
                maxQty: maxQty,
                onChange: setValue,
                symbol: symbol,
                onSubmit: onConfirm,
                onCancel: props.onCancel,
                order: order,
                canModifyQty: props.canModifyQty,
                isEditing: props.isEditing,
                oldOrder: props.order,
                errors: showError ? errors : null
            }),
            mode === "delete" ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_simpleDialog.SimpleDialog, {
                open: open,
                onOpenChange: setOpen,
                title: title,
                maxWidth: props.isEditing ? "xs" : "sm",
                onCancel: ()=>setOpen(false),
                onOk: ()=>onSubmit(),
                closable: true,
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                    className: "orderly-p-5",
                    children: `Are you sure you want to cancel this ${order.algo_type === _types.AlgoOrderRootType.POSITIONAL_TP_SL ? "Position TP/SL" : "TP/SL"} order?`
                })
            }) : /*#__PURE__*/ (0, _jsxruntime.jsx)(_simpleDialog.SimpleDialog, {
                open: open,
                onOpenChange: setOpen,
                title: title,
                maxWidth: props.isEditing ? "xs" : "sm",
                // onCancel={() => setOpen(false)}
                // onOk={() => onSubmit()}
                closable: true,
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_algoOrderConfirmView.AlgoOrderConfirmView, {
                    //@ts-ignore
                    order: {
                        ...orderEntity,
                        // ...order,
                        symbol,
                        algo_order_id: order.algo_order_id
                    },
                    isEditing: props.isEditing,
                    tp_trigger_price: order.tp_trigger_price,
                    sl_trigger_price: order.sl_trigger_price,
                    symbol: symbol,
                    oldOrder: props.order,
                    onCancel: ()=>setOpen(false),
                    onConfirm: ()=>onSubmit(),
                    quoteDp: quoteDp
                })
            })
        ]
    });
};

//# sourceMappingURL=tp_sl_editor.js.map