import { jsx as _jsx } from "react/jsx-runtime";
import { modal } from "../../../modal";
import { createContext, useCallback } from "react";
import { OrderEditFormSheet } from "../dialog/editor/editorSheet";
import { checkNotional, useSymbolsInfo } from "@orderly.network/hooks";
export const OrderListContext = /*#__PURE__*/ createContext({});
export const OrderListProvider = (props)=>{
    const { cancelOrder, editOrder, cancelAlgoOrder, editAlgoOrder } = props;
    const symbolInfo = useSymbolsInfo();
    const onCancelOrder = useCallback(async (order)=>{
        if (order.algo_order_id !== undefined) {
            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 = useCallback(async (order)=>{
        // @ts-ignore
        let isHidden = order.visible_quantity !== undefined ? order.visible_quantity === 0 : order.visible !== undefined ? order.visible === 0 : false;
        const orderEntry = await modal.sheet({
            title: "Edit Order",
            contentClassName: "orderly-edit-order-sheet-content",
            content: /*#__PURE__*/ _jsx(OrderEditFormSheet, {
                order: order,
                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 = useCallback((symbol, price, qty)=>{
        const { min_notional } = symbolInfo[symbol]();
        return checkNotional(price, qty, min_notional);
    }, [
        symbolInfo
    ]);
    return /*#__PURE__*/ _jsx(OrderListContext.Provider, {
        value: {
            onCancelOrder,
            onEditOrder,
            editOrder,
            editAlgoOrder,
            checkMinNotional
        },
        children: props.children
    });
};

//# sourceMappingURL=orderListContext.js.map