"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, {
    Select: function() {
        return Select;
    },
    selectVariants: function() {
        return selectVariants;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _classvarianceauthority = require("class-variance-authority");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _css = require("../utils/css");
const _icon = require("../icon");
const _dropdown = require("../dropdown/dropdown");
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 selectVariants = (0, _classvarianceauthority.cva)([
    "orderly-rounded orderly-transition-colors orderly-bg-base-600"
], {
    variants: {
        size: {
            small: "orderly-px-2 orderly-h-[28px]",
            default: "orderly-px-2 orderly-py-1 orderly-h-[40px]",
            large: "orderly-px-6 orderly-py-3"
        },
        fullWidth: {
            true: "orderly-w-full"
        },
        color: {
            // primary: "text-primary",
            default: "orderly-text-base-contract",
            buy: "orderly-text-trade-profit",
            sell: "orderly-text-trade-loss"
        },
        disabled: {
            true: "orderly-opacity-50 orderly-cursor-not-allowed"
        }
    },
    defaultVariants: {
        size: "default"
    }
});
const Select = ({ className, contentClassName, size, disabled, color, fullWidth, ...props })=>{
    const [open, setOpen] = (0, _react.useState)(false);
    const [width, setWidth] = (0, _react.useState)(0);
    // const uid = useId();
    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;
        }
        //@ts-ignore
        return props.value || props.label || props.placeholder;
    }, [
        props.value
    ]);
    const options = (0, _react.useMemo)(()=>{
        return props.options || [];
    }, [
        props
    ]);
    const triggerRef = _react.default.useRef(null);
    // const containerRef = React.useRef<HTMLDivElement | null>(null);
    (0, _react.useEffect)(()=>{
        if (triggerRef.current) {
            setWidth(triggerRef.current.offsetWidth);
        }
    }, []);
    (0, _react.useEffect)(()=>{
        if (triggerRef.current) {
            const resizeObserver = new ResizeObserver((entries)=>{
                if (triggerRef.current) {
                    setWidth(triggerRef.current.offsetWidth);
                }
            });
            resizeObserver.observe(triggerRef.current);
            return ()=>{
                if (triggerRef.current) {
                    resizeObserver.unobserve(triggerRef.current);
                }
            };
        }
    }, []);
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_dropdown.DropdownMenu, {
        open: open,
        onOpenChange: setOpen,
        modal: false,
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_dropdown.DropdownMenuTrigger, {
                asChild: true,
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                    ref: triggerRef,
                    className: (0, _css.cn)("orderly-inline-flex orderly-flex-row orderly-items-center orderly-rounded focus-within:orderly-outline orderly-space-x-2 orderly-cursor-pointer", selectVariants({
                        size,
                        disabled: disabled || options.length === 0,
                        className,
                        color,
                        fullWidth
                    }), open && "orderly-bg-base-600"),
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                            className: "orderly-flex-1 orderly-text-3xs desktop:orderly-text-2xs 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")
                        })
                    ]
                })
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_dropdown.DropdownMenuContent, {
                // @ts-ignore
                // container={containerRef.current}
                align: "start",
                style: {
                    minWidth: `${width}px`
                },
                className: contentClassName,
                children: props.options?.map((option, index)=>{
                    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_dropdown.DropdownMenuItem, {
                        // @ts-ignore
                        textValue: option.value,
                        className: (0, _css.cn)("orderly-text-base-contrast/60 hover:orderly-bg-base-700 orderly-text-3xs desktop:orderly-text-2xs", option.value === props.value && (color === "buy" ? "orderly-text-trade-profit" : color === "sell" ? "orderly-text-trade-loss" : "orderly-text-base-contract"), option.className, option.value === props.value && option.activeClassName),
                        onSelect: (evnet)=>{
                            // @ts-ignore
                            props.onChange?.(option.value);
                        },
                        children: option.label
                    }, index);
                })
            })
        ]
    });
};

//# sourceMappingURL=select.js.map