import { _ as _defineProperty } from './defineProperty-18999f8e.js';
import { _ as _objectSpread2 } from './objectSpread2-605331b1.js';
import './es.array.map-093cfbca.js';
import { v as vueClickOutside } from './index-b4d779f7.js';
import BaseInput from './BaseInput-aea1e5fe.js';
import BaseLoader from './BaseLoader-a5bfc4d9.js';
import { n as normalizeComponent } from './normalize-component-63e8c7a0.js';
import './export-ecf4b1cb.js';
import './array-iteration-dec5536e.js';
import './function-bind-context-b3fabf77.js';
import './array-species-create-afb281e6.js';
import './array-method-has-species-support-d96108b0.js';
import './es.object.to-string-5a59fbfc.js';
import './classof-b10e7604.js';
import './es.regexp.to-string-784af81c.js';
import './regexp-flags-b5fc4ddb.js';
import './es.array.concat-9f3136dc.js';
import './create-property-c00b5c27.js';
import './es.array.slice-e9c3dd03.js';
import './es.number.constructor-16a232f3.js';
import './object-set-prototype-of-9853ad1a.js';
import './es.array.includes-1f433385.js';
import './object-keys-a5d351f7.js';
import './BaseIcon-d03b1fa2.js';
import './es.array.join-6ca857ff.js';
import './array-method-is-strict-903f485b.js';
import './utils-21c04279.js';
import './typeof-6c6d8d7a.js';
import './unsupportedIterableToArray-577176bf.js';
import './regexp-exec-abstract-04d119fc.js';
import './string-multibyte-88926e02.js';
import './es.string.split-cd1f3afc.js';
import './is-regexp-5558fd03.js';
import './es.string.replace-07eeebfe.js';
import './es.string.includes-53dbb113.js';
import './es.array.sort-92cbfa54.js';
import './web.dom-collections.for-each-917714ef.js';
import './dom-iterables-b6f5ef3b.js';
import './es.object.keys-6369b884.js';
import './i18n-e25edebe.js';

/**
 * Form Input Field Component with Autocomplete Functionality
 */

var script = {
  name: 'BaseAutocompleteInput',
  components: {
    BaseInput: BaseInput,
    BaseLoader: BaseLoader
  },
  directives: {
    ClickOutside: vueClickOutside
  },
  model: {
    prop: 'inputText',
    event: 'autocomplete'
  },
  props: {
    /**
     * input field settable from outside
     *
     * @model
     *
     */
    inputText: {
      type: String,
      default: ''
    },

    /**
     * list of selectable entries
     */
    list: {
      type: Array,
      default: function _default() {
        return [];
      }
    },

    /**
     * label for the input field
     */
    label: {
      type: String,
      default: ''
    },

    /**
     * placeholder for the input field
     */
    placeholder: {
      type: String,
      default: ''
    },

    /**
     * text shown when no dynamic autocomplete entries present
     */
    dropDownNoOptionsInfo: {
      type: String,
      default: 'No options available'
    },

    /**
     * show spinner to indicate that something is loading
     * (for dynamically fetched entries that need to do backend requests)
     */
    isLoading: {
      type: Boolean,
      default: false
    },

    /**
     * specify the object property that should be used as value to be displayed
     */
    labelPropertyName: {
      type: String,
      default: ''
    }
  },
  data: function data() {
    return {
      // internal list representation allows for string or object input
      // or also additional internal properties (not set (yet?) though)
      listInt: [],
      // internal copy of input text settable from outside
      inputTextInt: null,
      // index for use of arrow keys
      selectedMenuEntryIndex: -1,
      // these two variables are set by clickoutside directives
      // and are supposed to set the showDropDown variable
      insideInput: false,
      insideDropDown: false
    };
  },
  computed: {
    // show drop down is governed by these two variables - one from baseinput component
    // and one from drop down (to prevent label (which is part of baseinput)
    // to be inside the "clickoutside" area)
    showDropDown: {
      get: function get() {
        return this.insideInput || this.insideDropDown;
      },
      set: function set(val) {
        this.insideInput = val;
        this.insideDropDown = val;
      }
    }
  },
  watch: {
    // watch for changes in the drop down list and set internal variable accordingly
    list: function list(val) {
      var _this = this;

      this.listInt = val && val.length && typeof val[0] === 'string' ? val.map(function (v) {
        return _objectSpread2({}, _defineProperty({}, _this.labelPropertyName, v));
      }) : val.map(function (v) {
        return _objectSpread2({}, v);
      });
    },
    // watch for changes in the text input from outside and set internal value accordingly
    inputText: function inputText(val) {
      this.inputTextInt = val;
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    // set internal text and list variables with values set in parent
    this.listInt = this.list && this.list.length && typeof this.list[0] === 'string' ? this.list.map(function (v) {
      return _objectSpread2({}, _defineProperty({}, _this2.labelPropertyName, v));
    }) : this.list.map(function (v) {
      return _objectSpread2({}, v);
    });
    this.inputTextInt = this.inputText;
  },
  methods: {
    // add an entry selected from the drop down list
    selectEntry: function selectEntry(entry) {
      // check if entry was selected by clicking in the dropdown
      if (entry) {
        this.inputTextInt = entry[this.labelPropertyName];
        this.$emit('selected', entry); // or was selected by use of arrow keys
        // (else no explicit setting of inputTextInt and event emitting is needed)
      } else if (this.selectedMenuEntryIndex >= 0) {
        this.inputTextInt = this.listInt[this.selectedMenuEntryIndex][this.labelPropertyName];
        /**
         * event triggered when entry is selected from drop down menu
         *
         * @type {object | string}
         */

        this.$emit('selected', this.listInt[this.selectedMenuEntryIndex]);
      } // as soon as entry was selected, hide the drop down, blur input focus and reset the list


      this.showDropDown = false;
      this.$refs.baseInput.$el.getElementsByTagName('input')[0].blur();
      this.listInt = [];
    },
    onInputFocus: function onInputFocus() {
      this.selectedMenuEntryIndex = -1; // TODO: check if maybe event should also be triggered if input is empty
      // e.g. for fetching of initial list??

      if (this.inputTextInt) {
        /**
         * emitting event on text input.
         *
         * @type {string}
         */
        this.$emit('autocomplete', this.inputTextInt);
      }

      this.showDropDown = true;
    },
    // allow for navigation with arrow keys
    triggerArrowKey: function triggerArrowKey(event) {
      if (event.key === 'ArrowDown') {
        // setting to -1 if outside list range was added to allow to get rid of active
        // element again (so user can add plain text (not from list) as well)
        this.selectedMenuEntryIndex = this.selectedMenuEntryIndex < this.listInt.length - 1 ? this.selectedMenuEntryIndex + 1 : -1;
      } else if (event.key === 'ArrowUp') {
        this.selectedMenuEntryIndex = this.selectedMenuEntryIndex > 0 ? this.selectedMenuEntryIndex - 1 : -1;
      }

      if (this.$refs.dropdownContainer.scrollHeight !== this.$refs.dropdownContainer.clientHeight) {
        this.$refs.option[this.selectedMenuEntryIndex].scrollIntoView({
          block: 'nearest',
          inline: 'nearest'
        });
      }
    }
  }
};

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "base-autocomplete-input"
  }, [_c('BaseInput', {
    ref: "baseInput",
    attrs: {
      "placeholder": !_vm.inputTextInt ? _vm.placeholder : '',
      "label": _vm.label
    },
    on: {
      "clicked-outside": function clickedOutside($event) {
        _vm.insideInput = false;
      },
      "focus": _vm.onInputFocus,
      "keydown": [function ($event) {
        if (!$event.type.indexOf('key') && _vm._k($event.keyCode, "up", 38, $event.key, ["Up", "ArrowUp"]) && _vm._k($event.keyCode, "down", 40, $event.key, ["Down", "ArrowDown"])) {
          return null;
        }

        $event.preventDefault();
        return _vm.triggerArrowKey.apply(null, arguments);
      }, function ($event) {
        if (!$event.type.indexOf('key') && _vm._k($event.keyCode, "enter", 13, $event.key, "Enter")) {
          return null;
        }

        $event.preventDefault();
        return _vm.selectEntry();
      }],
      "input": function input($event) {
        return _vm.$emit('autocomplete', _vm.inputTextInt);
      }
    },
    scopedSlots: _vm._u([{
      key: "input-field-addition-after",
      fn: function fn() {
        return [_vm.isLoading ? _c('div', {
          staticClass: "base-input-loader"
        }, [_c('BaseLoader')], 1) : _vm._e()];
      },
      proxy: true
    }]),
    model: {
      value: _vm.inputTextInt,
      callback: function callback($$v) {
        _vm.inputTextInt = $$v;
      },
      expression: "inputTextInt"
    }
  }), _vm._v(" "), _vm.showDropDown ? _c('div', {
    directives: [{
      name: "click-outside",
      rawName: "v-click-outside",
      value: function value() {
        return _vm.insideDropDown = false;
      },
      expression: "() => insideDropDown = false"
    }],
    ref: "dropdownContainer",
    staticClass: "base-autocomplete-drop-down",
    on: {
      "mouseleave": function mouseleave($event) {
        _vm.selectedMenuEntryIndex = -1;
      }
    }
  }, [_vm._l(_vm.listInt, function (entry, index) {
    return _c('div', {
      key: index,
      ref: "option",
      refInFor: true,
      staticClass: "base-autocomplete-drop-down-entry-wrapper",
      class: {
        'base-autocomplete-drop-down-entry-wrapper-active': index === _vm.selectedMenuEntryIndex
      },
      on: {
        "click": function click($event) {
          return _vm.selectEntry(entry);
        },
        "mouseover": function mouseover($event) {
          _vm.selectedMenuEntryIndex = index;
        }
      }
    }, [_vm._t("drop-down-entry", function () {
      return [_c('div', {
        staticClass: "base-autocomplete-drop-down-entry"
      }, [_vm._v("\n          " + _vm._s(entry[_vm.labelPropertyName]) + "\n        ")])];
    }, {
      "item": entry
    })], 2);
  }), _vm._v(" "), !_vm.listInt.length ? _vm._t("no-options", function () {
    return [_c('div', {
      staticClass: "base-autocomplete-drop-down-entry-wrapper"
    }, [_vm._v("\n        " + _vm._s(_vm.dropDownNoOptionsInfo) + "\n      ")])];
  }) : _vm._e()], 2) : _vm._e()], 1);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = "data-v-70ce27b8";
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

/* style inject shadow dom */

var __vue_component__ = /*#__PURE__*/normalizeComponent({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, false, undefined, undefined, undefined);

var BaseAutocompleteInput = __vue_component__;

export { BaseAutocompleteInput as default };
