import _Select from "@mui/material/Select";
/*!
 * Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */
import React, { forwardRef, memo, useCallback, useImperativeHandle, useMemo, useRef } from "react";
import { Field } from "./Field.js";
import { getControlState, useInputValues } from "./inputUtils.js";
import { jsx as _jsx } from "react/jsx-runtime";
const NativeSelect = forwardRef(({
  ariaDescribedBy,
  autoCompleteType,
  defaultValue,
  errorMessage,
  errorMessageList,
  hasMultipleChoices: hasMultipleChoicesProp,
  hint,
  HintLinkComponent,
  id: idOverride,
  inputRef,
  isDisabled = false,
  isFullWidth = false,
  isMultiSelect,
  isOptional = false,
  label,
  onBlur,
  onChange: onChangeProp,
  onFocus,
  testId,
  translate,
  value,
  children
}, ref) => {
  const controlledStateRef = useRef(getControlState({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const localInputRef = useRef(null);
  useImperativeHandle(inputRef, () => {
    return {
      focus: () => {
        localInputRef.current?.focus();
      }
    };
  }, []);
  const inputValues = useInputValues({
    defaultValue,
    value,
    controlState: controlledStateRef.current
  });
  const onChange = useCallback((event, child) => {
    onChangeProp?.(event, child);
  }, [onChangeProp]);
  const hasMultipleChoices = useMemo(() => hasMultipleChoicesProp === undefined ? isMultiSelect : hasMultipleChoicesProp, [hasMultipleChoicesProp, isMultiSelect]);
  const renderFieldComponent = useCallback(({
    ariaDescribedBy,
    errorMessageElementId,
    labelElementId
  }) => _jsx(_Select, {
    ...inputValues,
    "aria-describedby": ariaDescribedBy,
    autoComplete: autoCompleteType,
    children: children,
    id: idOverride,
    inputProps: {
      "aria-errormessage": errorMessageElementId,
      "aria-labelledby": labelElementId,
      "data-se": testId
    },
    inputRef: localInputRef,
    multiple: hasMultipleChoices,
    name: idOverride,
    native: true,
    onBlur: onBlur,
    onChange: onChange,
    onFocus: onFocus,
    ref: ref,
    translate: translate
  }), [autoCompleteType, children, idOverride, inputValues, hasMultipleChoices, onBlur, onChange, onFocus, ref, testId, translate]);
  return _jsx(Field, {
    ariaDescribedBy: ariaDescribedBy,
    errorMessage: errorMessage,
    errorMessageList: errorMessageList,
    fieldType: "single",
    hasVisibleLabel: true,
    hint: hint,
    HintLinkComponent: HintLinkComponent,
    id: idOverride,
    isDisabled: isDisabled,
    isFullWidth: isFullWidth,
    isOptional: isOptional,
    label: label,
    renderFieldComponent: renderFieldComponent
  });
});
const MemoizedNativeSelect = memo(NativeSelect);
MemoizedNativeSelect.displayName = "NativeSelect";
export { MemoizedNativeSelect as NativeSelect };
//# sourceMappingURL=NativeSelect.js.map