var $ = require('jquery');
var Assert = require('assert');

/**
* An abstract base class for implementing validator classes.
*
* Don't use this class directly, extend it instead.
*
* @memberof module:libresignage/ui/validator
*/
class BaseValidator {
	/**
	* Construct a BaseValidator object.
	*
	* Classes extending this class should always call the
	* original constructor with eg. 'super(...args);'.
	*
	* @param {object}  settings The validator settings array.
	* @param {string}  message  The invalid input message.
	* @param {boolean} nostyle  Disable input styling.
	*
	* @throws {AssertionError} If the settings parameter is not an object.
	* @throws {AssertionError} If the derived class hasn't overridden validate().
	*/
	constructor(settings, msg, nostyle) {
		Assert.ok(
			settings && settings === Object(settings),
			'Invalid validator settings object.'
		);
		Assert.ok(
			typeof this.validate === 'function',
			'Derived classes must override the function validate().'
		);

		this.settings = settings;
		this.msg = msg;
		this.nostyle = nostyle;
	}

	/**
	* Validate this.settings against an object to make sure all
	* required keys exist in this.settings.
	*
	* @throws {Error} If a required key doesn't exist in this.settings
	*/
	chk_settings(proto) {
		for (let i in proto) {
			let keys = Object.keys(this.settings);
			if (keys.indexOf(proto[i]) == -1) {
				throw new Error(
					"Invalid BaseValidator settings. '${proto[i]}' missing."
				);
			}
		}
	}

	/**
	* Validate an input with this BaseValidator.
	*
	* Any classes overriding the BaseValidator class should
	* override this function to implement the validation routines.
	*
	* @param {jQuery} The element to validate.
	*
	* @return {boolean} True if the input validates as OK, false otherwise.
	*/
	validate(element) {
		return true;
	}

	/**
	* Get the invalid input message of this validator.
	*
	* @return {string} The requested message.
	*/
	get_msg() { return this.msg; }
}
module.exports = BaseValidator;
