var Wick = window.Wick || {};

Wick.Validation = {
  el: {
    $form: $('form'),
    fieldRow: '.form-row',
    validationErrorClass: 'form-row_validation-error',
    fieldError: '.form-row__error',
  },
  displayValidationError(element, errorMsg) {
    const isElementHasError = !$(element)
      .closest(Wick.Validation.el.fieldRow)
      .hasClass(Wick.Validation.el.validationErrorClass);

    if (isElementHasError) {
      $(element)
        .closest(Wick.Validation.el.fieldRow)
        .addClass(Wick.Validation.el.validationErrorClass)
        .append(`<div class="form-row__error">${errorMsg}</div>`);
    }
  },
  removeValidationError(element) {
    $(element).closest(Wick.Validation.el.fieldRow)
      .removeClass(Wick.Validation.el.validationErrorClass)
      .find(Wick.Validation.el.fieldError)
      .remove();
  },
  isValidFieldSize(value) {
    return value.length > 0 && value.length < 255;
  },
  isValidInputTextField(value) {
    const regExp = new RegExp("^[\\w ()/&.,'-]+$|^$");
    return Wick.Validation.isValidFieldSize(value) && regExp.test(value);
  },
  isValidEmail (value) {
    const ipAddressPattern = "(?:(?:25[0-5]|2[0-4][\\d]|[01]?[\\d][\\d]?)\\.){3}(?:25[0-5]|2[0-4][\\d]|[01]?[\\d][\\d]?)",
      emailPattern = new RegExp("^(?=.{1,64}@)[\\w\\+-]+(\\.[\\w\\+-]+)*@(([A-Za-z\\d]+[A-Za-z\\d-]*\\.){1,3}[A-Za-z]{2,16}|" + ipAddressPattern + ")$"),
      charactersLimit = 254,
      isCharactersLimitReached = value.length <= charactersLimit;
    return emailPattern.test(value) && isCharactersLimitReached;
  },
}

$(document).ready(function () {
  if (!Wick.Validation.el.$form.length) {
    return;
  }
});
