"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    OrderListContext: function() {
        return OrderListContext;
    },
    OrderListProvider: function() {
        return OrderListProvider;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _modal = require("../../../modal");
const _react = require("react");
const _editorSheet = require("../dialog/editor/editorSheet");
const _hooks = require("@orderly.network/hooks");
const OrderListContext = /*#__PURE__*/ (0, _react.createContext)({});
const OrderListProvider = (props)=>{
    const { cancelOrder, editOrder, cancelAlgoOrder, editAlgoOrder } = props;
    const symbolInfo = (0, _hooks.useSymbolsInfo)();
    const onCancelOrder = (0, _react.useCallback)(async (order)=>{
        if (order.algo_order_id !== undefined) {
            if ("root_algo_order_id" in order && order.root_algo_order_id !== order.algo_order_id) {
                return cancelAlgoOrder(order.root_algo_order_id, order.symbol);
            }
            return cancelAlgoOrder(order.algo_order_id, order.symbol).then(()=>{});
        }
        // @ts-ignore
        return cancelOrder(order.order_id, order.symbol).then(()=>{
        // toast.success("Order canceled successfully");
        });
    }, []);
    const onEditOrder = (0, _react.useCallback)(async (order, position)=>{
        // @ts-ignore
        let isHidden = order.visible_quantity !== undefined ? order.visible_quantity === 0 : order.visible !== undefined ? order.visible === 0 : false;
        const orderEntry = await _modal.modal.sheet({
            title: "Edit Order",
            contentClassName: "orderly-edit-order-sheet-content",
            content: /*#__PURE__*/ (0, _jsxruntime.jsx)(_editorSheet.OrderEditFormSheet, {
                order: order,
                position: position,
                editOrder: (value)=>{
                    /// check order has order_tag, if exits add order_tag to request body
                    if (typeof order.order_tag !== undefined && order.reduce_only !== true) {
                        value = {
                            ...value,
                            order_tag: order.order_tag
                        };
                    }
                    if (order.algo_order_id !== undefined) {
                        return editAlgoOrder(order.algo_order_id.toString(), {
                            ...value
                        });
                    }
                    return editOrder(order.order_id.toString(), {
                        ...value,
                        ...isHidden ? {
                            visible_quantity: 0
                        } : {}
                    });
                }
            })
        });
    }, []);
    const checkMinNotional = (0, _react.useCallback)((symbol, price, qty)=>{
        const { min_notional } = symbolInfo[symbol]();
        return (0, _hooks.checkNotional)(price, qty, min_notional);
    }, [
        symbolInfo
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(OrderListContext.Provider, {
        value: {
            onCancelOrder,
            onEditOrder,
            editOrder,
            editAlgoOrder,
            checkMinNotional
        },
        children: props.children
    });
};

//# sourceMappingURL=orderListContext.js.map