"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PositionPane", {
    enumerable: true,
    get: function() {
        return PositionPane;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _positions = require("../../../../../../block/positions");
const _hooks = require("@orderly.network/hooks");
const _modal = require("../../../../../../modal");
const _types = require("@orderly.network/types");
const _markPriceConfirm = require("../../../../../../block/positions/sections/markPriceConfirm");
const _closeDialog = require("../../../../../../block/positions/sections/closeDialog");
const _page = require("../../../../../../page");
const _toast = require("../../../../../../toast");
const _tpslSheet = require("../../../../../../block/tp_sl/tpslSheet");
const _tpslSheetTitle = require("./tpslSheetTitle");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const PositionPane = ()=>{
    const context = (0, _react.useContext)(_page.TradingPageContext);
    //
    const [showAllSymbol, setShowAllSymbol] = (0, _hooks.useSessionStorage)("showAllSymbol_position", true);
    const [symbol, setSymbol] = _react.default.useState(()=>showAllSymbol ? "" : context.symbol);
    const onShowAllSymbolChange = (isAll)=>{
        setSymbol(isAll ? "" : context.symbol);
        setShowAllSymbol(isAll);
    };
    const [data, info, { loading }] = (0, _hooks.usePositionStream)(symbol);
    const { state } = (0, _hooks.useAccount)();
    const [postOrder] = (0, _hooks.useMutation)("/v1/order");
    const onLimitClose = (0, _react.useCallback)(async (position)=>{
        return _modal.modal.sheet({
            title: "Limit Close",
            content: /*#__PURE__*/ (0, _jsxruntime.jsx)(_closeDialog.PositionLimitCloseDialog, {
                positions: position,
                side: position.position_qty > 0 ? _types.OrderSide.SELL : _types.OrderSide.BUY
            })
        }).then(()=>{}).catch((e)=>{});
    }, []);
    const onTPSLOrder = (0, _react.useCallback)(async (position, order)=>{
        return _modal.modal.sheet({
            title: /*#__PURE__*/ (0, _jsxruntime.jsx)(_tpslSheetTitle.TPSLSheetTitle, {}),
            content: /*#__PURE__*/ (0, _jsxruntime.jsx)(_tpslSheet.TPSLOrderEditorSheet, {
                position: position,
                order: order
            })
        }).then(()=>{});
    }, []);
    const onMarketClose = (0, _react.useCallback)(async (position)=>{
        return _modal.modal.confirm({
            title: "Market Close",
            content: /*#__PURE__*/ (0, _jsxruntime.jsx)(_markPriceConfirm.MarkPriceConfirm, {
                position: position
            }),
            onCancel: ()=>{
                return Promise.reject();
            },
            onOk: ()=>{
                return postOrder({
                    symbol: position.symbol,
                    side: position.position_qty > 0 ? _types.OrderSide.SELL : _types.OrderSide.BUY,
                    order_type: _types.OrderType.MARKET,
                    order_quantity: Math.abs(position.position_qty),
                    reduce_only: true
                }).then((res)=>{
                // toast.success("success");
                }).catch((err)=>{
                    //
                    _toast.toast.error(err.message);
                });
            }
        }).catch(()=>{});
    }, []);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_positions.PositionsView, {
        dataSource: state.status < _types.AccountStatusEnum.EnableTrading ? [] : data.rows,
        aggregated: data.aggregated,
        isLoading: loading,
        onLimitClose: onLimitClose,
        onMarketClose: onMarketClose,
        onTPSLOrder: onTPSLOrder,
        showAllSymbol: showAllSymbol,
        onShowAllSymbolChange: onShowAllSymbolChange,
        onSymbolChange: context.onSymbolChange
    });
};

//# sourceMappingURL=index.js.map