export default class WsValidatorService {
  static $inject = ['$window'];

  constructor($window) {
    this.$window = $window;
  }

  clearErrors = function clearErrors(obj) {
    if (obj && obj.errors) {
      delete obj.errors;
    }
  }

  attachErrorForField = function attachErrorForField(obj, field, message) {
    obj.errors = {};
    obj.errors[field] = [message];
  }

  attachErrorsFor = function attachErrorsFor(obj, data) {
    this.$window.scrollTo(0, 0);
    obj.errors = data.data.errors;
  }

  hasErrorsFor = function hasErrorsFor(obj, key) {
    return Boolean(obj && obj.errors && obj.errors[key] && obj.errors[key].length > 0);
  }

  getErrorsFor = function getErrorsFor(obj, key) {
    if (this.hasErrorsFor(obj, key)) {
      return obj.errors[key][0];
    }
    return '';
  }
}
