/* eslint-disable no-nested-ternary */
/* eslint-disable guard-for-in */
/* eslint-disable no-undef */
/* eslint-disable no-continue */
/* eslint-disable no-restricted-syntax */
/* eslint-disable import/prefer-default-export */
/* eslint-disable no-param-reassign */
import Vue from 'vue'

export function createSimpleFunctional (c, el = 'div', name) {
  return Vue.extend({
    name: name || c.replace(/__/g, '-'),
    functional: true,

    render (h, {
      data,
      children
    }) {
      data.staticClass = `${c} ${data.staticClass || ''}`.trim()
      return h(el, data, children)
    }

  })
}

export function upperFirst (str) {
  return str.charAt(0).toUpperCase() + str.slice(1)
}
export function wrapInArray (v) {
  return v != null ? Array.isArray(v) ? v : [v] : []
}
export function isObject (obj) {
  return obj !== null && typeof obj === 'object'
}

export const keyCodes = Object.freeze({
  enter: 13,
  tab: 9,
  delete: 46,
  esc: 27,
  space: 32,
  up: 38,
  down: 40,
  left: 37,
  right: 39,
  end: 35,
  home: 36,
  del: 46,
  backspace: 8,
  insert: 45,
  pageup: 33,
  pagedown: 34,
  shift: 16
})

export function mergeDeep (source = {}, target = {}) {
  for (const key in target) {
    const sourceProperty = source[key]
    const targetProperty = target[key] // Only continue deep merging if
    // both properties are objects
    if (isObject(sourceProperty) && isObject(targetProperty)) {
      source[key] = mergeDeep(sourceProperty, targetProperty)
      continue
    }
    source[key] = targetProperty
  }
  return source
}
