import { shieldProps } from './utils'
import { parseHref } from './utils-path'

function validateClick(e) {
  return !(e.defaultPrevented || e.button !== 0 || e.metaKey || e.altKey || e.ctrlKey || e.shiftKey)
}

export function createListener(action) {
  return function clickHandler(e) {
    if (validateClick(e)) {
      e.preventDefault()
      action()
    }
  }
}

const defaultStyle = { touchAction: 'manipulation', msTouchAction: 'manipulation' }

export const Anchor = p => {
  let props = shieldProps(p, 'createElement', 'ultra', 'style', 'state', 'title')
  let { href, createElement, ultra, style, state, title } = props
  let loc = Object.assign(parseHref(href), { state, title })
  props.onClick = createListener(ultra.push.bind(null, loc))
  props.style = Object.assign({}, defaultStyle, style)
  return createElement('a', props)
}

@@ -1,16 +1,22 @@
 import %7B
+ isFn,
  shieldP
@@ -816,16 +816,38 @@
   return
+ isFn(createElement) ?
  createE
@@ -864,11 +864,19 @@
 , props)
+ : props
 %0A%7D%0A

return props obj if createElement is not provided