import Decimal from 'big.js';
import isNil from '../../utils/isNil';
var EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
function isEmptyInputValue(value) {
    return isNil(value) || value.length === 0;
}
export var SYMBOL_REQUIRED = Symbol('required');
export function markForRequired(validator) {
    validator.$$id = SYMBOL_REQUIRED;
}
export function isRequiredValidator(validator) {
    return validator.$$id === SYMBOL_REQUIRED;
}
export function min(limit, message) {
    return function min(value) {
        if (isEmptyInputValue(value)) {
            return null;
        }
        try {
            var decimal = new Decimal(value);
            if (decimal.lt(limit)) {
                return {
                    name: 'min',
                    actual: value,
                    limit: limit,
                    message: message,
                };
            }
        }
        catch (error) {
            return {
                name: 'min',
                actual: value,
                limit: limit,
                message: message,
            };
        }
        return null;
    };
}
export function max(limit, message) {
    return function max(value) {
        if (isEmptyInputValue(value)) {
            return null;
        }
        try {
            var decimal = new Decimal(value);
            if (decimal.gt(limit)) {
                return {
                    name: 'max',
                    actual: value,
                    limit: limit,
                    message: message,
                };
            }
        }
        catch (error) {
            return {
                name: 'max',
                actual: value,
                limit: limit,
                message: message,
            };
        }
        return null;
    };
}
export function required(message) {
    function required(input) {
        return isEmptyInputValue(input)
            ? {
                name: 'required',
                message: message,
                actual: input,
            }
            : null;
    }
    required.$$id = SYMBOL_REQUIRED;
    return required;
}
export function requiredTrue(message) {
    function requiredTrue(input) {
        return input === true
            ? null
            : {
                name: 'requiredTrue',
                message: message,
                expect: true,
                actual: input,
            };
    }
    return requiredTrue;
}
export function email(message) {
    function email(input) {
        return EMAIL_REGEXP.test(input)
            ? null
            : {
                name: 'email',
                message: message,
                actual: input,
            };
    }
    return email;
}
export function minLength(length, message) {
    function minLength(input) {
        return input.length < length
            ? {
                name: 'minLength',
                message: message,
                actual: input,
                limit: length,
            }
            : null;
    }
    return minLength;
}
export function maxLength(length, message) {
    function maxLength(input) {
        return input.length > length
            ? {
                name: 'maxLength',
                message: message,
                actual: input,
                limit: length,
            }
            : null;
    }
    return maxLength;
}
export function pattern(regexp, message) {
    function pattern(input) {
        return regexp.test(input)
            ? null
            : {
                name: 'pattern',
                message: message,
                actual: input,
                pattern: regexp,
            };
    }
    return pattern;
}
