export const mapRecursiveRouterParam = param => {
  let functions = {}
  normalizeMap(param).forEach(function(ref) {
    var key = ref.key
    var val = ref.val
    functions[key] = {
      default: function() {
        if (typeof this.$route !== 'undefined') {
          let value = this.$route.matched.find(route => {
            if (typeof route.props.default !== 'undefined' && typeof route.props.default[key] !== 'undefined') {
              return true
            }
          })
          if (value) {
            return value.props.default[key]
          } else {
            if (typeof val === 'object' && typeof val.default !== 'undefined') {
              if (typeof val.default === 'function') {
                return val.default()
              } else {
                return val.default
              }
            }
          }
        }
      },
    }
    if (typeof val === 'object') {
      functions[key] = Object.assign({}, val, functions[key])
    }
  })
  return functions
}

function normalizeMap(map) {
  return Array.isArray(map)
    ? map.map(function(key) {
        return { key: key, val: key }
      })
    : Object.keys(map).map(function(key) {
        return { key: key, val: map[key] }
      })
}
