{"size": 2499, "ext": "js", "max_stars_repo_path": "src/validators/foreignbusinesscontact.js", "max_stars_repo_name": "gaybro8777/e-QIP-prototype", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/validators/foreignbusinesscontact.js", "max_issues_repo_name": "gaybro8777/e-QIP-prototype", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/validators/foreignbusinesscontact.js", "max_forks_repo_name": "gaybro8777/e-QIP-prototype", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5135135135, "max_line_length": 80, "alphanum_fraction": 0.6546618647}
import NameValidator from './name'
import LocationValidator from './location'
import {
  validAccordion,
  validGenericTextfield,
  validDateField,
  BranchCollection
} from './helpers'

export default class ForeignBusinessContactValidator {
  constructor(data = {}) {
    this.hasForeignContact = (data.HasForeignContact || {}).value
    this.list = data.List || {}
  }

  validList() {
    if (this.hasForeignContact === 'No') {
      return true
    }

    return validAccordion(this.list, item => {
      return new ContactValidator(item).isValid()
    })
  }

  isValid() {
    return this.validList()
  }
}

export class ContactValidator {
  constructor(data = {}) {
    this.name = data.Name
    this.location = data.Location
    this.date = data.Date
    this.governments = data.Governments
    this.establishment = data.Establishment
    this.representatives = data.Representatives
    this.purpose = data.Purpose
    this.subsequentContacts = data.SubsequentContacts
  }

  validName() {
    return !!this.name && new NameValidator(this.name).isValid()
  }

  validLocation() {
    return !!this.location && new LocationValidator(this.location).isValid()
  }

  validDate() {
    return !!this.date && validDateField(this.date)
  }

  validGovernments() {
    return (
      !!this.governments &&
      !!this.governments.value &&
      this.governments.value.length > 0
    )
  }

  validEstablishment() {
    return !!this.establishment && validGenericTextfield(this.establishment)
  }

  validRepresentatives() {
    return !!this.representatives && validGenericTextfield(this.representatives)
  }

  validPurpose() {
    return !!this.purpose && validGenericTextfield(this.purpose)
  }

  validSubsequentContacts() {
    if (!this.subsequentContacts) {
      return false
    }

    const branchValidator = new BranchCollection(this.subsequentContacts)
    if (!branchValidator.validKeyValues()) {
      return false
    }

    if (branchValidator.hasNo()) {
      return true
    }

    return branchValidator.each(item => {
      return (
        validGenericTextfield(item.Subsequent) &&
        validDateField(item.Recent) &&
        validGenericTextfield(item.Future)
      )
    })
  }

  isValid() {
    return (
      this.validName() &&
      this.validLocation() &&
      this.validDate() &&
      this.validGovernments() &&
      this.validEstablishment() &&
      this.validRepresentatives() &&
      this.validPurpose() &&
      this.validSubsequentContacts()
    )
  }
}
