import _InputBase from "@mui/material/InputBase";
import _InputAdornment from "@mui/material/InputAdornment";
/*!
 * 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 { forwardRef, memo, useCallback, useImperativeHandle, useRef } from "react";
import { Field } from "./Field.js";
import { getControlState, useInputValues } from "./inputUtils.js";
import { jsx as _jsx } from "react/jsx-runtime";
export const textFieldTypeValues = ["email", "number", "tel", "text", "url"];
const TextField = forwardRef(({
  ariaDescribedBy,
  autoCompleteType,
  defaultValue,
  hasInitialFocus,
  endAdornment,
  errorMessage,
  errorMessageList,
  hint,
  HintLinkComponent,
  id: idOverride,
  inputRef,
  inputMode,
  isDisabled = false,
  isFullWidth = false,
  isMultiline = false,
  isOptional = false,
  isReadOnly,
  label,
  name: nameOverride,
  onBlur,
  onChange: onChangeProp,
  onFocus,
  placeholder,
  startAdornment,
  testId,
  translate,
  type = "text",
  value: value
}, ref) => {
  const controlledStateRef = useRef(getControlState({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const inputValues = useInputValues({
    defaultValue,
    value,
    controlState: controlledStateRef.current
  });
  const localInputRef = useRef(null);
  useImperativeHandle(inputRef, () => {
    return {
      focus: () => {
        localInputRef.current?.focus();
      }
    };
  }, []);
  const onChange = useCallback(event => {
    onChangeProp?.(event);
  }, [onChangeProp]);
  const renderFieldComponent = useCallback(({
    ariaDescribedBy,
    errorMessageElementId,
    id,
    labelElementId
  }) => _jsx(_InputBase, {
    ...inputValues,
    "aria-describedby": ariaDescribedBy,
    autoComplete: autoCompleteType,
    autoFocus: hasInitialFocus,
    endAdornment: endAdornment && _jsx(_InputAdornment, {
      position: "end",
      translate: translate,
      children: endAdornment
    }),
    id: id,
    inputProps: {
      "aria-errormessage": errorMessageElementId,
      "aria-labelledby": labelElementId,
      "data-se": testId,
      inputMode
    },
    inputRef: localInputRef,
    multiline: isMultiline,
    name: nameOverride ?? id,
    onBlur: onBlur,
    onChange: onChange,
    onFocus: onFocus,
    placeholder: placeholder,
    readOnly: isReadOnly,
    ref: ref,
    required: !isOptional,
    startAdornment: startAdornment && _jsx(_InputAdornment, {
      position: "start",
      translate: translate,
      children: startAdornment
    }),
    translate: translate,
    type: type
  }), [autoCompleteType, inputValues, hasInitialFocus, endAdornment, inputMode, isMultiline, nameOverride, onBlur, onChange, onFocus, placeholder, isOptional, isReadOnly, ref, startAdornment, testId, translate, type]);
  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 MemoizedTextField = memo(TextField);
MemoizedTextField.displayName = "TextField";
export { MemoizedTextField as TextField };
//# sourceMappingURL=TextField.js.map