"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
require("clsx");
const useResolvedStylesApi = require("../../core/styles-api/use-resolved-styles-api/use-resolved-styles-api.cjs");
const useProps = require("../../core/MantineProvider/use-props/use-props.cjs");
const factory = require("../../core/factory/factory.cjs");
const getParsedComboboxData = require("../Combobox/get-parsed-combobox-data/get-parsed-combobox-data.cjs");
const getOptionsLockup = require("../Combobox/get-options-lockup/get-options-lockup.cjs");
const Combobox = require("../Combobox/Combobox.cjs");
const OptionsDropdown = require("../Combobox/OptionsDropdown/OptionsDropdown.cjs");
const useCombobox = require("../Combobox/use-combobox/use-combobox.cjs");
const InputBase = require("../InputBase/InputBase.cjs");
const useId = require("../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-id/use-id.cjs");
const useUncontrolled = require("../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-uncontrolled/use-uncontrolled.cjs");
const usePrevious = require("../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-previous/use-previous.cjs");
const defaultProps = {
  searchable: false,
  withCheckIcon: true,
  allowDeselect: true,
  checkIconPosition: "left"
};
const Select = factory.factory((_props, ref) => {
  const props = useProps.useProps("Select", defaultProps, _props);
  const {
    classNames,
    styles,
    unstyled,
    vars,
    dropdownOpened,
    defaultDropdownOpened,
    onDropdownClose,
    onDropdownOpen,
    onFocus,
    onBlur,
    onClick,
    onChange,
    data,
    value,
    defaultValue,
    selectFirstOptionOnChange,
    onOptionSubmit,
    comboboxProps,
    readOnly,
    disabled,
    filter,
    limit,
    withScrollArea,
    maxDropdownHeight,
    size,
    searchable,
    rightSection,
    checkIconPosition,
    withCheckIcon,
    nothingFoundMessage,
    name,
    form,
    searchValue,
    defaultSearchValue,
    onSearchChange,
    allowDeselect,
    error,
    rightSectionPointerEvents,
    id,
    clearable,
    clearButtonProps,
    hiddenInputProps,
    renderOption,
    onClear,
    autoComplete,
    scrollAreaProps,
    ...others
  } = props;
  const parsedData = React.useMemo(() => getParsedComboboxData.getParsedComboboxData(data), [data]);
  const optionsLockup = React.useMemo(() => getOptionsLockup.getOptionsLockup(parsedData), [parsedData]);
  const _id = useId.useId(id);
  const [_value, setValue, controlled] = useUncontrolled.useUncontrolled({
    value,
    defaultValue,
    finalValue: null,
    onChange
  });
  const selectedOption = typeof _value === "string" ? optionsLockup[_value] : void 0;
  const previousSelectedOption = usePrevious.usePrevious(selectedOption);
  const [search, setSearch] = useUncontrolled.useUncontrolled({
    value: searchValue,
    defaultValue: defaultSearchValue,
    finalValue: selectedOption ? selectedOption.label : "",
    onChange: onSearchChange
  });
  const combobox = useCombobox.useCombobox({
    opened: dropdownOpened,
    defaultOpened: defaultDropdownOpened,
    onDropdownOpen: () => {
      onDropdownOpen == null ? void 0 : onDropdownOpen();
      combobox.updateSelectedOptionIndex("active", { scrollIntoView: true });
    },
    onDropdownClose: () => {
      onDropdownClose == null ? void 0 : onDropdownClose();
      combobox.resetSelectedOption();
    }
  });
  const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi.useResolvedStylesApi({
    props,
    styles,
    classNames
  });
  React.useEffect(() => {
    if (selectFirstOptionOnChange) {
      combobox.selectFirstOption();
    }
  }, [selectFirstOptionOnChange, _value]);
  React.useEffect(() => {
    if (value === null) {
      setSearch("");
    }
    if (typeof value === "string" && selectedOption && ((previousSelectedOption == null ? void 0 : previousSelectedOption.value) !== selectedOption.value || (previousSelectedOption == null ? void 0 : previousSelectedOption.label) !== selectedOption.label)) {
      setSearch(selectedOption.label);
    }
  }, [value, selectedOption]);
  const clearButton = clearable && !!_value && !disabled && !readOnly && /* @__PURE__ */ jsxRuntime.jsx(
    Combobox.Combobox.ClearButton,
    {
      size,
      ...clearButtonProps,
      onClear: () => {
        setValue(null, null);
        setSearch("");
        onClear == null ? void 0 : onClear();
      }
    }
  );
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsxs(
      Combobox.Combobox,
      {
        store: combobox,
        __staticSelector: "Select",
        classNames: resolvedClassNames,
        styles: resolvedStyles,
        unstyled,
        readOnly,
        onOptionSubmit: (val) => {
          onOptionSubmit == null ? void 0 : 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 && setSearch(typeof nextValue === "string" ? (optionLockup == null ? void 0 : optionLockup.label) || "" : "");
          combobox.closeDropdown();
        },
        size,
        ...comboboxProps,
        children: [
          /* @__PURE__ */ jsxRuntime.jsx(Combobox.Combobox.Target, { targetType: searchable ? "input" : "button", autoComplete, children: /* @__PURE__ */ jsxRuntime.jsx(
            InputBase.InputBase,
            {
              id: _id,
              ref,
              rightSection: rightSection || clearButton || /* @__PURE__ */ jsxRuntime.jsx(Combobox.Combobox.Chevron, { size, error, unstyled }),
              rightSectionPointerEvents: rightSectionPointerEvents || (clearButton ? "all" : "none"),
              ...others,
              size,
              __staticSelector: "Select",
              disabled,
              readOnly: readOnly || !searchable,
              value: search,
              onChange: (event) => {
                setSearch(event.currentTarget.value);
                combobox.openDropdown();
                selectFirstOptionOnChange && combobox.selectFirstOption();
              },
              onFocus: (event) => {
                searchable && combobox.openDropdown();
                onFocus == null ? void 0 : onFocus(event);
              },
              onBlur: (event) => {
                var _a;
                searchable && combobox.closeDropdown();
                setSearch(_value != null ? ((_a = optionsLockup[_value]) == null ? void 0 : _a.label) || "" : "");
                onBlur == null ? void 0 : onBlur(event);
              },
              onClick: (event) => {
                searchable ? combobox.openDropdown() : combobox.toggleDropdown();
                onClick == null ? void 0 : onClick(event);
              },
              classNames: resolvedClassNames,
              styles: resolvedStyles,
              unstyled,
              pointer: !searchable,
              error
            }
          ) }),
          /* @__PURE__ */ jsxRuntime.jsx(
            OptionsDropdown.OptionsDropdown,
            {
              data: parsedData,
              hidden: readOnly || disabled,
              filter,
              search,
              limit,
              hiddenWhenEmpty: !nothingFoundMessage,
              withScrollArea,
              maxDropdownHeight,
              filterOptions: searchable && (selectedOption == null ? void 0 : selectedOption.label) !== search,
              value: _value,
              checkIconPosition,
              withCheckIcon,
              nothingFoundMessage,
              unstyled,
              labelId: others.label ? `${_id}-label` : void 0,
              "aria-label": others.label ? void 0 : others["aria-label"],
              renderOption,
              scrollAreaProps
            }
          )
        ]
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(
      Combobox.Combobox.HiddenInput,
      {
        value: _value,
        name,
        form,
        disabled,
        ...hiddenInputProps
      }
    )
  ] });
});
Select.classes = { ...InputBase.InputBase.classes, ...Combobox.Combobox.classes };
Select.displayName = "@mantine/core/Select";
exports.Select = Select;
