"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    MSelect: function() {
        return MSelect;
    },
    SelectMenuItem: function() {
        return SelectMenuItem;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _select = require("./select");
const _css = require("../utils/css");
const _icon = require("../icon");
const _classvarianceauthority = require("class-variance-authority");
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 MSelect = ({ className, size, disabled, color, fullWidth, ...props })=>{
    const [open, setOpen] = (0, _react.useState)(false);
    const label = (0, _react.useMemo)(()=>{
        if (typeof props.value !== "undefined") {
            const activeItem = props.options?.find((item)=>item.value === props.value);
            if (activeItem) return activeItem.label;
        }
        return props.value || props.label || props.placeholder;
    }, [
        props.value
    ]);
    const options = (0, _react.useMemo)(()=>{
        return props.options || [];
    }, [
        props
    ]);
    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,
                    once: true
                });
                document.addEventListener("click", onClick, {
                    capture: true,
                    once: 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", {
        className: "orderly-relative",
        ref: containerRef,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsxs)("button", {
                type: "button",
                disabled: Boolean(disabled),
                onClick: ()=>setOpen((open)=>!open),
                className: (0, _css.cn)("orderly-select", "orderly-flex orderly-flex-row orderly-items-center orderly-rounded orderly-space-x-1 orderly-text-3xs desktop:orderly-text-xs", (0, _select.selectVariants)({
                    size,
                    disabled: disabled || options.length === 0,
                    className,
                    color,
                    fullWidth
                }), open && "orderly-bg-base-600"),
                children: [
                    /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                        className: "orderly-flex orderly-flex-1 orderly-text-inherit",
                        children: typeof label !== "undefined" && /*#__PURE__*/ (0, _jsxruntime.jsx)(_jsxruntime.Fragment, {
                            children: label
                        })
                    }),
                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_icon.ArrowIcon, {
                        size: 12,
                        className: (0, _classvarianceauthority.cx)("orderly-transition-transform", open && "orderly-rotate-180")
                    })
                ]
            }),
            open && /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                "data-state": open ? "open" : "closed",
                className: (0, _css.cn)("orderly-select-menu", "orderly-z-50 orderly-min-w-[8rem] orderly-overflow-hidden orderly-rounded orderly-bg-base-600 orderly-p-1 orderly-text-popover-foreground orderly-shadow-md orderly-py-2 orderly-absolute orderly-left-0 orderly-top-full orderly-mt-1 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: props.options?.map((option, index)=>{
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)(SelectMenuItem, {
                        value: option.value,
                        className: (0, _css.cn)("orderly-text-base-contrast-54 hover:orderly-bg-base-700 orderly-rounded", option.value === props.value && (color === "buy" ? "orderly-text-trade-profit" : "orderly-text-trade-loss")),
                        onValueChange: (value)=>{
                            setOpen(false);
                            // @ts-ignore
                            props.onChange?.(value);
                        },
                        children: option.label
                    }, index);
                })
            })
        ]
    });
};
const SelectMenuItem = (props)=>{
    const { value, className, onValueChange } = props;
    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
        children: /*#__PURE__*/ (0, _jsxruntime.jsx)("button", {
            type: "button",
            className: (0, _css.cn)("orderly-block orderly-p-2 orderly-text-inherit orderly-w-full orderly-h-full orderly-text-left orderly-text-3xs  desktop:orderly-text-xs", className),
            onClick: ()=>{
                onValueChange?.(value);
            },
            children: props.children
        })
    });
};

//# sourceMappingURL=mSelect.js.map