"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _Button = _interopRequireDefault(require("../Button"));

var _FormField = _interopRequireDefault(require("../FormField"));

var _Input = _interopRequireDefault(require("../Input"));

var _Translate = _interopRequireDefault(require("../Translate"));

var _PasswordMeter = _interopRequireDefault(require("./PasswordMeter"));

var _generatePassword = _interopRequireDefault(require("./generatePassword"));

var _enUS = _interopRequireDefault(require("./locale/en-US"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// Copyright 1999-2022. Plesk International GmbH. All rights reserved.

/**
 * `FormFieldPassword` component provides secure way to enter a password.
 * @since 0.0.58
 */
class FormFieldPassword extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      visible: false,
      passwordMeterVisible: false
    });
    (0, _defineProperty2.default)(this, "targetRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "handleToggleClick", () => {
      this.setState(({
        visible
      }) => ({
        visible: !visible
      }));
    });
    (0, _defineProperty2.default)(this, "handleClosePasswordMeter", () => {
      this.setState({
        passwordMeterVisible: false
      });
    });
    (0, _defineProperty2.default)(this, "handleBlur", this.handleClosePasswordMeter);
  }

  renderGenerateButton({
    setValue,
    isDisabled
  }) {
    const {
      baseClassName,
      generateButton
    } = this.props;
    const generateButtonProps = {
      className: `${baseClassName}__button--generate`,
      onClick: () => setValue((0, _generatePassword.default)()),
      tooltip: /*#__PURE__*/_react.default.createElement(_Translate.default, {
        content: "FormFieldPassword.generateButtonHint",
        fallback: _enUS.default.generateButtonHint
      }),
      disabled: isDisabled(),
      children: /*#__PURE__*/_react.default.createElement(_Translate.default, {
        content: "FormFieldPassword.generateButton",
        fallback: _enUS.default.generateButton
      })
    };
    let button;

    if (typeof generateButton === 'function') {
      button = generateButton(generateButtonProps);
    } else {
      button = /*#__PURE__*/_react.default.createElement(_Button.default, generateButtonProps);
    }

    return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, ` `, button);
  }

  render() {
    const {
      baseClassName,
      className,
      hideShowButton,
      generateButton,
      hideGenerateButton,
      hidePasswordMeter,
      passwordMeterProps,
      size,
      autoFocus,
      autoComplete,
      ...props
    } = this.props;
    const {
      visible,
      passwordMeterVisible
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_FormField.default, (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, className)
    }, props), ({
      getId,
      getName,
      getValue,
      setValue,
      isDisabled
    }) => {
      const value = getValue('');

      const input = /*#__PURE__*/_react.default.createElement(_Input.default, {
        id: getId(),
        name: getName(),
        type: visible ? 'text' : 'password',
        value: value,
        onChange: e => {
          setValue(e.target.value);
          this.setState({
            passwordMeterVisible: true
          });
        },
        onBlur: this.handleBlur,
        autoFocus: autoFocus,
        autoComplete: autoComplete,
        className: `${baseClassName}__input`,
        size: size,
        suffix: hideShowButton ? null : /*#__PURE__*/_react.default.createElement(_Button.default, {
          className: `${baseClassName}__button--show`,
          onClick: this.handleToggleClick,
          icon: visible ? 'visible' : 'invisible',
          tooltip: visible ? /*#__PURE__*/_react.default.createElement(_Translate.default, {
            content: "FormFieldPassword.hidePassword",
            fallback: _enUS.default.hidePassword
          }) : /*#__PURE__*/_react.default.createElement(_Translate.default, {
            content: "FormFieldPassword.showPassword",
            fallback: _enUS.default.showPassword
          }),
          ghost: true,
          disabled: isDisabled()
        }),
        disabled: isDisabled(),
        rootProps: {
          ref: this.targetRef
        }
      });

      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)(`${baseClassName}__control`, `${baseClassName}__control--${size}`)
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)(`${baseClassName}__field`, `${baseClassName}__field--${size}`)
      }, hidePasswordMeter ? input : /*#__PURE__*/_react.default.createElement(_PasswordMeter.default, (0, _extends2.default)({
        value: value,
        targetRef: this.targetRef,
        visible: passwordMeterVisible && value !== '',
        onClose: this.handleClosePasswordMeter
      }, passwordMeterProps), input)), hideGenerateButton ? null : this.renderGenerateButton({
        setValue,
        isDisabled
      }));
    });
  }

}

(0, _defineProperty2.default)(FormFieldPassword, "defaultProps", {
  hideShowButton: false,
  generateButton: undefined,
  hideGenerateButton: false,
  hidePasswordMeter: false,
  passwordMeterProps: {},
  size: 'md',
  autoFocus: undefined,
  autoComplete: undefined,
  className: undefined,
  baseClassName: `${_constants.CLS_PREFIX}form-field-password`
});
var _default = FormFieldPassword;
exports.default = _default;