"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "default", {
    enumerable: true,
    get: function() {
        return DisplayControl;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _kchart = require("../../../../../../icon/icons/kchart");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _css = require("../../../../../../utils/css");
const _checkSmall = require("../../../../../../icon/icons/checkSmall");
const _expland = require("../../../../../../icon/icons/expland");
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 DisplayControlMap = [
    [
        {
            label: "Position",
            id: "position"
        },
        {
            label: "Limit Orders",
            id: "limitOrders"
        }
    ],
    [
        {
            label: "Stop orders",
            id: "stopOrders"
        },
        {
            label: "TP/SL",
            id: "tpsl"
        }
    ],
    [
        {
            label: "Position TP/SL",
            id: "positionTpsl"
        },
        {
            label: "Buy/Sell",
            id: "buySell"
        }
    ]
];
function DisplayControl({ displayControlState, changeDisplayControlState }) {
    const [open, setOpen] = (0, _react.useState)(false);
    const containerRef = _react.default.useRef(null);
    (0, _react.useEffect)(()=>{
        const onTouchStart = (event)=>{
            if (containerRef.current === event.target || containerRef.current?.contains(event.target)) {
                return;
            }
            setOpen(false);
        //   document.removeEventListener("click", onClick);
        };
        const onClick = (event)=>{
            if (containerRef.current === event.target || containerRef.current?.contains(event.target)) {
                return;
            }
            //   event.stopPropagation();
            setOpen(false);
        };
        if (open) {
            //   const onTouchMove = () => {};
            setTimeout(()=>{
                document.addEventListener("touchstart", onTouchStart, {
                    capture: true
                });
                document.addEventListener("click", onClick, {
                    capture: true
                });
            }, 100);
            return ()=>{
                //
                document.removeEventListener("touchstart", onTouchStart);
                document.removeEventListener("click", onClick);
            };
        } else {
            document.removeEventListener("touchstart", onTouchStart);
            document.removeEventListener("click", onClick);
        }
    }, [
        open
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        ref: containerRef,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: (0, _css.cn)("orderly-flex orderly-items-center orderly-justify-center orderly-w-[44px] orderly-h-[36px] orderly-rounded orderly-text-base-contrast-54", open && "orderly-text-base-contrast"),
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)("button", {
                    className: "orderly-flex orderly-items-center ",
                    onClick: ()=>setOpen((open)=>!open),
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_kchart.KChartIcon, {
                            size: 20,
                            fill: "currentColor"
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_expland.ExpandIcon, {
                            size: 16,
                            className: (0, _css.cn)("orderly-transition-transform", open && "orderly-rotate-180")
                        })
                    ]
                })
            }),
            open && /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                "data-state": open ? "open" : "closed",
                className: (0, _css.cn)("orderly-z-50 orderly-min-w-[8rem] orderly-overflow-hidden orderly-rounded orderly-bg-base-800 orderly-px-4 orderly-py-3 orderly-text-popover-foreground orderly-shadow-md orderly-absolute orderly-left-0 orderly-top-full orderly-w-full orderly-space-y-1", "data-[state=open]:orderly-animate-in data-[state=closed]:orderly-animate-out data-[state=closed]:orderly-fade-out-0 data-[state=open]:orderly-fade-in-0 data-[state=closed]:orderly-zoom-out-95 data-[state=open]:orderly-zoom-in-95 data-[side=bottom]:orderly-slide-in-from-top-2 data-[side=left]:orderly-slide-in-from-right-2 data-[side=right]:orderly-slide-in-from-left-2 data-[side=top]:orderly-slide-in-from-bottom-2"),
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-text-base-contrast-54 orderly-text-3xs orderly-pb-3",
                        children: "Display settings"
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                        className: "orderly-flex orderly-flex-col orderly-gap-3",
                        children: DisplayControlMap.map((row, index)=>/*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                className: "orderly-flex orderly-flex-row orderly-gap-3",
                                children: row.map((item)=>item.id !== "buySell" ? /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                        className: (0, _css.cn)("orderly-text-3xs orderly-relative orderly-flex orderly-items-center orderly-justify-center orderly-w-[50%] orderly-bg-base-600 orderly-h-8 orderly-text-base-contrast-36 orderly-rounded-md", displayControlState[item.id] && "orderly-text-base-contrast orderly-border orderly-border-base-contrast"),
                                        onClick: ()=>{
                                            changeDisplayControlState({
                                                ...displayControlState,
                                                [item.id]: !displayControlState[item.id]
                                            });
                                        },
                                        children: [
                                            displayControlState[item.id] && /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                                className: "orderly-absolute -orderly-top-[1px] -orderly-right-[1px]",
                                                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_checkSmall.CheckSmallIcon, {
                                                    size: 12
                                                })
                                            }),
                                            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                                children: item.label
                                            })
                                        ]
                                    }, item.id) : /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                                        className: "orderly-w-[50%]"
                                    }, item.id))
                            }, index))
                    })
                ]
            })
        ]
    });
}

//# sourceMappingURL=displayControl.js.map