"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, {
    PositionsRowContext: function() {
        return PositionsRowContext;
    },
    PositionsRowProvider: function() {
        return PositionsRowProvider;
    },
    usePositionsRowContext: function() {
        return usePositionsRowContext;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _types = require("@orderly.network/types");
const _react = require("react");
const _hooks = require("@orderly.network/hooks");
const PositionsRowContext = /*#__PURE__*/ (0, _react.createContext)({});
const usePositionsRowContext = ()=>{
    return (0, _react.useContext)(PositionsRowContext);
};
const PositionsRowProvider = (props)=>{
    const [quantity, setQuantity] = (0, _react.useState)(Math.abs(props.position.position_qty).toString());
    (0, _react.useEffect)(()=>{
        setQuantity(Math.abs(props.position.position_qty).toString());
    }, [
        props.position.position_qty
    ]);
    const [price, setPrice] = (0, _react.useState)("");
    const [side, setSide] = (0, _react.useState)(props.position.position_qty > 0 ? _types.OrderSide.SELL : _types.OrderSide.BUY);
    const [type, setType] = (0, _react.useState)(_types.OrderType.MARKET);
    const { helper, onSubmit, submitting } = (0, _hooks.useOrderEntry)(props.position?.symbol, side, true);
    const updateOrderType = (type, price)=>{
        setType(type);
        if (type === _types.OrderType.LIMIT) {
            if (!price) {
                throw new Error("price is required");
            }
            setPrice(price);
        } else {
            setPrice("");
        }
    };
    const closeOrderData = (0, _react.useMemo)(()=>{
        const { position } = props;
        if (!position) return null;
        const data = {
            //   order_price: undefined,
            order_quantity: quantity,
            symbol: props.position.symbol,
            order_type: type,
            side,
            reduce_only: true
        };
        if (type === _types.OrderType.LIMIT) {
            data.order_price = price;
        }
        return data;
    }, [
        props.position,
        price,
        type,
        quantity
    ]);
    const onUpdateQuantity = (value)=>{
        const newValues = helper.calculate({}, "order_quantity", value);
        setQuantity(newValues["order_quantity"]);
    };
    const onUpdatePrice = (value)=>{
        const newValues = helper.calculate({}, "order_price", value);
        setPrice(newValues["order_price"]);
    };
    const postOrder = ()=>{
        return onSubmit(closeOrderData);
    };
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(PositionsRowContext.Provider, {
        value: {
            quantity,
            price,
            type,
            side,
            position: props.position,
            updatePriceChange: onUpdatePrice,
            updateQuantity: onUpdateQuantity,
            updateOrderType,
            tpslOrder: props.position.algo_order,
            onSubmit: postOrder,
            submitting,
            closeOrderData
        },
        children: props.children
    });
};

//# sourceMappingURL=positionRowContext.js.map