"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, {
    TPSLOrderRowContext: function() {
        return TPSLOrderRowContext;
    },
    TPSLOrderRowProvider: function() {
        return TPSLOrderRowProvider;
    },
    useTPSLOrderRowContext: function() {
        return useTPSLOrderRowContext;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _hooks = require("@orderly.network/hooks");
const TPSLOrderRowContext = /*#__PURE__*/ (0, _react.createContext)({});
const useTPSLOrderRowContext = ()=>{
    return (0, _react.useContext)(TPSLOrderRowContext);
};
const TPSLOrderRowProvider = (props)=>{
    const [position, setPosition] = (0, _react.useState)();
    const [doDeleteOrder] = (0, _hooks.useMutation)("/v1/algo/order", "DELETE");
    const [doUpdateOrder] = (0, _hooks.useMutation)("/v1/algo/order", "PUT");
    const config = (0, _hooks.useSWRConfig)();
    const { state } = (0, _hooks.useAccount)();
    const positionKey = (0, _react.useMemo)(()=>{
        return (0, _hooks.unstable_serialize)(()=>[
                "/v1/positions",
                state.accountId
            ]);
    }, [
        state.accountId
    ]);
    const onCancelOrder = async (order)=>{
        return doDeleteOrder(null, {
            order_id: order.algo_order_id,
            symbol: order.symbol
        });
    };
    const onUpdateOrder = async (order, params)=>{
        ;
        return doUpdateOrder({
            order_id: order.algo_order_id,
            child_orders: order.child_orders.map((order)=>({
                    order_id: order.algo_order_id,
                    quantity: params.order_quantity
                }))
        });
    };
    const getRelatedPosition = (symbol)=>{
        const positions = config.cache.get(positionKey);
        return positions?.data.rows.find((p)=>p.symbol === symbol);
    };
    const { sl_trigger_price, tp_trigger_price } = (0, _react.useMemo)(()=>{
        if (!("algo_type" in props.order) || !Array.isArray(props.order.child_orders)) {
            return {};
        }
        return _hooks.utils.findTPSLFromOrder(props.order);
    }, [
        props.order
    ]);
    (0, _react.useEffect)(()=>{
        if ("algo_type" in props.order) {
            const position = getRelatedPosition(props.order.symbol);
            if (position) {
                setPosition(position);
            }
        }
    }, [
        props.order.symbol
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(TPSLOrderRowContext.Provider, {
        value: {
            order: props.order,
            sl_trigger_price,
            tp_trigger_price,
            onCancelOrder,
            onUpdateOrder,
            getRelatedPosition,
            position
        },
        children: props.children
    });
};

//# sourceMappingURL=tpslOrderRowContext.js.map