'use strict';

function minValidator(min) {
    return {
        validate: (value) => {
            const valueAsNum = Number(value);
            const minAsNum = Number(min);
            if (isNaN(valueAsNum) || isNaN(minAsNum)) {
                return true;
            }
            return (valueAsNum < minAsNum) ? { min: { valid: false } } : undefined;
        }
    };
}

function maxValidator(max) {
    return {
        validate: (value) => {
            const valueAsNum = Number(value);
            const maxAsNum = Number(max);
            if (isNaN(valueAsNum) || isNaN(maxAsNum)) {
                return true;
            }
            return (valueAsNum > maxAsNum) ? { max: { valid: false } } : undefined;
        }
    };
}

var ValidatorsName;
(function (ValidatorsName) {
    ValidatorsName["min"] = "min";
    ValidatorsName["max"] = "max";
})(ValidatorsName || (ValidatorsName = {}));
function getValidator(list) {
    return (list || [])
        .map(v => {
        if (typeof v === 'string') {
            return validatorFactory(v, null);
        }
        else {
            return validatorFactory(v.name, v.options);
        }
    })
        .reduce(combineValidator, defaultValidator);
}
function combineValidator(v1, v2) {
    let combined;
    combined = { validate: (x) => {
            const res1 = v1.validate(x);
            const res2 = v2.validate(x);
            return Object.assign({}, res1, res2);
        } };
    return combined;
}
function validatorFactory(name, options) {
    switch (name) {
        case (ValidatorsName.max):
            return maxValidator(options.max);
        case (ValidatorsName.min):
            return minValidator(options.min);
        default:
            return defaultValidator;
    }
}
const defaultValidator = {
    validate: (_x) => null
};

exports.getValidator = getValidator;
