"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.successContentValidate = exports.tcpValidate = exports.httpsValidate = exports.httpValidate = exports.removeUrlSpace = exports.validatePort = exports.isPassForSpecification = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _validator = _interopRequireDefault(require("validator"));

/**
 ***********************************************************
 *
 *   校验相关公共方法
 *   使用第三方校验库 validator 详细使用参考 [官方文档](https://github.com/validatorjs/validator.js)
 *   @see {@link  https://github.com/validatorjs/validator.js|查看官方文档}
 *
 ********************************************************** */

/**
 * ---
 * 数字类校验，系统限制小数点前9位，小数点后两位，正数<br/>
 * 详单功能Table高级过滤器-比较过滤输入框校验 isPassForSpecification
 *  *
 * @returns boolean 校验结果
 * @function isPassForSpecification
 * @static

 * @returns {boolean}
 * @example 01 校验通过
 * --------------------------------------------------------------------------
 * import { validator } from '@riil-frontend/utils';
 * validator.isPassForSpecification('1564.33');
 *
 * // result
 * @example 02 校验不通过
 * --------------------------------------------------------------------------
 * import { validator } from '@riil-frontend/utils';
 * validator.isPassForSpecification('aaaa');
 *
 * // result
 * false
 */
var isPassForSpecification = function isPassForSpecification(inputValue) {
  if (inputValue === '.' || inputValue === '') {
    return true;
  }

  if (_validator.default.isFloat(inputValue) && !inputValue.includes('+') && !inputValue.includes('-')) {
    var integerSuccess = true;
    var decimalsSuccess = true;

    var _inputValue$split = inputValue.split('.'),
        _inputValue$split2 = (0, _slicedToArray2.default)(_inputValue$split, 2),
        integerStr = _inputValue$split2[0],
        decimalsStr = _inputValue$split2[1];

    if (integerStr && integerStr.length > 9) {
      integerSuccess = false;
    }

    if (decimalsStr && decimalsStr.length > 2) {
      decimalsSuccess = false;
    }

    return integerSuccess && decimalsSuccess;
  }

  return false;
};
/**
 * 端口验证
 *
 * @param rule  ant form rule
 * @param value 端口值可以为数组，将分别校验
 * @param callback ant form callback
 * @param source 要验证的对象（必填）。
 * @param options 一个对象，描述验证的处理选项（可选）。
 * @function validatePort
 * @static
 * @returns {*}
 */


exports.isPassForSpecification = isPassForSpecification;

var validatePort = function validatePort(rule, value, callback, source, options) {
  var values = Array.isArray(value) ? value : [value];

  for (var i = 0; i < values.length; i++) {
    var item = values[i];

    if (!isNull(item) && !_validator.default.isPort(item)) {
      // 布尔值，是否禁止显示有关无效值的内部警告。
      options.suppressWarning = true;
      callback('不是合法的端口');
      return;
    }
  }

  return callback();
};
/**
 * 去除url中的空格
 * @param value url
 * @function removeUrlSpace
 * @static

 * @returns {*|void|string}
 */


exports.validatePort = validatePort;

var removeUrlSpace = function removeUrlSpace(value) {
  return value.replace(/\s*/g, '');
};
/**
 * http Urls验证
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 * @static

 * @function httpValidate
 */


exports.removeUrlSpace = removeUrlSpace;

var httpValidate = function httpValidate(rule, value, callback) {
  var errorString = '';

  if (value.length > 10) {
    return callback("\u6700\u591A\u652F\u630110\u4E2A\u5730\u5740\uFF0C\u5F53\u524D" + value.length + "\u4E2A");
  }

  if (value.length === 0) {
    return callback('请输入地址');
  }

  for (var i = 0; i < value.length; i++) {
    var item = value[i];
    item = removeUrlSpace(item);

    if (!item.startsWith('http://')) {
      errorString = '包含不符合http开头的url';
    } else if (_validator.default.isURL(item)) {
      if (item.length > 256) {
        errorString = 'url地址长度，不能超过256个汉字或字符';
      }
    } else {
      errorString = '不符合url格式';
    }

    if (errorString != '') {
      break;
    }
  }

  if (errorString != '') {
    return callback(errorString);
  }

  return callback();
};
/**
 * https Urls验证
 * @param rule
 * @param value
 * @param callback
 * @function httpsValidate
 * @static

 * @returns {*}
 */


exports.httpValidate = httpValidate;

var httpsValidate = function httpsValidate(rule, value, callback) {
  var errorString = '';

  if (value.length > 10) {
    return callback("\u6700\u591A\u652F\u630110\u4E2A\u5730\u5740\uFF0C\u5F53\u524D" + value.length + "\u4E2A");
  }

  if (value.length === 0) {
    return callback('请输入地址');
  }

  for (var i = 0; i < value.length; i++) {
    var item = value[i];
    item = removeUrlSpace(item);

    if (!item.startsWith('https://')) {
      errorString = '包含不符合https开头的url';
    } else if (_validator.default.isURL(item)) {
      if (item.length > 256) {
        errorString = 'url地址长度，不能超过256个汉字或字符';
      }
    } else {
      errorString = '不符合url格式';
    }

    if (errorString !== '') {
      break;
    }
  }

  if (errorString !== '') {
    return callback(errorString);
  }

  return callback();
};
/**
 * ip+端口验证
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 * @static

 * @function tcpValidate
 */


exports.httpsValidate = httpsValidate;

var tcpValidate = function tcpValidate(rule, value, callback) {
  if (value.length > 10) {
    return callback("\u6700\u591A\u652F\u630110\u4E2A\u5730\u5740\uFF0C\u5F53\u524D" + value.length + "\u4E2A");
  }

  if (value.length === 0) {
    return callback('请输入地址');
  }

  var isPass = true;
  var pattIp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\:([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$/;

  for (var i = 0; i < value.length; i++) {
    var item = value[i];

    if (!pattIp.test(item)) {
      isPass = false;
      break;
    }
  }

  if (!isPass) {
    return callback('包含不符合IP+端口的地址，且端口范围应为0-65535');
  }

  return callback();
};
/**
 * 录像文本关键字验证
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 * @function successContentValidate
 * @static
 */


exports.tcpValidate = tcpValidate;

var successContentValidate = function successContentValidate(rule, value, callback) {
  var errorString = '';

  if (value.length > 10) {
    return callback("\u6700\u591A\u652F\u630110\u7EC4\uFF0C\u5F53\u524D" + value.length + "\u7EC4");
  }

  for (var i = 0; i < value.length; i++) {
    var item = value[i];

    if (item.length > 256) {
      errorString = '字符长度不能超过256个汉字或字符';
    }

    if (errorString !== '') {
      break;
    }
  }

  if (errorString !== '') {
    return callback(errorString);
  }

  return callback();
};

exports.successContentValidate = successContentValidate;