"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.textFieldTypeValues = exports.TextField = void 0;
var _InputBase2 = _interopRequireDefault(require("@mui/material/InputBase"));
var _InputAdornment2 = _interopRequireDefault(require("@mui/material/InputAdornment"));
var _react = require("react");
var _Field = require("./Field.cjs");
var _inputUtils = require("./inputUtils.cjs");
var _jsxRuntime = require("react/jsx-runtime");
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 textFieldTypeValues = exports.textFieldTypeValues = ["email", "number", "tel", "text", "url"];
const TextField = (0, _react.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 = (0, _react.useRef)((0, _inputUtils.getControlState)({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const inputValues = (0, _inputUtils.useInputValues)({
    defaultValue,
    value,
    controlState: controlledStateRef.current
  });
  const localInputRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(inputRef, () => {
    return {
      focus: () => {
        localInputRef.current?.focus();
      }
    };
  }, []);
  const onChange = (0, _react.useCallback)(event => {
    onChangeProp?.(event);
  }, [onChangeProp]);
  const renderFieldComponent = (0, _react.useCallback)(({
    ariaDescribedBy,
    errorMessageElementId,
    id,
    labelElementId
  }) => (0, _jsxRuntime.jsx)(_InputBase2.default, {
    ...inputValues,
    "aria-describedby": ariaDescribedBy,
    autoComplete: autoCompleteType,
    autoFocus: hasInitialFocus,
    endAdornment: endAdornment && (0, _jsxRuntime.jsx)(_InputAdornment2.default, {
      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 && (0, _jsxRuntime.jsx)(_InputAdornment2.default, {
      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 (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 MemoizedTextField = exports.TextField = (0, _react.memo)(TextField);
MemoizedTextField.displayName = "TextField";
//# sourceMappingURL=TextField.cjs.map