'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports['default'] = checkPassword;

var _intl = require('./intl');

function checkPassword(passwordPolicy, value, callback, userName) {
  if (passwordPolicy) {
    var check = passwordPolicy.enablePassword,
        minLength = passwordPolicy.minLength,
        maxLength = passwordPolicy.maxLength,
        upcount = passwordPolicy.uppercaseCount,
        spcount = passwordPolicy.specialCharCount,
        lowcount = passwordPolicy.lowercaseCount,
        notEqualsUsername = passwordPolicy.notUsername,
        regexCheck = passwordPolicy.regularExpression;

    if (value && check) {
      var len = 0;
      var rs = '';
      var sp = void 0;
      var up = 0;
      var low = 0;
      for (var i = 0; i < value.length; i += 1) {
        var a = value.charAt(i);
        if (a.match(/[^\x00-\xff]/ig) != null) {
          len += 2;
        } else {
          len += 1;
        }
      }
      var pattern = new RegExp('[-~`@#$%^&*_=+|/()<>,.;:!]');
      for (var _i = 0; _i < value.length; _i += 1) {
        rs += value.substr(_i, 1).replace(pattern, '');
        sp = value.length - rs.length;
      }
      if (/[A-Z]/i.test(value)) {
        var ups = value.match(/[A-Z]/g);
        up = ups ? ups.length : 0;
      }
      if (/[a-z]/i.test(value)) {
        var lows = value.match(/[a-z]/g);
        low = lows ? lows.length : 0;
      }
      if (minLength && len < minLength) {
        callback((0, _intl.getMessage)('\u5BC6\u7801\u957F\u5EA6\u81F3\u5C11\u4E3A' + minLength, 'Password length is at least ' + minLength));
        return;
      }
      if (maxLength && len > maxLength) {
        callback((0, _intl.getMessage)('\u5BC6\u7801\u957F\u5EA6\u6700\u591A\u4E3A' + maxLength, 'Password length is upto ' + maxLength));
        return;
      }
      if (upcount && up < upcount) {
        callback((0, _intl.getMessage)('\u5927\u5199\u5B57\u6BCD\u81F3\u5C11\u4E3A' + upcount, 'At least for a capital letter ' + upcount));
        return;
      }
      if (lowcount && low < lowcount) {
        callback((0, _intl.getMessage)('\u5C0F\u5199\u5B57\u6BCD\u81F3\u5C11\u4E3A' + lowcount, 'At least for a lower-case letters ' + lowcount));
        return;
      }
      if (notEqualsUsername && value === userName) {
        callback((0, _intl.getMessage)('密码不能与账号相同', 'password can not equal with the userName'));
        return;
      }
      if (regexCheck) {
        var regex = new RegExp(regexCheck);
        if (regex.test(value)) {
          callback();
        } else {
          callback((0, _intl.getMessage)('正则不匹配', 'can not test regex'));
        }
      }
      if (spcount && sp < spcount) {
        callback((0, _intl.getMessage)('\u7279\u6B8A\u5B57\u7B26\u81F3\u5C11\u4E3A' + spcount, 'At least for special characters ' + spcount));
      } else {
        callback();
      }
    } else {
      callback();
    }
  } else {
    callback();
  }
}