'use strict';

import {IValidationResult} from './interfaces';

export class ValidationResult implements IValidationResult {

  protected _errors: any[];

  constructor(errors) {
    this._errors = errors;
  }
  
  get isValid(): boolean {
    return !this._errors.length;
  }
  
  get errors() {
    return this._errors;
  }
  
  getInvalidProperties() {
    return this._errors.reduce((properties, error) => {
      if(properties.indexOf(error.property) === -1) {
        properties.push(error.property);
      }
      return properties;
    }, []);
  }
  
  getPropertyErrors(propertyName: string) {
    return this._errors.reduce((errors, error) => {
      if(error && error.property === propertyName) {
        errors.push(error);
      }
      return errors;
    }, []);
  }
  
  toJSON() {
    return {
      isValid: this.isValid,
      errors: this.errors
    };
  }
}