import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { cva, cx } from "class-variance-authority";
import { useEffect, useMemo, useState } from "react";
import { cn } from "../utils/css";
import { ArrowIcon } from "../icon";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "../dropdown/dropdown";
import React from "react";
const selectVariants = 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] = useState(false);
    const [width, setWidth] = useState(0);
    // const uid = useId();
    const label = 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 = useMemo(()=>{
        return props.options || [];
    }, [
        props
    ]);
    const triggerRef = React.useRef(null);
    // const containerRef = React.useRef<HTMLDivElement | null>(null);
    useEffect(()=>{
        if (triggerRef.current) {
            setWidth(triggerRef.current.offsetWidth);
        }
    }, []);
    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__*/ _jsxs(DropdownMenu, {
        open: open,
        onOpenChange: setOpen,
        modal: false,
        children: [
            /*#__PURE__*/ _jsx(DropdownMenuTrigger, {
                asChild: true,
                children: /*#__PURE__*/ _jsxs("div", {
                    ref: triggerRef,
                    className: 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__*/ _jsx("div", {
                            className: "orderly-flex-1 orderly-text-3xs desktop:orderly-text-2xs orderly-text-inherit",
                            children: typeof label !== "undefined" && /*#__PURE__*/ _jsx(_Fragment, {
                                children: label
                            })
                        }),
                        /*#__PURE__*/ _jsx(ArrowIcon, {
                            size: 12,
                            className: cx("orderly-transition-transform", open && "orderly-rotate-180")
                        })
                    ]
                })
            }),
            /*#__PURE__*/ _jsx(DropdownMenuContent, {
                // @ts-ignore
                // container={containerRef.current}
                align: "start",
                style: {
                    minWidth: `${width}px`
                },
                className: contentClassName,
                children: props.options?.map((option, index)=>{
                    return /*#__PURE__*/ _jsx(DropdownMenuItem, {
                        // @ts-ignore
                        textValue: option.value,
                        className: 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);
                })
            })
        ]
    });
};
export { Select, selectVariants };

//# sourceMappingURL=select.js.map