"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PasswordField = void 0;
var _InputBase2 = _interopRequireDefault(require("@mui/material/InputBase"));
var _InputAdornment2 = _interopRequireDefault(require("@mui/material/InputAdornment"));
var _IconButton2 = _interopRequireDefault(require("@mui/material/IconButton"));
var _react = require("react");
var _Field = require("./Field.cjs");
var _i18n = require("./i18n.generated/i18n.cjs");
var _index = require("./icons.generated/index.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 PasswordField = (0, _react.forwardRef)(({
  ariaDescribedBy,
  autoCompleteType,
  defaultValue,
  errorMessage,
  errorMessageList,
  hasInitialFocus,
  hint,
  id: idOverride,
  inputRef,
  isDisabled = false,
  isFullWidth = false,
  isOptional = false,
  hasShowPassword = true,
  isReadOnly,
  label,
  name: nameOverride,
  onChange: onChangeProp,
  onFocus,
  onBlur,
  placeholder,
  testId,
  translate,
  value
}, ref) => {
  const {
    t
  } = (0, _i18n.useTranslation)();
  const [inputType, setInputType] = (0, _react.useState)("password");
  const togglePasswordVisibility = (0, _react.useCallback)(() => {
    setInputType(inputType => inputType === "password" ? "text" : "password");
  }, []);
  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: inputType === "password" ? autoCompleteType : "off",
    autoFocus: hasInitialFocus,
    endAdornment: hasShowPassword && (0, _jsxRuntime.jsx)(_InputAdornment2.default, {
      position: "end",
      children: (0, _jsxRuntime.jsx)(_IconButton2.default, {
        "aria-controls": id,
        "aria-label": t("passwordfield.icon.label.show"),
        "aria-pressed": inputType === "text",
        onClick: togglePasswordVisibility,
        children: inputType === "password" ? (0, _jsxRuntime.jsx)(_index.ShowIcon, {}) : (0, _jsxRuntime.jsx)(_index.HideIcon, {})
      })
    }),
    id: id,
    inputProps: {
      "aria-errormessage": errorMessageElementId,
      "aria-labelledby": labelElementId,
      "data-se": testId,
      role: "textbox"
    },
    inputRef: localInputRef,
    name: nameOverride ?? id,
    onBlur: onBlur,
    onChange: onChange,
    onFocus: onFocus,
    placeholder: placeholder,
    readOnly: isReadOnly,
    ref: ref,
    required: !isOptional,
    translate: translate,
    type: inputType
  }), [autoCompleteType, hasInitialFocus, inputValues, t, togglePasswordVisibility, inputType, nameOverride, onChange, onFocus, onBlur, placeholder, isOptional, isReadOnly, hasShowPassword, ref, testId, translate]);
  return (0, _jsxRuntime.jsx)(_Field.Field, {
    ariaDescribedBy: ariaDescribedBy,
    errorMessage: errorMessage,
    errorMessageList: errorMessageList,
    fieldType: "single",
    hasVisibleLabel: true,
    hint: hint,
    id: idOverride,
    isDisabled: isDisabled,
    isFullWidth: isFullWidth,
    isOptional: isOptional,
    label: label,
    renderFieldComponent: renderFieldComponent
  });
});
const MemoizedPasswordField = exports.PasswordField = (0, _react.memo)(PasswordField);
MemoizedPasswordField.displayName = "PasswordField";
//# sourceMappingURL=PasswordField.cjs.map