"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Renew", {
    enumerable: true,
    get: function() {
        return Renew;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _hooks = require("@orderly.network/hooks");
const _button = /*#__PURE__*/ _interop_require_default(require("../../../button"));
const _popover = require("../../../popover");
const _orderConfirmView = require("../../../block/orderEntry/sections/orderConfirmView");
const _divider = require("../../../divider");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const Renew = (props)=>{
    const { record } = props;
    const [open, setOpen] = (0, _react.useState)(false);
    const [doCreateOrder, { data, error, reset, isMutating }] = (0, _hooks.useMutation)("/v1/order");
    const [loading, setLoading] = (0, _react.useState)(false);
    const brokerId = (0, _hooks.useConfig)("brokerId");
    const onSubmit = (0, _react.useCallback)(()=>{
        setLoading(false);
        setOpen(false);
        const data = {
            symbol: record.symbol,
            order_type: record.type,
            order_price: record.price,
            order_quantity: record.quantity,
            order_amount: record.amount,
            // visible_quantity: record.visible,
            side: record.side,
            // reduce_only: record.reduce_only,
            broker_id: brokerId
        };
        if (Number(record.visible_quantity) < Number(record.quantity)) {
            data.visible_quantity = 0;
        }
        if (typeof record.reduce_only !== "undefined") {
            data.reduce_only = record.reduce_only;
        }
        doCreateOrder(data);
    }, []);
    const ordetEntity = (0, _react.useMemo)(()=>{
        return {
            symbol: record.symbol,
            order_type: record.type,
            order_price: record.price,
            order_quantity: record.quantity,
            order_amount: record.amount,
            visible_quantity: record.visible,
            side: record.side,
            broker_id: brokerId
        };
    }, []);
    const base = (0, _react.useMemo)(()=>{
        return record.symbol.split("_")[1];
    }, [
        record.symbol
    ]);
    const quote = (0, _react.useMemo)(()=>{
        return record.symbol.split("_")[2];
    }, [
        record.symbol
    ]);
    // console.log("renew record", record, base, quote);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
        size: "small",
        variant: "outlined",
        color: "tertiary",
        loading: isMutating,
        disabled: isMutating,
        onClick: (event)=>{
            // setOpen(true);
            event.preventDefault();
            event.stopPropagation();
            onSubmit();
        },
        children: "Renew"
    });
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_popover.Popover, {
        open: open,
        onOpenChange: setOpen,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverTrigger, {
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                    size: "small",
                    variant: "outlined",
                    color: "tertiary",
                    onClick: ()=>{
                        setOpen(true);
                    },
                    children: "Renew"
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverContent, {
                align: "end",
                side: "top",
                className: "orderly-w-[340px] orderly-z-20",
                sideOffset: 5,
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                            className: "orderly-flex orderly-justify-start orderly-text-base-contrast orderly-text-lg orderly-pb-2",
                            children: "Renew order"
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_divider.Divider, {
                            className: "orderly-mb-2"
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_orderConfirmView.OrderConfirmView, {
                            // @ts-ignore
                            order: {
                                ...ordetEntity,
                                total: record.price * record.quantity
                            },
                            symbol: record.symbol,
                            base: base,
                            quote: quote
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_divider.Divider, {
                            className: "orderly-my-2"
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                            className: "orderly-grid orderly-grid-cols-2 sm:orderly-flex sm:orderly-flex-row sm:orderly-justify-end orderly-gap-3 orderly-mt-3",
                            children: [
                                /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                    className: "orderly-text-xs desktop:orderly-text-xs orderly-font-bold",
                                    type: "button",
                                    variant: "contained",
                                    color: "tertiary",
                                    disabled: loading,
                                    onClick: ()=>{
                                        setOpen(false);
                                    },
                                    fullWidth: true,
                                    children: "Cancel"
                                }, "cancel"),
                                /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                    className: "orderly-text-xs desktop:orderly-text-xs orderly-font-bold",
                                    type: "button",
                                    fullWidth: true,
                                    disabled: loading,
                                    onClick: ()=>{
                                        setLoading(true);
                                        onSubmit();
                                    },
                                    children: "OK"
                                }, "ok")
                            ]
                        })
                    ]
                })
            })
        ]
    });
};

//# sourceMappingURL=renew.js.map