import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
import { cva } from "class-variance-authority";
import { cn } from "../utils/css";
import { ActionSheet } from "../sheet";
import { ArrowIcon } from "../icon";
const pickerVariants = cva("orderly-flex orderly-flex-row orderly-items-stretch orderly-rounded focus-within:orderly-outline orderly-outline-primary orderly-bg-base-700 orderly-text-base-contrast/50", {
    variants: {
        size: {
            small: "orderly-px-2 orderly-h-[26px] orderly-text-3xs",
            default: "orderly-px-3 orderly-h-[40px]",
            large: "orderly-px-6 orderly-py-3"
        },
        fullWidth: {
            true: "orderly-w-full"
        },
        disabled: {
            true: "orderly-opacity-50 orderly-cursor-not-allowed"
        },
        color: {
            // primary: "text-primary",
            default: "orderly-text-base-contract",
            buy: "orderly-text-trade-profit",
            sell: "orderly-text-trade-loss"
        }
    },
    defaultVariants: {
        size: "default",
        color: "default"
    }
});
export const Picker = /*#__PURE__*/ forwardRef(({ size, color, value, label, //@ts-ignore
placeholder, className, options, fullWidth, disabled, ...props }, ref)=>{
    const [open, setOpen] = useState(false);
    const selectedItem = useMemo(()=>{
        if (value && !!value.value) {
            return value;
        }
        if (typeof value === "number" || typeof value === "string") {
            const option = options.find((option)=>option.value === value);
            if (option) {
                return option;
            }
        }
    }, [
        value,
        options
    ]);
    const text = useMemo(()=>{
        if (selectedItem) {
            return selectedItem.label;
        }
        return placeholder || label || "";
    }, [
        selectedItem,
        label,
        placeholder
    ]);
    // @ts-ignore
    const actions = useMemo(()=>{
        return [
            ...options,
            "---",
            "Cancel"
        ];
    }, [
        options
    ]);
    const triggerRef = useRef(null);
    const [dropdownWidth, setDropdownWidth] = useState(218);
    useEffect(()=>{
        if (triggerRef.current) {
            // @ts-ignore
            const width = triggerRef.current.offsetWidth;
            setDropdownWidth(width);
        }
    }, []);
    return /*#__PURE__*/ _jsx(ActionSheet, {
        actionSheets: actions,
        onOpenChange: setOpen,
        open: open,
        onClose: ()=>setOpen(false),
        // @ts-ignore
        value: selectedItem,
        onValueChange: props.onValueChange,
        children: /*#__PURE__*/ _jsxs("div", {
            className: cn("orderly-space-x-1 orderly-text-base-contrast/80", pickerVariants({
                size,
                color,
                fullWidth,
                disabled: disabled || options.length === 0,
                className
            })),
            children: [
                /*#__PURE__*/ _jsx("div", {
                    className: "orderly-flex-1 orderly-flex orderly-justify-start orderly-items-center orderly-text-inherit",
                    children: text
                }),
                /*#__PURE__*/ _jsx("div", {
                    className: "orderly-flex orderly-items-center",
                    children: /*#__PURE__*/ _jsx(ArrowIcon, {
                        size: 12,
                        className: "orderly-text-inherit"
                    })
                })
            ]
        })
    });
});

//# sourceMappingURL=picker.js.map