/**
 * ldesign v0.4.6
 * (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 VueCompositionAPI = require('@vue/composition-api');
var isFunction = require('lodash/isFunction');
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('../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
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-7c884501.js');
require('dayjs');
require('vue');
require('raf');
require('lodash/isString');
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);

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 = VueCompositionAPI.defineComponent({
  name: "AutoCompleteOptionList",
  props: {
    sizeClassNames: Object,
    value: String,
    size: String,
    options: Array,
    popupVisible: Boolean,
    highlightKeyword: Boolean,
    filterable: Boolean,
    filter: Function
  },
  setup: function setup(props, _ref) {
    var emit = _ref.emit;
    var active = VueCompositionAPI.ref("");
    var classPrefix = hooks_useConfig.usePrefixClass();
    var classes = VueCompositionAPI.computed(function () {
      return "".concat(classPrefix.value, "-select__list");
    });
    var optionClasses = VueCompositionAPI.computed(function () {
      return ["".concat(classPrefix.value, "-select-option"), _defineProperty__default["default"]({}, props.sizeClassNames[props.size], props.size)];
    });
    var tOptions = VueCompositionAPI.computed(function () {
      var options = (props.options || []).map(function (item) {
        var option = {};
        if (typeof item === "string") {
          option = {
            text: item,
            label: item
          };
        } else {
          if (item.text && typeof item.text !== "string") _common_js_log_log["default"].warn("AutoComplete", "`text` must be a string.");
          if (!item.text) {
            if (typeof item.label === "string") 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);
    };
    VueCompositionAPI.watch(function () {
      return props.popupVisible;
    }, function () {
      if (props.popupVisible) addKeyboardListener();else removeKeyboardListener();
    }, {
      immediate: true
    });
    VueCompositionAPI.watch(function () {
      return props.value;
    }, function () {
      if (!props.value) active.value = "";
    }, {
      immediate: true
    });
    VueCompositionAPI.onBeforeMount(function () {
      removeKeyboardListener();
    });
    return {
      classes: classes,
      classPrefix: classPrefix,
      optionClasses: optionClasses,
      active: active,
      tOptions: tOptions,
      addKeyboardListener: addKeyboardListener,
      removeKeyboardListener: removeKeyboardListener,
      onOptionClick: onOptionClick
    };
  },
  render: function render() {
    var _this = this;
    var h = arguments[0];
    if (!this.tOptions.length) return null;
    return h("ul", {
      "class": this.classes
    }, [this.tOptions.map(function (item) {
      var _this$$scopedSlots$op, _this$$scopedSlots;
      var cls = _toConsumableArray__default["default"](_this.optionClasses);
      if (item.text === _this.active) cls.push("".concat(_this.classPrefix, "-select-option--hover"));
      var labelNode = item.label;
      if (isFunction__default["default"](item.label)) labelNode = item.label(h);else if (_this.$scopedSlots.option) labelNode = (_this$$scopedSlots$op = (_this$$scopedSlots = _this.$scopedSlots).option) === null || _this$$scopedSlots$op === void 0 ? void 0 : _this$$scopedSlots$op.call(_this$$scopedSlots, {
        option: item
      });
      var content = labelNode || item.text;
      return h("li", {
        "key": item.text,
        "class": cls,
        "attrs": {
          "title": item.text
        },
        "on": {
          "click": _this.onOptionClick
        }
      }, [typeof content === "string" && _this.highlightKeyword ? h(autoComplete_highlightOption["default"], {
        "attrs": {
          "content": content,
          "keyword": _this.value
        }
      }) : content]);
    })]);
  }
});

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