var defaults = require('lodash.defaultsdeep');
var libphonenumber = require('google-libphonenumber');
var phoneNumberUtil = libphonenumber.PhoneNumberUtil.getInstance();
var React = require('react');

module.exports = {};

module.exports.validations = {
    notEquals: function (values, value, neq) {
        return value !== neq;
    },
    notEqualsField: function (values, value, field) {
        return value !== values[field];
    },
    isPhone: function (values, value) {
        if (typeof value === 'undefined') return true;
        if (value && value.national_number === '+') return true;
        try {
            var parsed = phoneNumberUtil.parse(value.national_number, value.country_code.iso2);
        } catch (err) {
            return false;
        }
        return phoneNumberUtil.isValidNumber(parsed);
    }
};

module.exports.validationHOCFactory = function (defaultValidationErrors) {
    return function (Component) {
        var ValidatedComponent = React.createClass({
            render: function () {
                var validationErrors = defaults(
                    this.props.validationErrors,
                    defaultValidationErrors
                );
                return (
                    <Component {...this.props} validationErrors={validationErrors} />
                );
            }
        });
        return ValidatedComponent;
    };
};

module.exports.defaultValidationHOC = module.exports.validationHOCFactory({
    isDefaultRequiredValue: 'This field is required'
});
