function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { useMemo } from 'react';
var DEFAULT_MESSAGES = {
  required: 'Password is required',
  minLength: 'Password must be at least {length} characters long',
  requireUppercase: 'at least one uppercase letter',
  requireLowercase: 'at least one lowercase letter',
  requireNumber: 'at least one number',
  requireSpecialCharacters: 'at least one special character',
  summary: 'Password must contain: {requirements}'
};
var formatMessage = function formatMessage(template, variables) {
  return Object.keys(variables).reduce(function (message, key) {
    var _variables$key;
    return message.replace(new RegExp("{".concat(key, "}"), 'g'), String((_variables$key = variables[key]) !== null && _variables$key !== void 0 ? _variables$key : ''));
  }, template);
};
var resolveMessage = function resolveMessage(key, translate, messageKeys) {
  var messageKey = messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys[key];
  if (messageKey && translate) {
    var translated = translate(messageKey);
    if (translated) {
      return translated;
    }
  }
  return DEFAULT_MESSAGES[key];
};
export var usePasswordValidationRules = function usePasswordValidationRules(passwordRules, options) {
  var translate = options === null || options === void 0 ? void 0 : options.translate;
  var messageKeys = options === null || options === void 0 ? void 0 : options.messageKeys;
  var fallbackMinLength = options === null || options === void 0 ? void 0 : options.fallbackMinLength;
  return useMemo(function () {
    var rules = [{
      required: true,
      message: resolveMessage('required', translate, messageKeys)
    }];
    var effectiveRules = _objectSpread({}, passwordRules);
    if (effectiveRules.minLength == null && fallbackMinLength) {
      effectiveRules.minLength = fallbackMinLength;
    }
    if (effectiveRules.minLength) {
      var rawMessage = resolveMessage('minLength', translate, messageKeys);
      rules.push({
        min: effectiveRules.minLength,
        message: formatMessage(rawMessage, {
          length: effectiveRules.minLength
        })
      });
    }
    if (effectiveRules.requireUppercase || effectiveRules.requireLowercase || effectiveRules.requireNumber || effectiveRules.requireSpecialCharacters) {
      rules.push({
        validator: function validator(_rule, value) {
          if (!value) {
            return Promise.resolve();
          }
          var requirements = [];
          if (effectiveRules.requireUppercase && !/[A-Z]/.test(value)) {
            requirements.push(resolveMessage('requireUppercase', translate, messageKeys));
          }
          if (effectiveRules.requireLowercase && !/[a-z]/.test(value)) {
            requirements.push(resolveMessage('requireLowercase', translate, messageKeys));
          }
          if (effectiveRules.requireNumber && !/\d/.test(value)) {
            requirements.push(resolveMessage('requireNumber', translate, messageKeys));
          }
          if (effectiveRules.requireSpecialCharacters && !/[!@#$%^&*]/.test(value)) {
            requirements.push(resolveMessage('requireSpecialCharacters', translate, messageKeys));
          }
          if (requirements.length > 0) {
            var template = resolveMessage('summary', translate, messageKeys);
            return Promise.reject(new Error(formatMessage(template, {
              requirements: requirements.join(', ')
            })));
          }
          return Promise.resolve();
        }
      });
    }
    return rules;
  }, [fallbackMinLength, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireLowercase, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireNumber, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireSpecialCharacters, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireUppercase, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.minLength, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.required, messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.summary, passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.minLength, passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireLowercase, passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireNumber, passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireSpecialCharacters, passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireUppercase, translate]);
};
export default usePasswordValidationRules;