import Client from './client'

/*
function setObjByPath (obj, path, val) {
  if (!obj) return

  let pathes = path.split('.')
  if (pathes.length === 1) {
    obj[path] = val
  } else {
    setObjByPath(obj[pathes.shift()], pathes.join('.'), val)
  }
}
*/

function buildApiMudule (api) {
  let state = function state() {
    let parameters = api.getDefaultParameters()
    let result = api.getDefaultResponse()
    return {
      parameters,
      result,
      parametersError: {},
      resultError: {}
    }
  }

  let title = api.title

  let actions = {
    [title + '_validateParameters']: function (context, path, val) {
      api.validateParameters(path, val).catch(function (error) {
        context.commit('updateParametesErrorProp', path, error)
      })
    },
    [title + '_validateResult']: function (context, path, val) {
      api.validateResponse(path, val).catch(function (error) {
        context.commit('updateResultErrorProp', path, error)
      })
    },
    [title]: function (context) {
      api.send(context.state.parameters).catch(function (error) {
        if (error.type === 'parameters') {
          context.commit('updateParametersError', error)
        } else if (error.type === 'response') {
          context.commit('updateResultError', error)
        }
      })
    }
  }

  let mutations = {
    updateParametesErrorProp (state, path, error) {
      state.parametersError[path] = error
    },
    updateResultErrorProp (state, path, error) {
      state.ResultError[path] = error
    },
    updateParametersError (state, error) {
      state.parametersError = error
    },
    updateResultError (state, error) {
      state.ResultError = error
    }
  }

  let watch = {
    parameters (context, val, oldVal, path) {
      context.dispatch(title + '_validateParameters', path.relative, val)
    },
    result (context, val, oldVal, path) {
      context.dispatch(title + '_validateResult', path.relative, val)
    }
  }

  return {
    state,
    actions,
    mutations,
    watch
  }
}

export default {
  install: function (modello) {
    modello.on('init', function (modelDesc) {
      if (modelDesc.hyperApi) {
        modelDesc.hyperApi.forEach(api => {
          if (Client[api]) {
            modelDesc.mixins[api] = buildApiMudule(Client[api])
          }
        })
      }
    })
  }
}

@@ -717,32 +717,33 @@
 ('updateParamete
+r
 sErrorProp', pat
@@ -1009,24 +1009,31 @@
 xt) %7B%0A      
+return 
 api.send(con
@@ -1055,16 +1055,97 @@
 meters).
+then(function (result) %7B%0A        context.commit('updateResult', result)%0A      %7D).
 catch(fu
@@ -1169,18 +1169,22 @@
         
-if
+switch
  (error.
@@ -1187,20 +1187,34 @@
 ror.type
- ===
+) %7B%0A          case
  'parame
@@ -1218,20 +1218,20 @@
 ameters'
-) %7B%0A
+:%0A  
         
@@ -1264,32 +1264,38 @@
 rametersError', 
+error.
 error)%0A        %7D
@@ -1297,33 +1297,32 @@
     
-%7D else if (error.type ===
+    break%0A          case
  're
@@ -1328,20 +1328,20 @@
 esponse'
-) %7B%0A
+:%0A  
         
@@ -1378,23 +1378,146 @@
 Error', 
+error.
 error)%0A
+            break%0A          case 'request':%0A            throw error.error%0A          default:%0A            throw error%0A
         
@@ -1580,26 +1580,23 @@
 mete
+r
 sError
-Prop
  (state,
  pat
@@ -1583,38 +1583,32 @@
 ersError (state,
- path,
  error) %7B%0A      
@@ -1624,30 +1624,24 @@
 ametersError
-%5Bpath%5D
  = error%0A   
@@ -1650,30 +1650,34 @@
 ,%0A    update
-Result
+Parameters
 ErrorProp (s
@@ -1705,30 +1705,34 @@
       state.
-Result
+parameters
 Error%5Bpath%5D 
@@ -1752,34 +1752,30 @@
 ,%0A    update
-Parameters
+Result
 Error (state
@@ -1793,34 +1793,30 @@
       state.
-parameters
+result
 Error = erro
@@ -1841,32 +1841,36 @@
 eResultError
+Prop
  (state,
  error) %7B%0A  
@@ -1849,32 +1849,38 @@
 rrorProp (state,
+ path,
  error) %7B%0A      
@@ -1885,17 +1885,17 @@
   state.
-R
+r
 esultErr
@@ -1888,32 +1888,38 @@
 tate.resultError
+%5Bpath%5D
  = error%0A    %7D%0A 
@@ -1907,24 +1907,94 @@
 th%5D = error%0A
+    %7D,%0A    updateResult (state, result) %7B%0A      state.result = result%0A
     %7D%0A  %7D%0A%0A 
@@ -2022,16 +2022,33 @@
 rameters
+: %7B%0A      handler
  (contex
@@ -2063,32 +2063,34 @@
 oldVal, path) %7B%0A
+  
       context.di
@@ -2155,21 +2155,93 @@
     
-%7D,%0A    result
+  %7D,%0A      deep: true,%0A      onlyDescendence: true%0A    %7D,%0A    result: %7B%0A      handler
  (co
@@ -2264,24 +2264,26 @@
 al, path) %7B%0A
+  
       contex
@@ -2336,24 +2336,79 @@
 ative, val)%0A
+      %7D,%0A      deep: true,%0A      onlyDescendence: true%0A
     %7D%0A  %7D%0A%0A 

Add 'onlyDescendence' option for parameter and response watch, catch and handle error for api.send()