<!--
Copyright 2019 Huawei Technologies Co., Ltd.All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<template>
  <div class="el-autocomplete"
       v-clickoutside="close"
       aria-haspopup="listbox"
       role="combobox"
       :aria-expanded="suggestionVisible"
       :aria-owns="id">
    <el-input ref="input"
              v-bind="[$props, $attrs]"
              @input="handleChange"
              @focus="handleFocus"
              @blur="handleBlur"
              @clear="handleClear"
              @keydown.up.native.prevent="highlight(highlightedIndex - 1)"
              @keydown.down.native.prevent="highlight(highlightedIndex + 1)"
              @keydown.enter.native="handleKeyEnter"
              @keydown.native.tab="close">
      <template slot="prepend"
                v-if="$slots.prepend">
        <slot name="prepend"></slot>
      </template>
      <template slot="append"
                v-if="$slots.append">
        <slot name="append"></slot>
      </template>
      <template slot="prefix"
                v-if="$slots.prefix">
        <slot name="prefix"></slot>
      </template>
      <template slot="suffix"
                v-if="$slots.suffix">
        <slot name="suffix"></slot>
      </template>
    </el-input>
    <el-autocomplete-suggestions visible-arrow
                                 :class="[popperClass ? popperClass : '']"
                                 :suggestions-has-data="suggestionsHasData"
                                 :popper-options="popperOptions"
                                 :append-to-body="popperAppendToBody"
                                 ref="suggestions"
                                 :placement="placement"
                                 :id="id">
      <li v-for="(item, index) in suggestions"
          :key="index"
          :class="{ highlighted: highlightedIndex === index }"
          class="rtl-item"
          @click="select(item)"
          :id="`${id}-item-${index}`"
          role="option"
          :aria-selected="highlightedIndex === index">
        <slot :item="item">
          <span :title="item[valueKey]">{{ item[valueKey] }}</span>
        </slot>
      </li>
    </el-autocomplete-suggestions>
  </div>
</template>
<script>
import {Autocomplete} from 'element-ui';
export default {
  extends: Autocomplete,
  props: {
    highlightFirstItem: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    suggestionsHasData() {
      return !!this.suggestions.length;
    },
  },
};
</script>
<style lang="scss" scoped>
.rtl-item {
  text-align: right;
  direction: rtl;
  text-overflow: ellipsis;
}
</style>
