import _InputBase from "@mui/material/InputBase";
import _InputAdornment from "@mui/material/InputAdornment";
import _IconButton from "@mui/material/IconButton";
/*!
 * 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, useState } from "react";
import { Field } from "./Field.js";
import { useTranslation } from "./i18n.generated/i18n.js";
import { HideIcon, ShowIcon } from "./icons.generated/index.js";
import { getControlState, useInputValues } from "./inputUtils.js";
import { jsx as _jsx } from "react/jsx-runtime";
const PasswordField = 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
  } = useTranslation();
  const [inputType, setInputType] = useState("password");
  const togglePasswordVisibility = useCallback(() => {
    setInputType(inputType => inputType === "password" ? "text" : "password");
  }, []);
  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: inputType === "password" ? autoCompleteType : "off",
    autoFocus: hasInitialFocus,
    endAdornment: hasShowPassword && _jsx(_InputAdornment, {
      position: "end",
      children: _jsx(_IconButton, {
        "aria-controls": id,
        "aria-label": t("passwordfield.icon.label.show"),
        "aria-pressed": inputType === "text",
        onClick: togglePasswordVisibility,
        children: inputType === "password" ? _jsx(ShowIcon, {}) : _jsx(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 _jsx(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 = memo(PasswordField);
MemoizedPasswordField.displayName = "PasswordField";
export { MemoizedPasswordField as PasswordField };
//# sourceMappingURL=PasswordField.js.map