"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 _react = _interopRequireWildcard(require("react"));

var _Popover = _interopRequireDefault(require("../Popover"));

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

var _estimatePassword = _interopRequireWildcard(require("./estimatePassword"));

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.
const strengthIntents = {
  VeryWeak: 'danger',
  Weak: 'danger',
  Medium: 'warning',
  Strong: 'success',
  VeryStrong: 'success'
};

const PasswordMeter = ({
  value,
  visible,
  onEstimate,
  children,
  ...props
}) => {
  const [result, setResult] = (0, _react.useState)(null);
  (0, _react.useEffect)(() => {
    if (!visible) {
      return;
    }

    Promise.resolve(onEstimate ? onEstimate(value, _estimatePassword.default, _estimatePassword.DEFAULT_RULES) : (0, _estimatePassword.default)(value)).then(result => {
      setResult(result);
    });
  }, [visible, value, onEstimate]);
  return /*#__PURE__*/_react.default.createElement(_Popover.default, (0, _extends2.default)({
    visible: visible && result !== null,
    target: children,
    placement: "bottom-right",
    intent: result ? strengthIntents[result.strength] : undefined,
    canCloseOnOutsideClick: false,
    canCloseOnEscapePress: false
  }, props), result !== null && result !== void 0 && result.strength ? /*#__PURE__*/_react.default.createElement(_Translate.default, {
    component: "div",
    content: "FormFieldPassword.passwordStrength",
    fallback: _enUS.default.passwordStrength,
    params: {
      strength: /*#__PURE__*/_react.default.createElement("b", null, /*#__PURE__*/_react.default.createElement(_Translate.default, {
        content: `FormFieldPassword.strength${result === null || result === void 0 ? void 0 : result.strength}`,
        fallback: _enUS.default[`strength${result === null || result === void 0 ? void 0 : result.strength}`]
      }))
    }
  }) : null, result !== null && result !== void 0 && result.suggestions.length ? /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(_Translate.default, {
    content: "FormFieldPassword.improvePassword",
    fallback: _enUS.default.improvePassword
  }), /*#__PURE__*/_react.default.createElement("br", null), /*#__PURE__*/_react.default.createElement("ul", null, result.suggestions.slice(0, 3).map((suggestion, index) => /*#__PURE__*/_react.default.createElement("li", {
    key: index.toString()
  }, suggestion)))) : /*#__PURE__*/_react.default.createElement(_Translate.default, {
    content: "FormFieldPassword.yourPasswordIsStrong",
    fallback: _enUS.default.yourPasswordIsStrong
  }));
};

var _default = PasswordMeter;
exports.default = _default;