/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _toConsumableArray } from '../_chunks/dep-bf1e33c8.mjs';
import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { defineComponent, ref, computed, watch, onBeforeUnmount, createVNode, h } from 'vue';
import { i as isFunction_1 } from '../_chunks/dep-5717e263.mjs';
import { i as isString_1 } from '../_chunks/dep-876fb3ec.mjs';
import '../_common/js/log/index.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { on, off } from '../utils/dom.mjs';
import _HighlightOption from './highlight-option.mjs';
import log from '../_common/js/log/log.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../utils/easing.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var AutoCompleteOptionList = defineComponent({
  name: "AutoCompleteOptionList",
  props: {
    sizeClassNames: Object,
    value: String,
    size: String,
    options: Array,
    popupVisible: Boolean,
    highlightKeyword: Boolean,
    filterable: Boolean,
    filter: Function
  },
  emits: ["select"],
  setup: function setup(props, _ref) {
    var emit = _ref.emit,
      slots = _ref.slots,
      expose = _ref.expose;
    var active = ref("");
    var classPrefix = usePrefixClass();
    var classes = computed(function () {
      return "".concat(classPrefix.value, "-select__list");
    });
    var optionClasses = computed(function () {
      return ["".concat(classPrefix.value, "-select-option"), _defineProperty({}, props.sizeClassNames[props.size], props.size)];
    });
    var tOptions = computed(function () {
      var options = (props.options || []).map(function (item) {
        var option = {};
        if (isString_1(item)) {
          option = {
            text: item,
            label: item
          };
        } else {
          if (item.text && !isString_1(item.text)) log.warn("AutoComplete", "`text` must be a string.");
          if (!item.text) {
            if (isString_1(item.label)) option = _objectSpread(_objectSpread({}, item), {}, {
              text: item.label
            });else log.warn("AutoComplete", "one of `label` and `text` must be a existed string.");
          } else {
            option = item;
          }
        }
        return option;
      });
      if (props.filter) {
        options = options.filter(function (option) {
          return props.filter(props.value, option);
        });
      } else if (props.filterable) {
        var regExp = new RegExp(props.value, "i");
        options = options.filter(function (item) {
          return regExp.test(item.text);
        });
      }
      return options;
    });
    var onOptionClick = function onOptionClick(e) {
      var liNode = e.target;
      while (liNode && liNode.tagName !== "LI") liNode = liNode.parentNode;
      var keyword = liNode.getAttribute("title");
      active.value = keyword;
      emit("select", keyword, {
        e: e
      });
    };
    var onKeyInnerPress = function onKeyInnerPress(e) {
      if (e.code === "ArrowUp" || e.key === "ArrowUp") {
        var index = tOptions.value.findIndex(function (item) {
          return item.text === active.value;
        });
        var newIndex = index - 1 < 0 ? tOptions.value.length - 1 : index - 1;
        active.value = tOptions.value[newIndex].text;
      } else if (e.code === "ArrowDown" || e.key === "ArrowDown") {
        var _index = tOptions.value.findIndex(function (item) {
          return item.text === active.value;
        });
        var _newIndex = _index + 1 >= tOptions.value.length ? 0 : _index + 1;
        active.value = tOptions.value[_newIndex].text;
      } else if (e.code === "Enter" || e.key === "Enter") {
        emit("select", active.value, {
          e: e
        });
      }
    };
    var addKeyboardListener = function addKeyboardListener() {
      on(document, "keydown", onKeyInnerPress);
    };
    var removeKeyboardListener = function removeKeyboardListener() {
      off(document, "keydown", onKeyInnerPress);
    };
    expose({
      addKeyboardListener: addKeyboardListener,
      removeKeyboardListener: removeKeyboardListener
    });
    watch(function () {
      return props.popupVisible;
    }, function () {
      if (props.popupVisible) addKeyboardListener();else removeKeyboardListener();
    }, {
      immediate: true
    });
    watch(function () {
      return props.value;
    }, function () {
      if (!props.value) active.value = "";
    }, {
      immediate: true
    });
    onBeforeUnmount(function () {
      removeKeyboardListener();
    });
    return function () {
      if (!tOptions.value.length) return null;
      return createVNode("ul", {
        "class": classes.value
      }, [tOptions.value.map(function (item) {
        var _slots$option;
        var cls = _toConsumableArray(optionClasses.value);
        if (item.text === active.value) cls.push("".concat(classPrefix.value, "-select-option--hover"));
        var labelNode = item.label;
        if (isFunction_1(item.label)) labelNode = item.label(h);else if (slots.option) labelNode = (_slots$option = slots.option) === null || _slots$option === void 0 ? void 0 : _slots$option.call(slots, {
          option: item
        });
        var content = labelNode || item.text;
        return createVNode("li", {
          "key": item.text,
          "class": cls,
          "title": item.text,
          "onClick": onOptionClick
        }, [isString_1(content) && props.highlightKeyword ? createVNode(_HighlightOption, {
          "content": content,
          "keyword": props.value
        }, null) : content]);
      })]);
    };
  }
});

export { AutoCompleteOptionList as default };
//# sourceMappingURL=option-list.mjs.map
