<template>
  <div
    class="row no-wrap items-center relative-position"
    :class="classes"
    :tabindex="focusable && !disable ? 0 : null"
    @click="__onClick"
    v-ripple.mat="inverted"
  >
    <template v-if="before">
      <s-icon
        v-for="item in before"
        :key="item"
        class="s-if-control s-if-control-before"
        :class="{hidden: __additionalHidden(item, hasError, length)}"
        :name="item.icon"
        @click="item.handler || __defaultHander"
      ></s-icon>
    </template>

    <div class="s-if-inner col row no-wrap items-center relative-position">
      <div
        v-if="label" style="position: absolute !important;"
        class="s-if-label ellipsis full-width absolute self-start"
        :class="{'s-if-label-above': labelIsAbove}"
        v-html="label"
      ></div>

      <span
        v-if="prefix"
        class="s-if-addon s-if-addon-left"
        :class="addonClass"
        v-html="prefix"
      ></span>

      <slot></slot>

      <span
        v-if="suffix"
        class="s-if-addon s-if-addon-right"
        :class="addonClass"
        v-html="suffix"
      ></span>
    </div>

    <slot name="after"></slot>
    <template v-if="after">
      <s-icon
        v-for="item in after"
        :key="item"
        class="s-if-control"
        :class="{hidden: __additionalHidden(item, hasError, length)}"
        :name="item.icon"
        @click="item.handler || __defaultHandler"
      ></s-icon>
    </template>
  </div>
</template>

<script>
import Mixin from './input-frame-mixin'
import Ripple from '../directives/ripple'

export default {
  name: 's-input-frame',
  mixins: [Mixin],
  directives: {
    Ripple
  },
  props: {
    topAddons: Boolean,
    focused: Boolean,
    length: Number,
    focusable: Boolean,
    additionalLength: Boolean
  },
  data () {
    return {
      field: {}
    }
  },
  inject: ['__field'],
  computed: {
    label () {
      return this.stackLabel || this.floatLabel
    },
    addonClass () {
      return {
        's-if-addon-visible': this.labelIsAbove,
        'self-start': this.topAddons
      }
    },
    classes () {
      const cls = [{
        's-if-has-label': this.label,
        's-if-focused': this.focused,
        's-if-error': this.hasError,
        's-if-disabled': this.disable,
        's-if-focusable': this.focusable && !this.disable,
        's-if-inverted': this.inverted,
        's-if-dark': this.dark || this.inverted
      }]

      const color = this.hasError ? 'negative' : this.color
      if (color) {
        if (this.inverted) {
          cls.push(`bg-${color}`)
          cls.push(`text-white`)
        }
        else {
          cls.push(`text-${color}`)
        }
      }
      return cls
    },
    hasError () {
      return !!(this.field.error || this.error)
    }
  },
  methods: {
    __onClick (e) {
      this.$emit('click', e)
    },
    __additionalHidden (item, hasError, length) {
      if (item.condition !== void 0) {
        return item.condition === false
      }
      return (
        (item.content !== void 0 && !item.content === (length > 0)) ||
        (item.error !== void 0 && !item.error === hasError)
      )
    },
    __defaultHandler () {}
  },
  created () {
    if (this.__field) {
      this.field = this.__field
      this.field.__registerInput(this)
    }
  },
  beforeDestroy () {
    if (this.__field) {
      this.field.__unregisterInput()
    }
  }
}
</script>
