"use client";
import { useProps } from "../../core/MantineProvider/use-props/use-props.mjs";
import { useResolvedStylesApi } from "../../core/styles-api/use-resolved-styles-api/use-resolved-styles-api.mjs";
import { genericFactory } from "../../core/factory/factory.mjs";
import { InputBase } from "../InputBase/InputBase.mjs";
import { getParsedComboboxData } from "../Combobox/get-parsed-combobox-data/get-parsed-combobox-data.mjs";
import { getOptionsLockup } from "../Combobox/get-options-lockup/get-options-lockup.mjs";
import { useCombobox } from "../Combobox/use-combobox/use-combobox.mjs";
import { Combobox } from "../Combobox/Combobox.mjs";
import { OptionsDropdown } from "../Combobox/OptionsDropdown/OptionsDropdown.mjs";
import { useEffect, useMemo, useRef } from "react";
import { useId as useId$1, usePrevious, useUncontrolled } from "@mantine/hooks";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
//#region packages/@mantine/core/src/components/Select/Select.tsx
const defaultProps = {
	size: "sm",
	withCheckIcon: true,
	allowDeselect: true,
	checkIconPosition: "left",
	openOnFocus: true
};
const Select = genericFactory((_props) => {
	const props = useProps([
		"Input",
		"InputWrapper",
		"Select"
	], defaultProps, _props);
	const { classNames, styles, unstyled, vars, dropdownOpened, defaultDropdownOpened, onDropdownClose, onDropdownOpen, onFocus, onBlur, onClick, onChange, data, value, defaultValue, selectFirstOptionOnChange, selectFirstOptionOnDropdownOpen, onOptionSubmit, comboboxProps, readOnly, disabled, filter, limit, withScrollArea, maxDropdownHeight, floatingHeight, size, searchable, rightSection, checkIconPosition, withCheckIcon, withAlignedLabels, nothingFoundMessage, name, form, searchValue, defaultSearchValue, onSearchChange, allowDeselect, error, rightSectionPointerEvents, id, clearable, clearSectionMode, clearButtonProps, hiddenInputProps, renderOption, onClear, autoComplete, scrollAreaProps, __defaultRightSection, __clearSection, __clearable, chevronColor, autoSelectOnBlur, openOnFocus, attributes, ...others } = props;
	const parsedData = useMemo(() => getParsedComboboxData(data), [data]);
	const retainedSelectedOptions = useRef({});
	const optionsLockup = useMemo(() => getOptionsLockup(parsedData), [parsedData]);
	const _id = useId$1(id);
	const [_value, setValue, controlled] = useUncontrolled({
		value,
		defaultValue,
		finalValue: null,
		onChange
	});
	const selectedOption = _value != null ? `${_value}` in optionsLockup ? optionsLockup[`${_value}`] : retainedSelectedOptions.current[`${_value}`] : void 0;
	const previousSelectedOption = usePrevious(selectedOption);
	const [search, setSearch, searchControlled] = useUncontrolled({
		value: searchValue,
		defaultValue: defaultSearchValue,
		finalValue: selectedOption ? selectedOption.label : "",
		onChange: onSearchChange
	});
	const combobox = useCombobox({
		opened: dropdownOpened,
		defaultOpened: defaultDropdownOpened,
		onDropdownOpen: () => {
			onDropdownOpen?.();
			if (selectFirstOptionOnDropdownOpen) combobox.selectFirstOption();
			else combobox.updateSelectedOptionIndex("active", { scrollIntoView: true });
		},
		onDropdownClose: () => {
			onDropdownClose?.();
			setTimeout(combobox.resetSelectedOption, 0);
		}
	});
	const handleSearchChange = (value) => {
		setSearch(value);
		combobox.resetSelectedOption();
	};
	const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi({
		props,
		styles,
		classNames
	});
	useEffect(() => {
		if (selectFirstOptionOnChange) combobox.selectFirstOption();
	}, [selectFirstOptionOnChange, search]);
	useEffect(() => {
		if (value === null) handleSearchChange("");
		if (value != null && selectedOption && (previousSelectedOption?.value !== selectedOption.value || previousSelectedOption?.label !== selectedOption.label)) handleSearchChange(selectedOption.label);
	}, [value, selectedOption]);
	useEffect(() => {
		if (!controlled && !searchControlled) handleSearchChange(_value != null ? `${_value}` in optionsLockup ? optionsLockup[`${_value}`]?.label : retainedSelectedOptions.current[`${_value}`]?.label || "" : "");
	}, [optionsLockup, _value]);
	useEffect(() => {
		if (_value) {
			if (`${_value}` in optionsLockup) retainedSelectedOptions.current[`${_value}`] = optionsLockup[`${_value}`];
		}
	}, [optionsLockup, _value]);
	const clearButton = /* @__PURE__ */ jsx(Combobox.ClearButton, {
		...clearButtonProps,
		onClear: () => {
			setValue(null, null);
			handleSearchChange("");
			onClear?.();
		}
	});
	const _clearable = clearable && _value != null && !disabled && !readOnly;
	return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsxs(Combobox, {
		store: combobox,
		__staticSelector: "Select",
		classNames: resolvedClassNames,
		styles: resolvedStyles,
		unstyled,
		readOnly,
		size,
		attributes,
		floatingHeight,
		keepMounted: autoSelectOnBlur,
		onOptionSubmit: (val) => {
			onOptionSubmit?.(val);
			const optionLockup = allowDeselect ? `${optionsLockup[val].value}` === `${_value}` ? null : optionsLockup[val] : optionsLockup[val];
			const nextValue = optionLockup ? optionLockup.value : null;
			nextValue !== _value && setValue(nextValue, optionLockup);
			!controlled && handleSearchChange(nextValue != null ? optionLockup?.label || "" : "");
			combobox.closeDropdown();
		},
		...comboboxProps,
		children: [/* @__PURE__ */ jsx(Combobox.Target, {
			targetType: searchable ? "input" : "button",
			autoComplete,
			withExpandedAttribute: true,
			children: /* @__PURE__ */ jsx(InputBase, {
				id: _id,
				__defaultRightSection: /* @__PURE__ */ jsx(Combobox.Chevron, {
					size,
					error,
					unstyled,
					color: chevronColor
				}),
				__clearSection: clearButton,
				__clearable: _clearable,
				__clearSectionMode: clearSectionMode,
				rightSection,
				rightSectionPointerEvents: rightSectionPointerEvents || "none",
				...others,
				size,
				__staticSelector: "Select",
				disabled,
				readOnly: readOnly || !searchable,
				value: search,
				onChange: (event) => {
					handleSearchChange(event.currentTarget.value);
					combobox.openDropdown();
					selectFirstOptionOnChange && combobox.selectFirstOption();
				},
				onFocus: (event) => {
					openOnFocus && searchable && combobox.openDropdown();
					onFocus?.(event);
				},
				onBlur: (event) => {
					if (autoSelectOnBlur) combobox.clickSelectedOption();
					searchable && combobox.closeDropdown();
					const optionLockup = _value != null && (`${_value}` in optionsLockup ? optionsLockup[`${_value}`] : retainedSelectedOptions.current[`${_value}`]);
					handleSearchChange(optionLockup ? optionLockup.label || "" : "");
					onBlur?.(event);
				},
				onClick: (event) => {
					searchable ? combobox.openDropdown() : combobox.toggleDropdown();
					onClick?.(event);
				},
				classNames: resolvedClassNames,
				styles: resolvedStyles,
				unstyled,
				pointer: !searchable,
				error,
				attributes
			})
		}), /* @__PURE__ */ jsx(OptionsDropdown, {
			data: parsedData,
			hidden: readOnly || disabled,
			filter,
			search,
			limit,
			hiddenWhenEmpty: !nothingFoundMessage,
			withScrollArea,
			maxDropdownHeight,
			filterOptions: !!searchable && selectedOption?.label !== search,
			value: _value,
			checkIconPosition,
			withCheckIcon,
			withAlignedLabels,
			nothingFoundMessage,
			unstyled,
			labelId: others.label ? `${_id}-label` : void 0,
			"aria-label": others.label ? void 0 : others["aria-label"],
			renderOption,
			scrollAreaProps
		})]
	}), /* @__PURE__ */ jsx(Combobox.HiddenInput, {
		value: _value,
		name,
		form,
		disabled,
		...hiddenInputProps
	})] });
});
Select.classes = {
	...InputBase.classes,
	...Combobox.classes
};
Select.displayName = "@mantine/core/Select";
//#endregion
export { Select };

//# sourceMappingURL=Select.mjs.map