<commit_before>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])
          }
        })<commit_msg>Add 'onlyDescendence' option for parameter and response watch, catch and handle error for api.send()<commit_after>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('updateParametersErrorProp', path, error)
      })
    },
    [title + '_validateResult']: function (context, path, val) {
      api.validateResponse(path, val).catch(function (error) {
        context.commit('updateResultErrorProp', path, error)
      })
    },
    [title]: function (context) {
      return api.send(context.state.parameters).then(function (result) {
        context.commit('updateResult', result)
      }).catch(function (error) {
        switch (error.type) {
          case 'parameters':
            context.commit('updateParametersError', error.error)
            break
          case 'response':
            context.commit('updateResultError', error.error)
            break
          case 'request':
            throw error.error
          default:
            throw error
        }
      })
    }
  }

  let mutations = {
    updateParametersError (state, error) {
      state.parametersError = error
    },
    updateParametersErrorProp (state, path, error) {
      state.parametersError[path] = error
    },
    updateResultError (state, error) {
      state.resultError = error
    },
    updateResultErrorProp (state, path, error) {
      state.resultError[path] = error
    },
    updateResult (state, result) {
      state.result = result
    }
  }

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

  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])
          }
        })
{'fraction_non_alphanumeric': 0.13048476719678098, 'fraction_numerical': 0.0003832151753209427, 'mean_word_length': 2.484646194926569, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1042623', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1541, 'n_words': 343}