var validateHelper = require('../helpers/validate.js')
var joinPath = require('../helpers/joinPath.js')
var transformPathToArray = require('./../helpers/transformPathToArray.js')

function validate (arg) {
  var input = arg.input
  var state = arg.state

  var doValidation = function (path, form, key) {
    var field = form[key]
    var result = validateHelper(form, field.value, field.validations)
    state.merge(path.concat(key), {
      isValid: result.isValid,
      errorMessage: result.isValid ? null : field.errorMessages[result.failedRuleIndex]
    })
  }

  var pathToField = transformPathToArray(input.field)
  var path = pathToField.slice()
  var key = path.pop()
  var form = state.get(path)
  var field = form[key]

  doValidation(path, form, key)

  if (Array.isArray(field.dependents) && field.dependents.length) {
    console.warn('DEPRECATED: Use new field.dependsOn API instead');
    field.dependents.forEach(function (dependent) {
      if (!Array.isArray(dependent) || !dependent.length) {
        console.warn('cerebral-module-forms: dependent path expected to be Array. Check out dependents provided for ' + input.field + '.')
        return
      }
      var path = joinPath(pathToField, dependent)
      var key = path.pop()
      var form = state.get(path)

      doValidation(path, form, key)
    })
  }

  if (Array.isArray(field.dependsOn)) {
    field.dependsOn.forEach(function (stringPath) {
      var path = stringPath.split('.')
      var key = path.pop()
      var form = state.get(path)

      doValidation(path, form, key)
    })
  } else if (field.dependsOn) {
    var path = field.dependsOn.split('.')
    var key = path.pop()
    var form = state.get(path)

    doValidation(path, form, key)
  }
}

module.exports = validate

@@ -1461,33 +1461,42 @@
 ('.')%0A      var 
-k
+dependsOnK
 ey = path.pop()%0A
@@ -1521,32 +1521,199 @@
 tate.get(path)%0A%0A
+      if (!form) %7B%0A        throw new Error('The path %22' + path.join('.')  + '%22 used with %22dependsOn%22 on field %22' + key + '%22 is not correct, please check it')%0A      %7D%0A%0A
       doValidati
@@ -1719,33 +1719,42 @@
 ion(path, form, 
-k
+dependsOnK
 ey)%0A    %7D)%0A  %7D e
@@ -1822,33 +1822,42 @@
 it('.')%0A    var 
-k
+dependsOnK
 ey = path.pop()%0A
@@ -1884,24 +1884,185 @@
 .get(path)%0A%0A
+    if (!form) %7B%0A      throw new Error('The path %22' + path.join('.')  + '%22 used with %22dependsOn%22 on field %22' + key + '%22 is not correct, please check it')%0A    %7D%0A%0A
     doValida
@@ -2074,25 +2074,34 @@
 path, form, 
-k
+dependsOnK
 ey)%0A  %7D%0A%7D%0A%0Am

throw error when path to form is wrong