const Validator = (($) => {
    class Validator {
        constructor(config) {
        }
        static check(config) {
            if (arguments.length > 1) {
                let args = [{
                    value: arguments[0],
                    name: arguments[1],
                    rules: arguments[2]
                }]
                config = args;
            }
            return Validator.checkArray(config);
        }
        static checkArray(config) {
            let result = {
                result: true
            }
            if (Array.isArray(config)) {
                for (var i = 0; i < config.length; i++) {
                    let _result = Validator.checkSingle(config[i]);
                    if (_result.result === false) {
                        result = _result;
                        break;
                    }
                }
            }
            return result;
        }
        static checkSingle(param) {
            let { value, name, rules } = param;
            let result = { reusult: true }
            for (var rule in rules) {
                if (Validator.rules[rule]) {
                    let _result = Validator.rules[rule]({
                        value: value,
                        name: name,
                        arg: rules[rule]
                    });
                    if (_result.result === false) {
                        result = _result;
                        break;
                    }
                }
            }
            return result;
        }
    }
    Validator.rules = {
        require: function(param) {
            let { value, name, arg } = param;
            if (arg === true) {
                if (value === undefined || value === null || value === '') {
                    return {
                        result: false,
                        msg: `${name}不能为空`
                    }
                }
            }
            return {
                result: true
            }
        },
        lenRange: function(param) {
            let { value, name, arg } = param;
            let min = parseInt(arg.split('-')[0]);
            let max = parseInt(arg.split('-')[1]);
            let length = value.length;
            if (length > max || length < min) {
                return {
                    result: false,
                    msg: `${name}长度错误，应在${min}到${max}的范围内。`
                }
            } else {
                return {
                    result: true
                }
            }
        },
        minLength: function(param) {
            let { value, name, arg } = param;
            let length = value.length;
            if (length < arg) {
                return {
                    result: false,
                    msg: `${name}的最小长度为${arg}。`
                }
            } else {
                return {
                    result: true
                }
            }
        },
        maxLength: function(param) {
            let { value, name, arg } = param;
            let length = value.length;
            if (length > arg) {
                return {
                    result: false,
                    msg: `${name}的最大长度为${arg}。`
                }
            } else {
                return {
                    result: true
                }
            }
        },
        type: function(param) {
            let { value, name, arg } = param;
            switch (arg) {
                case 'number':
                    if (!/^[0-9]+$/.test(value)) {
                        return {
                            result: false,
                            msg: `${name}不是数字类型`
                        }
                    } else {
                        return {
                            result: true
                        }
                    }
                    break;
                default:
                    return {
                        result: true
                    }
            }
        },
        user: function(param) {
            let { value, name, arg } = param;
            if (arg instanceof RegExp) {
                return Validator.rules.regExp(param);
            } else if (arg instanceof Function) {
                return arg(param);
            }
        },
        regExp: function(param) {
            let { value, name, arg } = param;
            if (!arg.test(value)) {
                return {
                    result: false,
                    msg: `${name}的格式不正确`
                }
            } else {
                return {
                    result: true
                }
            }
        },
        min: function(param) {
            let { value, name, arg } = param;
            if (!/^[0-9]+$/.test(value)) {
                return {
                    result: false,
                    msg: `${name}不是数字类型`
                }
            } else {
                if(parseInt(value, 10) < arg) {
                    return {
                    result: false,
                        msg: `${name}小于${arg}`
                    }
                }else {
                    return {
                        result: true
                    }
                }
            }
        },
        max: function(param) {
            let { value, name, arg } = param;
            if (!/^[0-9]+$/.test(value)) {
                return {
                    result: false,
                    msg: `${name}不是数字类型`
                }
            } else {
                if(parseInt(value, 10) > arg) {
                    return {
                    result: false,
                        msg: `${name}大于${arg}`
                    }
                }else {
                    return {
                        result: true
                    }
                }
            }
        }
    }
    return Validator;
})($);
export default Validator.check;