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

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

    componentName: 'FlAutocompleteSuggestions',

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

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

    methods: {
      select(item) {
        this.dispatch('FlAutocomplete', '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.$parent.$refs.input.$refs.textarea;
      this.referenceList = this.$el.querySelector('.fl-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>
