"use client";
import { cn } from "./index3.js";
import { useControllableState } from "./index11.js";
import { FieldTooltip } from "./index19.js";
import { hasFieldProps, splitFieldProps } from "./index20.js";
import { Field, FieldBottom, FieldBottomRight, FieldControl, FieldDescription, FieldError, FieldLabel, FieldTop, FieldTopRight } from "./index22.js";
import { Check, ChevronDown, ChevronUp } from "lucide-react";
import * as React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Select } from "@base-ui/react/select";
//#region src/components/atoms/forms/Select/Select.tsx
var selectTriggerSizeClasses = {
	xs: "h-7 px-3 py-1 text-xs [&_svg]:size-4 [@media(pointer:coarse)]:min-h-11",
	sm: "h-8 px-3 py-1.5 text-xs [&_svg]:size-[18px] [@media(pointer:coarse)]:min-h-11",
	md: "h-9 px-3 py-2 text-xs [&_svg]:size-5",
	lg: "h-10 px-3 py-2.5 text-sm [&_svg]:size-5"
};
var SelectTriggerIcon = React.forwardRef(({ className, children, ...props }, ref) => /* @__PURE__ */ jsx("span", {
	ref,
	"data-slot": "select-trigger-icon",
	className: cn("shrink-0 text-muted-foreground", className),
	...props,
	children
}));
SelectTriggerIcon.displayName = "SelectTrigger.Icon";
/** Recursively extract value→label entries from SelectItem children for base-ui label resolution. */
function extractItemsFromChildren(node) {
	const map = {};
	let found = false;
	function traverse(n) {
		React.Children.forEach(n, (child) => {
			if (!React.isValidElement(child)) return;
			const p = child.props;
			if (p.value != null) {
				map[String(p.value)] = p.children;
				found = true;
				return;
			}
			if (p.children != null) traverse(p.children);
		});
	}
	traverse(node);
	return found ? map : void 0;
}
function SelectRoot({ trigger, value: controlledValue, defaultValue, onValueChange, items: itemsProp, children, ...props }) {
	const [value, setValue] = useControllableState({
		value: controlledValue,
		defaultValue: defaultValue ?? "",
		onChange: onValueChange
	});
	const handleValueChange = React.useCallback((newValue) => {
		setValue(newValue);
	}, [setValue]);
	const items = itemsProp ?? extractItemsFromChildren(children);
	return /* @__PURE__ */ jsxs(Select.Root, {
		value,
		onValueChange: handleValueChange,
		items,
		...props,
		children: [trigger && /* @__PURE__ */ jsx(SelectTrigger, { render: trigger }), children]
	});
}
var SelectGroup = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(Select.Group, {
	ref,
	"data-slot": "select-group",
	className: cn("scroll-my-1 p-1", className),
	...props
}));
SelectGroup.displayName = "Select.Group";
var SelectValue = React.forwardRef(({ placeholder, children, className, ...props }, ref) => /* @__PURE__ */ jsx(Select.Value, {
	ref,
	"data-slot": "select-value",
	className: cn("flex flex-1 text-left", className),
	placeholder: !children && placeholder ? /* @__PURE__ */ jsx("span", {
		className: "text-muted-foreground",
		children: placeholder
	}) : void 0,
	...props,
	children
}));
SelectValue.displayName = "Select.Value";
var SelectTrigger = React.forwardRef(({ className, size = "md", shape = "rounded", state = "default", readOnly = false, children, "aria-label": ariaLabel, ...props }, ref) => {
	let resolvedAriaLabel = ariaLabel;
	if (!resolvedAriaLabel) React.Children.forEach(children, (child) => {
		if (React.isValidElement(child) && typeof child.props === "object" && child.props !== null && "placeholder" in child.props && typeof child.props.placeholder === "string") resolvedAriaLabel = child.props.placeholder;
	});
	if (readOnly) return /* @__PURE__ */ jsx("output", {
		ref,
		"data-slot": "select-trigger",
		"data-readonly": true,
		"data-size": size,
		"data-shape": shape,
		className: cn("flex w-full items-center gap-1.5 bg-transparent", selectTriggerSizeClasses[size], shape === "rounded" && "rounded-xl", shape === "round" && "rounded-full", "text-foreground", className),
		children
	});
	return /* @__PURE__ */ jsxs(Select.Trigger, {
		ref,
		"data-slot": "select-trigger",
		"data-size": size,
		"data-shape": shape,
		"data-state": state,
		"aria-label": resolvedAriaLabel,
		"aria-invalid": state === "error" || void 0,
		className: cn("group/select flex w-full items-center justify-between gap-1.5 outline-none", "bg-background border shadow-xs transition-[color,box-shadow,border-color] duration-200 ease-smooth motion-reduce:transition-none", "disabled:cursor-not-allowed disabled:opacity-50", "*:data-[slot=select-value]:flex *:data-[slot=select-value]:flex-1 *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5", "*:data-[slot=select-value]:overflow-hidden *:data-[slot=select-value]:text-ellipsis *:data-[slot=select-value]:whitespace-nowrap", "data-[placeholder]:text-muted-foreground", selectTriggerSizeClasses[size], shape === "rounded" && "rounded-xl", shape === "round" && "rounded-full", state === "default" && "border-input", state === "success" && "border-success", state === "error" && "border-destructive", "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", state === "error" && "focus-visible:ring-destructive/20", className),
		...props,
		children: [children, /* @__PURE__ */ jsx(Select.Icon, { render: /* @__PURE__ */ jsx(ChevronDown, { className: "size-4 shrink-0 text-muted-foreground transition-transform duration-200 ease-smooth group-data-[popup-open]/select:rotate-180 motion-reduce:transition-none" }) })]
	});
});
SelectTrigger.displayName = "Select.Trigger";
var SelectPopup = React.forwardRef(({ className, children, side = "bottom", sideOffset = 4, align = "center", alignOffset = 0, alignItemWithTrigger = true, ...props }, ref) => /* @__PURE__ */ jsx(Select.Portal, { children: /* @__PURE__ */ jsx(Select.Positioner, {
	side,
	sideOffset,
	align,
	alignOffset,
	alignItemWithTrigger,
	className: "isolate z-50",
	children: /* @__PURE__ */ jsxs(Select.Popup, {
		ref,
		"data-slot": "select-popup",
		className: cn("bg-popover text-popover-foreground data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 data-closed:zoom-out-95 data-open:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 motion-reduce:animate-none ring-foreground/10 min-w-36 rounded-md shadow-md ring-1 duration-100 relative isolate z-50 max-h-(--available-height) w-(--anchor-width) origin-(--transform-origin) overflow-x-hidden overflow-y-auto", className),
		...props,
		children: [
			/* @__PURE__ */ jsx(SelectScrollUpButton, {}),
			/* @__PURE__ */ jsx(Select.List, { children }),
			/* @__PURE__ */ jsx(SelectScrollDownButton, {})
		]
	})
}) }));
SelectPopup.displayName = "Select.Popup";
var SelectGroupLabel = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(Select.GroupLabel, {
	ref,
	"data-slot": "select-group-label",
	className: cn("text-muted-foreground px-2 py-1.5 text-xs", className),
	...props
}));
SelectGroupLabel.displayName = "Select.GroupLabel";
var SelectItem = React.forwardRef(({ className, children, ...props }, ref) => /* @__PURE__ */ jsxs(Select.Item, {
	ref,
	"data-slot": "select-item",
	className: cn("focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground gap-2 rounded-sm py-1.5 pe-8 ps-2 text-sm [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2 relative flex w-full cursor-default items-center outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0", className),
	...props,
	children: [/* @__PURE__ */ jsx(Select.ItemText, {
		className: "flex flex-1 gap-2 shrink-0 whitespace-nowrap",
		children
	}), /* @__PURE__ */ jsx(Select.ItemIndicator, {
		render: /* @__PURE__ */ jsx("span", { className: "pointer-events-none absolute end-2 flex size-4 items-center justify-center" }),
		children: /* @__PURE__ */ jsx(Check, { className: "pointer-events-none" })
	})]
}));
SelectItem.displayName = "Select.Item";
var SelectSeparator = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(Select.Separator, {
	ref,
	"data-slot": "select-separator",
	className: cn("bg-border -mx-1 my-1 h-px pointer-events-none", className),
	...props
}));
SelectSeparator.displayName = "Select.Separator";
var SelectScrollUpButton = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(Select.ScrollUpArrow, {
	ref,
	"data-slot": "select-scroll-up-button",
	className: cn("bg-popover z-10 flex cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4 top-0 w-full", className),
	...props,
	children: /* @__PURE__ */ jsx(ChevronUp, {})
}));
SelectScrollUpButton.displayName = "Select.ScrollUpButton";
var SelectScrollDownButton = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(Select.ScrollDownArrow, {
	ref,
	"data-slot": "select-scroll-down-button",
	className: cn("bg-popover z-10 flex cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4 bottom-0 w-full", className),
	...props,
	children: /* @__PURE__ */ jsx(ChevronDown, {})
}));
SelectScrollDownButton.displayName = "Select.ScrollDownButton";
SelectRoot.displayName = "SelectRoot";
function Select$1({ placeholder, size = "md", shape = "rounded", readOnly = false, value, defaultValue, onValueChange, disabled, children, className, name, ...rest }) {
	const [fieldPropsObj, _leftover] = splitFieldProps(rest);
	if (!hasFieldProps(fieldPropsObj)) return /* @__PURE__ */ jsxs(SelectRoot, {
		value,
		defaultValue,
		onValueChange,
		disabled,
		name,
		children: [/* @__PURE__ */ jsx(SelectTrigger, {
			size,
			shape,
			readOnly,
			state: fieldPropsObj.error ? "error" : "default",
			className,
			"aria-label": placeholder,
			children: /* @__PURE__ */ jsx(SelectValue, { placeholder })
		}), !readOnly && /* @__PURE__ */ jsx(SelectPopup, { children })]
	});
	const { label, error, description, tooltip, labelIcon, topRight, bottomRight, fieldClassName } = fieldPropsObj;
	const hasTopRow = label || topRight;
	const hasBottomRow = description || bottomRight || error;
	return /* @__PURE__ */ jsxs(Field, {
		state: error ? "destructive" : void 0,
		disabled,
		className: fieldClassName,
		children: [
			hasTopRow && /* @__PURE__ */ jsxs(FieldTop, { children: [label && /* @__PURE__ */ jsxs(FieldLabel, {
				required: fieldPropsObj.required,
				labelIcon,
				children: [label, tooltip && /* @__PURE__ */ jsx(FieldTooltip, { content: tooltip })]
			}), topRight && /* @__PURE__ */ jsx(FieldTopRight, { children: topRight })] }),
			/* @__PURE__ */ jsx(FieldControl, { children: (ctrlProps) => /* @__PURE__ */ jsxs(SelectRoot, {
				value,
				defaultValue,
				onValueChange,
				disabled,
				name,
				children: [/* @__PURE__ */ jsx(SelectTrigger, {
					id: ctrlProps.id,
					"aria-describedby": ctrlProps["aria-describedby"],
					"aria-invalid": ctrlProps["aria-invalid"],
					size,
					shape,
					readOnly,
					state: error ? "error" : "default",
					className,
					"aria-label": placeholder,
					children: /* @__PURE__ */ jsx(SelectValue, { placeholder })
				}), !readOnly && /* @__PURE__ */ jsx(SelectPopup, { children })]
			}) }),
			hasBottomRow && /* @__PURE__ */ jsxs(FieldBottom, { children: [
				error && /* @__PURE__ */ jsx(FieldError, { children: error }),
				!error && description && /* @__PURE__ */ jsx(FieldDescription, { children: description }),
				bottomRight && /* @__PURE__ */ jsx(FieldBottomRight, {
					className: "ms-auto",
					children: bottomRight
				})
			] })
		]
	});
}
Select$1.displayName = "Select";
/**
* Compound Select for advanced usage (custom triggers, popups, etc.)
*
* @example
* ```tsx
* <SelectCompound>
*   <SelectTrigger>
*     <SelectValue placeholder="Select an option" />
*   </SelectTrigger>
*   <SelectPopup>
*     <SelectItem value="1">Option 1</SelectItem>
*   </SelectPopup>
* </SelectCompound>
* ```
*/
var SelectCompound = Object.assign(SelectRoot, {
	Root: SelectRoot,
	Group: SelectGroup,
	Value: SelectValue,
	Trigger: Object.assign(SelectTrigger, { Icon: SelectTriggerIcon }),
	Popup: SelectPopup,
	GroupLabel: SelectGroupLabel,
	Item: SelectItem,
	Separator: SelectSeparator,
	ScrollUpButton: SelectScrollUpButton,
	ScrollDownButton: SelectScrollDownButton
});
//#endregion
export { Select$1 as Select, SelectCompound, SelectGroup, SelectGroupLabel, SelectItem, SelectPopup, SelectRoot, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectTriggerIcon, SelectValue };

//# sourceMappingURL=index32.js.map