var BaseValidator = require('libresignage/ui/validator/BaseValidator');

/**
* @typedef {object} StrValidatorSettings
*
* @property {number} min   The minimum string length.
* @property {number} max   The maximum string length.
* @property {RegExp} regex The regex to match.
*
* @memberof module:libresignage/ui/validator
* @alias    StrValidatorSettings
*/

/**
* Class for validating input strings.
*
* @memberof module:libresignage/ui/validator
*/
class StrValidator extends BaseValidator {
	/**
	* Construct a new Stralidator object.
	*
	* @param {StralidatorSettings} settings
	* @param {string}              msg
	* @param {boolean}             nostyle
	*
	* @see {@link module:libresignage/ui/validator.BaseValidator} Parent class.
	*/
	constructor(...args) {
		super(...args);
		this.chk_settings(['min', 'max', 'regex']);
	}

	validate(element) {
		let val = null;
		let a, b, c, tmp;
		let {min, max, regex} = this.settings;

		val = element.value;
		a = (min == null || val.length >= min);
		b = (max == null || val.length <= max);

		if (regex == null) {
			c = true;
		} else {
			tmp = val.match(regex);
			c = (tmp && tmp[0].length == val.length);
		}
		return a && b && c;
	}
}
module.exports = StrValidator;
