import h from '../shared/h.js'
import bubbleEvent from '../shared/bubble-event.js'

export function Input ({ type, disabled, ...props }) {
  const disabledClass = disabled ? 'bg-txa/10' : 'pal-brand1 bg-bga'
  const commonClass = `block rounded-lg text-txa border-txa/60 border w-full p-3 ${disabledClass}`
  const style = 'box-shadow: inset 1px 1px 2px 2px hsla(var(--color-txa), 0.1)'
  const onInvalid = bubbleEvent

  switch (type) {
    case 'checkbox':
    case 'radio':
      return h('input', { ...props, onInvalid, type, disabled, className: 'appearance-auto' })
    case 'file':
      return h('input', {
        ...props,
        type,
        disabled,
        className: `block cursor-pointer ${disabled ? 'grayscale' : ''}`,
        onInvalid,
        _style: id => `
          ${id}::file-selector-button {
            border-radius: 6px;
            padding: 4px 12px;
            border-width: 1px;
            border-style: solid;
            background-color: hsl(var(--color-bgb));
            border-color: hsl(var(--color-bgb));
            color: hsl(var(--color-txb));
            font-weight: bold;
            transition-property: background-color, border-color, color;
            transition-duration: var(--anim-time);
            height: 32px;
            cursor: pointer;
          }
          ${id}:not([disabled])::file-selector-button:hover {
            background-color: hsl(var(--color-txb));
            color: hsl(var(--color-bgb));
          }
        `
      })
    case 'textarea':
      return h('textarea', {
        ...props,
        disabled,
        onInvalid,
        style: `${style}; resize: vertical`,
        className: `${commonClass} h-36`
      })
    case 'select':
      return h('select', { ...props, onInvalid, disabled, style, className: `${commonClass} h-12` })
    default:
      return h('input', { ...props, type, onInvalid, disabled, style, className: `${commonClass} h-12` })
  }
}
