"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ClosePositionPane", {
    enumerable: true,
    get: function() {
        return ClosePositionPane;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _button = /*#__PURE__*/ _interop_require_default(require("../../../button"));
const _divider = require("../../../divider");
const _input = require("../../../input");
const _slider = require("../../../slider");
const _statistic = require("../../../statistic");
const _text = require("../../../text");
const _react = require("react");
const _limitConfirm = require("./limitConfirm");
const _modal = require("../../../modal");
const _hooks = require("@orderly.network/hooks");
const _reacthookform = require("react-hook-form");
const _types = require("@orderly.network/types");
const _toast = require("../../../toast");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const ClosePositionPane = (props)=>{
    const { position, side } = props;
    // const { hide, reject, resolve } = useModal();
    const { markPrice, maxQty, helper, onSubmit } = (0, _hooks.useOrderEntry)(position?.symbol, side, true);
    const { register, handleSubmit, control, getValues, setValue, formState: { errors, submitCount, isSubmitting } } = (0, _reacthookform.useForm)({
        values: {
            order_price: Math.abs(position?.mark_price),
            order_quantity: Math.abs(position?.position_qty),
            symbol: position?.symbol,
            order_type: _types.OrderType.LIMIT,
            side: side
        },
        resolver: async (values)=>{
            const errors = await helper.validator(values);
            return {
                values,
                errors
            };
        }
    });
    (0, _react.useEffect)(()=>{
        // init order_price value
        if (getValues()?.order_price === undefined) {
            //@ts-ignore
            setValue("order_price", markPrice);
        }
    }, [
        markPrice
    ]);
    const symbolInfo = (0, _hooks.useSymbolsInfo)()[position?.symbol];
    //
    const base = (0, _react.useMemo)(()=>symbolInfo("base"), [
        symbolInfo
    ]);
    const quote = (0, _react.useMemo)(()=>symbolInfo("quote"), [
        symbolInfo
    ]);
    const quoteDp = (0, _react.useMemo)(()=>symbolInfo("quote_dp"), [
        symbolInfo
    ]);
    const typeText = (0, _react.useMemo)(()=>{
        if (side === _types.OrderSide.SELL) return /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
            type: "sell",
            children: "Limit Sell"
        });
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
            type: "buy",
            children: "Limit Buy"
        });
    }, [
        side
    ]);
    const onConfirm = (data)=>{
        return _modal.modal.confirm({
            title: "Limit Close",
            onCancel: ()=>{
                return Promise.reject();
            },
            content: /*#__PURE__*/ (0, _jsxruntime.jsx)(_limitConfirm.LimitConfirm, {
                order: data,
                base: base,
                quote: quote,
                side: side
            })
        });
    };
    const onFormSubmit = (data)=>{
        return onConfirm(data).then(()=>{
            return onSubmit({
                ...data,
                reduce_only: true
            }).then((res)=>{
                //
                if (res.success) {
                // toast.success("successfully");
                }
                props.onClose(res);
            }, (error)=>{
                _toast.toast.error(error.message);
            });
        }, ()=>{});
    };
    const onFieldChange = (name, value)=>{
        const newValues = helper.calculate(getValues(), name, value);
        if (name === "order_price") {
            // @ts-ignore
            setValue("order_price", newValues.order_price, {
                shouldValidate: submitCount > 0
            });
        }
        // @ts-ignore
        setValue("order_quantity", newValues.order_quantity, {
            shouldValidate: submitCount > 0
        });
    };
    if (!position) return null;
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, {
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: "orderly-pb-3 orderly-pt-5 orderly-text-xs",
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                    rule: "symbol",
                    children: position.symbol
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                className: "orderly-grid orderly-grid-cols-2",
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_statistic.Statistic, {
                        label: "Order type",
                        value: typeText,
                        valueClassName: "orderly-text-2xs",
                        labelClassName: "orderly-text-4xs orderly-text-base-contrast-36"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_statistic.Statistic, {
                        label: "Last price",
                        value: markPrice,
                        precision: quoteDp,
                        rule: "price",
                        labelClassName: "orderly-text-4xs orderly-text-base-contrast-36",
                        valueClassName: "orderly-text-2xs"
                    })
                ]
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_divider.Divider, {
                className: "orderly-py-5"
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("form", {
                onSubmit: handleSubmit(onFormSubmit),
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: "orderly-flex orderly-flex-col orderly-gap-5",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_reacthookform.Controller, {
                                name: "order_price",
                                control: control,
                                render: ({ field })=>{
                                    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_input.Input, {
                                        prefix: "Price",
                                        suffix: quote,
                                        type: "text",
                                        inputMode: "decimal",
                                        helpText: errors.order_price?.message,
                                        error: !!errors.order_price,
                                        className: "orderly-text-right orderly-text-3xs",
                                        containerClassName: "orderly-bg-base-500 orderly-rounded-borderRadius",
                                        value: field.value,
                                        onChange: (e)=>{
                                            // field.onChange(e.target.value)
                                            onFieldChange("order_price", e.target.value);
                                        }
                                    });
                                }
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_reacthookform.Controller, {
                                name: "order_quantity",
                                control: control,
                                render: ({ field })=>{
                                    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_input.Input, {
                                        prefix: "Quantity",
                                        suffix: base,
                                        type: "text",
                                        inputMode: "decimal",
                                        containerClassName: "orderly-bg-base-500 orderly-rounded-borderRadius",
                                        helpText: errors.order_quantity?.message,
                                        error: !!errors.order_quantity,
                                        className: "orderly-text-right orderly-text-3xs",
                                        value: field.value,
                                        onChange: (e)=>{
                                            // field.onChange(e.target.value)
                                            onFieldChange("order_quantity", e.target.value);
                                        }
                                    });
                                }
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-py-5",
                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_reacthookform.Controller, {
                            name: "order_quantity",
                            control: control,
                            render: ({ field })=>{
                                //
                                return /*#__PURE__*/ (0, _jsxruntime.jsx)(_slider.Slider, {
                                    step: symbolInfo("base_tick"),
                                    min: 0,
                                    max: Math.abs(position.position_qty),
                                    color: "buy",
                                    markCount: 4,
                                    value: [
                                        Number(field.value ?? 0)
                                    ],
                                    onValueChange: (value)=>{
                                        // if (typeof value[0] !== "undefined") {
                                        //   field.onChange(value[0]);
                                        // }
                                        setValue("order_quantity", value[0]);
                                    }
                                });
                            }
                        })
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                        className: "orderly-grid orderly-grid-cols-2 orderly-text-xs orderly-gap-3 orderly-py-5",
                        children: [
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                variant: "contained",
                                color: "tertiary",
                                fullWidth: true,
                                type: "button",
                                onClick: ()=>{
                                    props.onCancel?.();
                                },
                                children: "Cancel"
                            }),
                            /*#__PURE__*/ (0, _jsxruntime.jsx)(_button.default, {
                                fullWidth: true,
                                type: "submit",
                                loading: isSubmitting,
                                children: "Confirm"
                            })
                        ]
                    })
                ]
            })
        ]
    });
};

//# sourceMappingURL=closeForm.js.map