<template>
  <transition name="hn-zoom-in-top" @after-leave="doDestroy">
    <div
      v-show="showPopper"
      class="hn-autocomplete-suggestion hn-popper"
      :class="{ 'is-loading': !parent.hideLoading && parent.loading }"
      :style="{ width: dropdownWidth }"
      role="region">
      <hn-scrollbar
        tag="ul"
        wrap-class="hn-autocomplete-suggestion__wrap"
        view-class="hn-autocomplete-suggestion__list">
        <li v-if="!parent.hideLoading && parent.loading"><i class="hn-icon-loading"></i></li>
        <slot v-else>
        </slot>
      </hn-scrollbar>
    </div>
  </transition>
</template>
<script>
import Popper from 'heiner-ui/src/utils/vue-popper';
import Emitter from 'heiner-ui/src/mixins/emitter';
import HnScrollbar from 'heiner-ui/packages/scrollbar';

export default {
  components: { HnScrollbar },
  mixins: [Popper, Emitter],

  componentName: 'HnAutocompleteSuggestions',

  data() {
    return {
      parent: this.$parent,
      dropdownWidth: ''
    };
  },

  props: {
    options: {
      default() {
        return {
          gpuAcceleration: false
        };
      }
    },
    id: String
  },

  methods: {
    select(item) {
      this.dispatch('HnAutocomplete', 'item-click', item);
    }
  },

  updated() {
    this.$nextTick(_ => {
      this.popperJS && this.updatePopper();
    });
  },

  mounted() {
    this.$parent.popperElm = this.popperElm = this.$el;
    this.referenceElm = this.$parent.$refs.input.$refs.input;
    this.referenceList = this.$el.querySelector('.hn-autocomplete-suggestion__list');
    this.referenceList.setAttribute('role', 'listbox');
    this.referenceList.setAttribute('id', this.id);
  },

  created() {
    this.$on('visible', (val, inputWidth) => {
      this.dropdownWidth = inputWidth + 'px';
      this.showPopper = val;
    });
  }
};
</script>
