/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-a4bd011a.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, toRefs, inject, computed, onMounted, onBeforeUnmount } from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import { useTNodeJSX } from '../hooks/tnode.js';
import { renderTNodeJSXDefault } from '../utils/render-tnode.js';
import { useConfig, usePrefixClass } from '../config-provider/useConfig.js';
import _Option from './option.js';
import useVirtualScroll from '../hooks/useVirtualScroll.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../hooks/render-tnode.js';
import 'lodash/isString';
import 'lodash/isObject';
import '@babel/runtime/helpers/typeof';
import 'vue';
import '../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import '../utils/ripple.js';
import '../_common/js/utils/set-style.js';
import '../config.js';
import '../config-provider/config-receiver.js';
import './option-props.js';
import '../checkbox/index.js';
import '../checkbox/group.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/event.js';
import '../utils/mixins.js';
import '../checkbox/checkbox-group-props.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../checkbox/type.js';
import '../hooks/useLazyLoad.js';
import '../_common/js/utils/observe.js';
import './util.js';
import 'lodash/cloneDeep';
import '../hooks/useCommonClassName.js';

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 sizeClassMap = {
  small: "s",
  medium: "m",
  large: "l"
};
var SelectPanel = defineComponent({
  name: "TSelectPanel",
  components: {
    TOption: _Option
  },
  props: ["inputValue", "panelTopContent", "panelBottomContent", "size", "options", "empty", "filter", "loading", "loadingText", "multiple", "scroll", "creatable", "filterable"],
  setup: function setup(props) {
    var _props$scroll5;

    var _toRefs = toRefs(props),
        options = _toRefs.options,
        inputValue = _toRefs.inputValue;

    var renderTNode = useTNodeJSX();

    var _useConfig = useConfig("select"),
        t = _useConfig.t,
        global = _useConfig.global;

    var selectProvider = inject("tSelect");
    var COMPONENT_NAME = usePrefixClass("select");
    var panelContentRef = computed(function () {
      return selectProvider.getOverlayElm();
    });

    var _ref = props.scroll || {},
        type = _ref.type,
        _ref$rowHeight = _ref.rowHeight,
        rowHeight = _ref$rowHeight === void 0 ? 28 : _ref$rowHeight,
        _ref$bufferSize = _ref.bufferSize,
        bufferSize = _ref$bufferSize === void 0 ? 20 : _ref$bufferSize,
        _ref$isFixedRowHeight = _ref.isFixedRowHeight,
        isFixedRowHeight = _ref$isFixedRowHeight === void 0 ? false : _ref$isFixedRowHeight,
        _ref$threshold = _ref.threshold,
        threshold = _ref$threshold === void 0 ? 100 : _ref$threshold;

    var displayOptions = computed(function () {
      if (!inputValue.value || !(props.filterable || isFunction(props.filter))) return options.value;

      var filterMethods = function filterMethods(option) {
        var _option$label;

        if (isFunction(props.filter)) {
          return props.filter("".concat(props.inputValue), option);
        }

        return ((_option$label = option.label) === null || _option$label === void 0 ? void 0 : _option$label.indexOf("".concat(props.inputValue))) > -1;
      };

      var res = [];
      var groupIndex = 0;
      props.options.forEach(function (option) {
        if (option.group && option.children) {
          res.push(_objectSpread(_objectSpread({}, option), {}, {
            children: option.children.filter(filterMethods).reduce(function (pre, cur) {
              pre.push(_objectSpread(_objectSpread({}, cur), {}, {
                index: groupIndex
              }));
              groupIndex += 1;
              return pre;
            }, [])
          }));
        }

        if (filterMethods(option)) {
          res.push(_objectSpread(_objectSpread({}, option), {}, {
            index: res.length
          }));
        }
      });
      return res;
    });

    var getDisplayOptions = function getDisplayOptions() {
      return displayOptions.value;
    };

    var isCreateOptionShown = computed(function () {
      return props.creatable && props.filterable && props.inputValue;
    });
    var isEmpty = computed(function () {
      return !(displayOptions.value.length > 0);
    });
    var isVirtual = computed(function () {
      var _props$scroll, _props$options, _props$scroll2;

      return ((_props$scroll = props.scroll) === null || _props$scroll === void 0 ? void 0 : _props$scroll.type) === "virtual" && ((_props$options = props.options) === null || _props$options === void 0 ? void 0 : _props$options.length) > (((_props$scroll2 = props.scroll) === null || _props$scroll2 === void 0 ? void 0 : _props$scroll2.threshold) || 100);
    });

    var _ref2 = type === "virtual" ? useVirtualScroll({
      container: panelContentRef,
      data: displayOptions,
      fixedHeight: isFixedRowHeight,
      lineHeight: rowHeight,
      bufferSize: bufferSize,
      threshold: threshold
    }) : {},
        _ref2$trs = _ref2.trs,
        trs = _ref2$trs === void 0 ? null : _ref2$trs,
        _ref2$visibleData = _ref2.visibleData,
        visibleData = _ref2$visibleData === void 0 ? null : _ref2$visibleData,
        _ref2$handleScroll = _ref2.handleScroll,
        handleVirtualScroll = _ref2$handleScroll === void 0 ? null : _ref2$handleScroll,
        _ref2$scrollHeight = _ref2.scrollHeight,
        scrollHeight = _ref2$scrollHeight === void 0 ? null : _ref2$scrollHeight,
        _ref2$translateY = _ref2.translateY,
        translateY = _ref2$translateY === void 0 ? null : _ref2$translateY,
        _ref2$handleRowMounte = _ref2.handleRowMounted,
        handleRowMounted = _ref2$handleRowMounte === void 0 ? null : _ref2$handleRowMounte;

    var lastScrollY = -1;

    var onInnerVirtualScroll = function onInnerVirtualScroll(e) {
      if (!isVirtual.value) {
        return;
      }

      var target = e.target || e.srcElement;
      var top = target.scrollTop;

      if (Math.abs(lastScrollY - top) > 5) {
        handleVirtualScroll();
        lastScrollY = top;
      } else {
        lastScrollY = -1;
      }
    };

    onMounted(function () {
      var _props$scroll3;

      if (((_props$scroll3 = props.scroll) === null || _props$scroll3 === void 0 ? void 0 : _props$scroll3.type) === "virtual") {
        selectProvider.getOverlayElm().addEventListener("scroll", onInnerVirtualScroll);
      }
    });
    onBeforeUnmount(function () {
      var _props$scroll4;

      if (((_props$scroll4 = props.scroll) === null || _props$scroll4 === void 0 ? void 0 : _props$scroll4.type) === "virtual") {
        selectProvider.getOverlayElm().removeEventListener("scroll", onInnerVirtualScroll);
      }
    });
    return {
      t: t,
      global: global,
      isEmpty: isEmpty,
      renderTNode: renderTNode,
      selectProvider: selectProvider,
      isCreateOptionShown: isCreateOptionShown,
      trs: trs,
      isVirtual: isVirtual,
      onInnerVirtualScroll: onInnerVirtualScroll,
      visibleData: visibleData,
      scrollHeight: scrollHeight,
      translateY: translateY,
      scrollType: (_props$scroll5 = props.scroll) === null || _props$scroll5 === void 0 ? void 0 : _props$scroll5.type,
      handleRowMounted: handleRowMounted,
      bufferSize: bufferSize,
      threshold: threshold,
      displayOptions: displayOptions,
      getDisplayOptions: getDisplayOptions,
      componentName: COMPONENT_NAME
    };
  },
  methods: {
    renderEmptyContent: function renderEmptyContent() {
      var h = this.$createElement;
      var empty = this.empty,
          t = this.t,
          global = this.global;

      if (empty && typeof empty === "string") {
        return h("div", {
          "class": "".concat(this.componentName, "__empty")
        }, [empty]);
      }

      return renderTNodeJSXDefault(this, "empty", h("div", {
        "class": "".concat(this.componentName, "__empty")
      }, [t(global.empty)]));
    },
    renderLoadingContent: function renderLoadingContent() {
      var h = this.$createElement;
      var loadingText = this.loadingText,
          t = this.t,
          global = this.global;

      if (loadingText && typeof loadingText === "string") {
        return h("div", {
          "class": "".concat(this.componentName, "__loading-tips")
        }, [loadingText]);
      }

      return renderTNodeJSXDefault(this, "loadingText", h("div", {
        "class": "".concat(this.componentName, "__loading-tips")
      }, [t(global.loadingText)]));
    },
    renderCreateOption: function renderCreateOption() {
      var h = this.$createElement;
      var inputValue = this.inputValue,
          trs = this.trs,
          scrollType = this.scrollType,
          isVirtual = this.isVirtual,
          handleRowMounted = this.handleRowMounted,
          bufferSize = this.bufferSize;
      var on = isVirtual ? {
        onRowMounted: handleRowMounted
      } : {};
      return h("ul", {
        "class": ["".concat(this.componentName, "__create-option"), "".concat(this.componentName, "__list")]
      }, [h("t-option", helper([{
        "attrs": {
          "isCreatedOption": true,
          "value": inputValue,
          "label": inputValue,
          "trs": trs,
          "scrollType": scrollType,
          "isVirtual": isVirtual,
          "bufferSize": bufferSize
        },
        "class": "".concat(this.componentName, "__create-option--special")
      }, {
        "on": on
      }]))]);
    },
    renderOptionsContent: function renderOptionsContent(options) {
      var _this = this;

      var h = this.$createElement;
      var multiple = this.multiple,
          trs = this.trs,
          scrollType = this.scrollType,
          isVirtual = this.isVirtual,
          handleRowMounted = this.handleRowMounted,
          bufferSize = this.bufferSize;
      var on = isVirtual ? {
        onRowMounted: handleRowMounted
      } : {};
      return h("ul", {
        "class": "".concat(this.componentName, "__list")
      }, [options.map(function (item, index) {
        if (item.group) {
          return h("t-option-group", {
            "attrs": {
              "label": item.group,
              "divider": item.divider
            }
          }, [_this.renderOptionsContent(item.children)]);
        }

        var scrollProps = isVirtual ? {
          rowIndex: item.$index,
          trs: trs,
          scrollType: scrollType,
          isVirtual: isVirtual,
          bufferSize: bufferSize
        } : {
          key: index
        };
        return h("t-option", helper([{
          "class": item["class"],
          "style": item.style,
          "props": _objectSpread({}, _objectSpread(_objectSpread({}, item), scrollProps)),
          "attrs": {
            "multiple": multiple
          },
          "scopedSlots": {
            "default": item.slots
          },
          "key": "".concat(item.$index || "", "_").concat(index)
        }, {
          "on": on
        }]));
      })]);
    },
    renderPanelContent: function renderPanelContent() {
      var innerStyle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var h = this.$createElement;
      var renderTNode = this.renderTNode,
          isEmpty = this.isEmpty,
          isCreateOptionShown = this.isCreateOptionShown,
          size = this.size,
          loading = this.loading,
          isVirtual = this.isVirtual,
          visibleData = this.visibleData,
          displayOptions = this.displayOptions;
      return h("div", {
        "class": ["".concat(this.componentName, "__dropdown-inner"), "".concat(this.componentName, "__dropdown-inner--size-").concat(sizeClassMap[size])],
        "style": innerStyle
      }, [renderTNode("panelTopContent"), isCreateOptionShown && this.renderCreateOption(), loading && this.renderLoadingContent(), !loading && isEmpty && !isCreateOptionShown && this.renderEmptyContent(), !loading && !isEmpty && this.renderOptionsContent(isVirtual && visibleData ? visibleData : displayOptions), renderTNode("panelBottomContent")]);
    }
  },
  render: function render() {
    var h = arguments[0];
    var translateY = this.translateY,
        scrollHeight = this.scrollHeight,
        isVirtual = this.isVirtual;

    if (isVirtual) {
      var cursorTranslate = "translate(0, ".concat(scrollHeight, "px)");
      var cursorTranslateStyle = {
        position: "absolute",
        width: "1px",
        height: "1px",
        transition: "transform 0.2s",
        transform: cursorTranslate,
        "-ms-transform": cursorTranslate,
        "-moz-transform": cursorTranslate,
        "-webkit-transform": cursorTranslate
      };
      var translate = "translate(0, ".concat(translateY, "px)");
      var virtualStyle = {
        transform: translate,
        "-ms-transform": translate,
        "-moz-transform": translate,
        "-webkit-transform": translate
      };
      return h("div", [h("div", {
        "style": _objectSpread({}, cursorTranslateStyle)
      }), this.renderPanelContent(virtualStyle)]);
    }

    return this.renderPanelContent();
  }
});

export { SelectPanel as default };
//# sourceMappingURL=select-panel.js.map
