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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var isString = require('lodash/isString');
var _common_js_log_log = require('../_common/js/log/log.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var utils_dom = require('../utils/dom.js');
var autoComplete_highlightOption = require('./highlight-option.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('../utils/easing.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);

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__default["default"](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 = vue.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 = vue.ref("");
    var classPrefix = hooks_useConfig.usePrefixClass();
    var classes = vue.computed(function () {
      return "".concat(classPrefix.value, "-select__list");
    });
    var optionClasses = vue.computed(function () {
      return ["".concat(classPrefix.value, "-select-option"), _defineProperty__default["default"]({}, props.sizeClassNames[props.size], props.size)];
    });
    var tOptions = vue.computed(function () {
      var options = (props.options || []).map(function (item) {
        var option = {};
        if (isString__default["default"](item)) {
          option = {
            text: item,
            label: item
          };
        } else {
          if (item.text && !isString__default["default"](item.text)) _common_js_log_log["default"].warn("AutoComplete", "`text` must be a string.");
          if (!item.text) {
            if (isString__default["default"](item.label)) option = _objectSpread(_objectSpread({}, item), {}, {
              text: item.label
            });else _common_js_log_log["default"].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() {
      utils_dom.on(document, "keydown", onKeyInnerPress);
    };
    var removeKeyboardListener = function removeKeyboardListener() {
      utils_dom.off(document, "keydown", onKeyInnerPress);
    };
    expose({
      addKeyboardListener: addKeyboardListener,
      removeKeyboardListener: removeKeyboardListener
    });
    vue.watch(function () {
      return props.popupVisible;
    }, function () {
      if (props.popupVisible) addKeyboardListener();else removeKeyboardListener();
    }, {
      immediate: true
    });
    vue.watch(function () {
      return props.value;
    }, function () {
      if (!props.value) active.value = "";
    }, {
      immediate: true
    });
    vue.onBeforeUnmount(function () {
      removeKeyboardListener();
    });
    return function () {
      if (!tOptions.value.length) return null;
      return vue.createVNode("ul", {
        "class": classes.value
      }, [tOptions.value.map(function (item) {
        var _slots$option;
        var cls = _toConsumableArray__default["default"](optionClasses.value);
        if (item.text === active.value) cls.push("".concat(classPrefix.value, "-select-option--hover"));
        var labelNode = item.label;
        if (isFunction__default["default"](item.label)) labelNode = item.label(vue.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 vue.createVNode("li", {
          "key": item.text,
          "class": cls,
          "title": item.text,
          "onClick": onOptionClick
        }, [isString__default["default"](content) && props.highlightKeyword ? vue.createVNode(autoComplete_highlightOption["default"], {
          "content": content,
          "keyword": props.value
        }, null) : content]);
      })]);
    };
  }
});

exports["default"] = AutoCompleteOptionList;
//# sourceMappingURL=option-list.js.map
