"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "MyOrderEntry", {
    enumerable: true,
    get: function() {
        return MyOrderEntry;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _orderEntry = require("../../../../../block/orderEntry");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _hooks = require("@orderly.network/hooks");
const _types = require("@orderly.network/types");
const _assetsProvider = require("../../../../../provider/assetsProvider");
const _entrySessionStorageUtils = require("./entrySessionStorageUtils");
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 MyOrderEntry = (props)=>{
    const { symbol } = props;
    const { onDeposit } = (0, _react.useContext)(_assetsProvider.AssetsContext);
    const { state } = (0, _hooks.useAccount)();
    const ee = (0, _hooks.useEventEmitter)();
    const containerRef = (0, _react.useRef)(null);
    (0, _react.useEffect)(()=>{
        const observer = new ResizeObserver(function(entries) {
            if (Array.isArray(entries) && entries[0]) {
                ee.emit("dom:orderEntry:resize", {
                    height: entries[0].contentBoxSize[0].blockSize,
                    width: entries[0].contentBoxSize[0].inlineSize
                });
            }
        });
        observer.observe(containerRef.current);
        return ()=>{
            //
            if (containerRef.current) {
                observer.unobserve(containerRef.current);
            }
        };
    }, []);
    /// local storage hidden check box
    const [visibleQuantity, setVisibleQuantity] = (0, _hooks.useLocalStorage)("visible_quantity_key", 0);
    /// session storage
    const sessionData = (0, _entrySessionStorageUtils.getEntrySessionStorageInfo)();
    const orderType = sessionData.orderType;
    const reduceOnly = sessionData.reduceOnly;
    const orderSide = sessionData.orderSide;
    const orderTypeExt = sessionData.orderTypeExt;
    const sessionOrderEntry = sessionData.sessionOrderEntry;
    const [order, setOrder] = (0, _react.useState)({
        reduce_only: reduceOnly,
        side: orderSide,
        order_type: orderType,
        isStopOrder: orderType === _types.OrderType.STOP_LIMIT || orderType === _types.OrderType.STOP_MARKET,
        symbol,
        visible_quantity: visibleQuantity,
        order_type_ext: orderType === _types.OrderType.LIMIT ? orderTypeExt : undefined,
        // timestamp: Date.now(),
        ...sessionOrderEntry
    });
    // const [reduceOnly, setReduceOnly] = useState(false);
    const formState = (0, _hooks.useOrderEntry)(order, {
        watchOrderbook: true
    });
    (0, _react.useEffect)(()=>{
        setOrder((order)=>({
                ...order,
                order_price: "",
                order_quantity: "",
                trigger_price: "",
                symbol
            }));
        (0, _entrySessionStorageUtils.clearOrderEntrySessionData)();
    }, [
        symbol
    ]);
    (0, _react.useEffect)(()=>{
        // console.log(
        //   "=======>>>>>>>>formState.formattedOrder",
        //   formState.formattedOrder.total,
        //   formState.formattedOrder.order_quantity
        // );
        order.order_quantity = formState.formattedOrder.order_quantity;
        order.total = formState.formattedOrder.total;
    }, [
        formState.formattedOrder.total,
        formState.formattedOrder.order_quantity
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
        id: "orderly-order-entry",
        className: "orderly-pl-1",
        ref: containerRef,
        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_orderEntry.OrderEntry, {
            ...formState,
            showConfirm: true,
            // side={side}
            symbol: symbol,
            onFieldChange: (field, value)=>{
                ;
                /// save to local storage and refresh ui
                if (field === "visible_quantity") {
                    setVisibleQuantity(value);
                }
                /// save to session storage and not refresh ui
                (0, _entrySessionStorageUtils.setEntrySessionStorage)(field, value);
                // if (field === "reduce_only") {
                //   setOrder((order) => ({
                //     ...order,
                //     order_quantity: "",
                //     order_price: "",
                //     [field]: value,
                //     // timestamp: Date.now(),
                //   }));
                // } else {
                setOrder((order)=>({
                        ...order,
                        [field]: value
                    }));
            // }
            },
            setValues: (values)=>{
                setOrder((order)=>({
                        ...order,
                        ...values
                    }));
            },
            // onReduceOnlyChange={setReduceOnly}
            disabled: state.status < _types.AccountStatusEnum.EnableTrading,
            onDeposit: onDeposit
        })
    });
};

//# sourceMappingURL=index.js.map