"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DEFAULT_RULES = void 0;
var _Translate = _interopRequireDefault(require("../Translate"));
var _enUS = _interopRequireDefault(require("./locale/en-US"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const EXCLUSIONS = {
  numbers1: 'numbers3',
  specialChar1: 'specialChar2',
  lettersLowerCase: 'comboUpperAndLower',
  lettersUpperCase: 'comboUpperAndLower'
};
const DEFAULT_RULES = exports.DEFAULT_RULES = [{
  name: 'passwordTooShort',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.passwordTooShort`,
    fallback: _enUS.default.passwordTooShort
  }),
  score(passwd) {
    return passwd.length < 5 ? -1 : 0;
  }
}, {
  name: 'passwordLength',
  score(passwd) {
    if (passwd.length < 5) {
      return 3;
    }
    if (passwd.length > 4 && passwd.length < 8) {
      return 6;
    }
    if (passwd.length > 7 && passwd.length < 16) {
      return 12;
    }
    return 18;
  }
}, {
  name: 'lettersLowerCase',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.lettersLowerCase`,
    fallback: _enUS.default.lettersLowerCase
  }),
  score(passwd) {
    // [verified] at least one lower case letter
    return passwd.match(/[a-z]/) ? 1 : -1;
  }
}, {
  name: 'lettersUpperCase',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.lettersUpperCase`,
    fallback: _enUS.default.lettersUpperCase
  }),
  score(passwd) {
    // [verified] at least one upper case letter
    return passwd.match(/[A-Z]/) ? 5 : -1;
  }
}, {
  name: 'numbers1',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.numbers1`,
    fallback: _enUS.default.numbers1
  }),
  score(passwd) {
    // [verified] at least one number
    return passwd.match(/\d+/) ? 5 : -1;
  }
}, {
  name: 'numbers3',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.numbers3`,
    fallback: _enUS.default.numbers3
  }),
  score(passwd) {
    // [verified] at least three numbers
    return passwd.match(/(.*[0-9].*[0-9].*[0-9])/) ? 5 : -1;
  }
}, {
  name: 'specialChar1',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.specialChar1`,
    fallback: _enUS.default.specialChar1
  }),
  score(passwd) {
    // [verified] at least one special character
    return passwd.match(/[!@#$%^&*?_~]/) ? 5 : -1;
  }
}, {
  name: 'specialChar2',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.specialChar2`,
    fallback: _enUS.default.specialChar2
  }),
  score(passwd) {
    // [verified] at least two special characters
    return passwd.match(/(.*[!@#$%^&*?_~].*[!@#$%^&*?_~])/) ? 5 : -1;
  }
}, {
  name: 'comboUpperAndLower',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.comboUpperAndLower`,
    fallback: _enUS.default.comboUpperAndLower
  }),
  score(passwd) {
    // [verified] both upper and lower case
    return passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) ? 2 : -1;
  }
}, {
  name: 'comboLettersAndNumbers',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.comboLettersAndNumbers`,
    fallback: _enUS.default.comboLettersAndNumbers
  }),
  score(passwd) {
    // [verified] both letters and numbers
    return passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/) ? 2 : -1;
  }
}, {
  name: 'comboLettersNumbersSpecial',
  suggestion: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: `FormFieldPassword.comboLettersNumbersSpecial`,
    fallback: _enUS.default.comboLettersNumbersSpecial
  }),
  score(passwd) {
    // [verified] letters, numbers, and special characters
    return passwd.match(/([a-zA-Z0-9].*[!@#$%^&*?_~])|([!@#$%^&*?_~].*[a-zA-Z0-9])/) ? 2 : -1;
  }
}];
var _default = exports.default = function _default(password) {
  let rules = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_RULES;
  let passwordScore = 0;
  const suggestions = [];
  const skippedRules = new Set();
  rules.forEach(rule => {
    const ruleScore = rule.score(password);
    if (ruleScore >= 0) {
      passwordScore += ruleScore;
      return;
    }
    skippedRules.add(EXCLUSIONS[rule.name]);
    if (!skippedRules.has(rule.name) && rule.suggestion) {
      suggestions.push(rule.suggestion);
    }
  });
  let strength;
  switch (true) {
    case passwordScore < 16:
      strength = 'VeryWeak';
      break;
    case passwordScore < 25:
      strength = 'Weak';
      break;
    case passwordScore < 35:
      strength = 'Medium';
      break;
    case passwordScore < 45:
      strength = 'Strong';
      break;
    default:
      strength = 'VeryStrong';
      break;
  }
  return {
    strength,
    suggestions
  };
};