"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.NativeSelect = void 0;
var _Select2 = _interopRequireDefault(require("@mui/material/Select"));
var _react = _interopRequireWildcard(require("react"));
var _Field = require("./Field.cjs");
var _inputUtils = require("./inputUtils.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * 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.
 */

const NativeSelect = (0, _react.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 = (0, _react.useRef)((0, _inputUtils.getControlState)({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const localInputRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(inputRef, () => {
    return {
      focus: () => {
        localInputRef.current?.focus();
      }
    };
  }, []);
  const inputValues = (0, _inputUtils.useInputValues)({
    defaultValue,
    value,
    controlState: controlledStateRef.current
  });
  const onChange = (0, _react.useCallback)((event, child) => {
    onChangeProp?.(event, child);
  }, [onChangeProp]);
  const hasMultipleChoices = (0, _react.useMemo)(() => hasMultipleChoicesProp === undefined ? isMultiSelect : hasMultipleChoicesProp, [hasMultipleChoicesProp, isMultiSelect]);
  const renderFieldComponent = (0, _react.useCallback)(({
    ariaDescribedBy,
    errorMessageElementId,
    labelElementId
  }) => (0, _jsxRuntime.jsx)(_Select2.default, {
    ...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 (0, _jsxRuntime.jsx)(_Field.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 = exports.NativeSelect = (0, _react.memo)(NativeSelect);
MemoizedNativeSelect.displayName = "NativeSelect";
//# sourceMappingURL=NativeSelect.cjs.map