"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _Popover = _interopRequireDefault(require("../Popover"));
var _Translate = _interopRequireDefault(require("../Translate"));
var _estimatePassword = _interopRequireWildcard(require("./estimatePassword"));
var _enUS = _interopRequireDefault(require("./locale/en-US"));
var _jsxRuntime = require("react/jsx-runtime");
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-2023. Plesk International GmbH. All rights reserved.

const strengthIntents = {
  VeryWeak: 'danger',
  Weak: 'danger',
  Medium: 'warning',
  Strong: 'success',
  VeryStrong: 'success'
};
const PasswordMeter = _ref => {
  let {
    value,
    visible,
    onEstimate,
    children,
    ...props
  } = _ref;
  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__*/(0, _jsxRuntime.jsxs)(_Popover.default, {
    visible: visible && result !== null,
    placement: "bottom-right",
    intent: result ? strengthIntents[result.strength] : undefined,
    canCloseOnOutsideClick: false,
    canCloseOnEscapePress: false,
    ...props,
    target: children,
    children: [result?.strength ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      component: "div",
      content: "FormFieldPassword.passwordStrength",
      fallback: _enUS.default.passwordStrength,
      params: {
        strength: /*#__PURE__*/(0, _jsxRuntime.jsx)("b", {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
            content: `FormFieldPassword.strength${result?.strength}`,
            fallback: _enUS.default[`strength${result?.strength}`]
          })
        })
      }
    }) : null, result?.suggestions.length ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
        content: "FormFieldPassword.improvePassword",
        fallback: _enUS.default.improvePassword
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("br", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
        children: result.suggestions.slice(0, 3).map((suggestion, index) =>
        /*#__PURE__*/
        // eslint-disable-next-line react/no-array-index-key
        (0, _jsxRuntime.jsx)("li", {
          children: suggestion
        }, index.toString()))
      })]
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "FormFieldPassword.yourPasswordIsStrong",
      fallback: _enUS.default.yourPasswordIsStrong
    })]
  });
};
var _default = exports.default = PasswordMeter;