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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, reactive, onMounted, h, set } from '@vue/composition-api';
import { ChevronRightIcon, ChevronLeftIcon } from '@ldesign/icons-vue';
import isFunction from 'lodash/isFunction';
import { Divider } from '../divider/index.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useGlobalIcon } from '../hooks/useGlobalIcon.js';
import _DropdownItem from './dropdown-item.js';
import props from './props.js';
import '../utils/withInstall.js';
import 'vue';
import 'lodash/capitalize';
import '../divider/divider.js';
import '../utils/render-tnode.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-2cb61c79.js';
import 'dayjs';
import '../utils/mixins.js';
import '../divider/props.js';
import '../config-provider/useConfig.js';
import '../hooks/tnode.js';
import 'lodash/isEmpty';
import '../hooks/render-tnode.js';
import 'lodash/isString';
import 'lodash/isObject';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/ripple.js';
import '../_common/js/utils/set-style.js';
import '../config.js';
import './dropdown-item-props.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 _DropdownMenu = defineComponent({
  name: "TDropdownMenu",
  props: _objectSpread({}, props),
  setup: function setup(props, _ref) {
    var emit = _ref.emit;
    var dropdownClass = usePrefixClass("dropdown");
    var dropdownMenuClass = usePrefixClass("dropdown__menu");
    var menuRef = ref();
    var isOverMaxHeight = ref(false);
    var scrollTopMap = reactive({});
    var handleItemClick = function handleItemClick(optionItem, options) {
      var _data$onClick, _props$onClick;
      if (optionItem.disabled || optionItem.children) return;
      var data = options.data,
        context = options.context;
      data === null || data === void 0 ? void 0 : (_data$onClick = data.onClick) === null || _data$onClick === void 0 ? void 0 : _data$onClick.call(data, data, context);
      (_props$onClick = props.onClick) === null || _props$onClick === void 0 ? void 0 : _props$onClick.call(props, data, context);
      emit("click", data, context);
    };
    var handleScroll = function handleScroll(e, deep) {
      var scrollTop = e.target.scrollTop;
      set(scrollTopMap, deep, scrollTop);
    };
    onMounted(function () {
      if (menuRef.value) {
        var _window;
        var menuHeight = parseInt((_window = window) === null || _window === void 0 ? void 0 : _window.getComputedStyle(menuRef.value).height, 10);
        if (menuHeight >= props.maxHeight) isOverMaxHeight.value = true;
      }
    });
    return {
      dropdownClass: dropdownClass,
      dropdownMenuClass: dropdownMenuClass,
      handleItemClick: handleItemClick,
      menuRef: menuRef,
      isOverMaxHeight: isOverMaxHeight,
      handleScroll: handleScroll,
      scrollTopMap: scrollTopMap
    };
  },
  methods: {
    renderOptionContent: function renderOptionContent(content) {
      if (isFunction(content)) return content(h);
      return content;
    },
    renderOptions: function renderOptions(data, deep) {
      var _data$forEach,
        _this = this;
      var h = this.$createElement;
      var _useGlobalIcon = useGlobalIcon({
          ChevronRightIcon: ChevronRightIcon,
          ChevronLeftIcon: ChevronLeftIcon
        }),
        ChevronRightIcon$1 = _useGlobalIcon.ChevronRightIcon,
        ChevronLeftIcon$1 = _useGlobalIcon.ChevronLeftIcon;
      var arr = [];
      var renderContent;
      (_data$forEach = data.forEach) === null || _data$forEach === void 0 ? void 0 : _data$forEach.call(data, function (menu, idx) {
        var optionItem = _objectSpread({}, menu);
        var onViewIdx = idx - Math.ceil(_this.scrollTopMap[deep] / 30);
        var renderIdx = onViewIdx >= 0 ? onViewIdx : idx;
        if (optionItem.children) {
          optionItem.children = _this.renderOptions(optionItem.children, deep + 1);
          renderContent = h("div", {
            "key": idx
          }, [h(_DropdownItem, {
            "class": ["".concat(_this.dropdownClass, "__item"), "".concat(_this.dropdownClass, "__item--suffix"), optionItem["class"]],
            "props": _objectSpread({}, {
              value: optionItem.value,
              theme: optionItem.theme,
              active: optionItem.active,
              prefixIcon: optionItem.prefixIcon,
              disabled: optionItem.disabled,
              minColumnWidth: _this.minColumnWidth,
              maxColumnWidth: _this.maxColumnWidth,
              isSubmenu: true
            })
          }, [_this.direction === "right" ? h("div", {
            "class": "".concat(_this.dropdownClass, "__item-content")
          }, [h("span", {
            "class": "".concat(_this.dropdownClass, "__item-text")
          }, [_this.renderOptionContent(optionItem.content)]), h(ChevronRightIcon$1, {
            "class": "".concat(_this.dropdownClass, "__item-direction"),
            "attrs": {
              "size": "16"
            }
          })]) : h("div", {
            "class": "".concat(_this.dropdownClass, "__item-content")
          }, [h(ChevronLeftIcon$1, {
            "class": "".concat(_this.dropdownClass, "__item-direction"),
            "attrs": {
              "size": "16"
            }
          }), h("span", {
            "class": "".concat(_this.dropdownClass, "__item-text")
          }, [_this.renderOptionContent(optionItem.content)])]), h("div", {
            "class": ["".concat(_this.dropdownClass, "__submenu-wrapper"), _defineProperty({}, "".concat(_this.dropdownClass, "__submenu-wrapper--").concat(_this.direction), _this.direction)],
            "style": {
              position: "absolute",
              top: "".concat(renderIdx * 30, "px")
            }
          }, [h("div", {
            "class": ["".concat(_this.dropdownClass, "__submenu"), _defineProperty({}, "".concat(_this.dropdownClass, "__submenu--disabled"), optionItem.disabled)],
            "style": {
              position: "static",
              maxHeight: "".concat(_this.maxHeight, "px")
            },
            "on": {
              "scroll": function scroll(e) {
                return _this.handleScroll(e, deep + 1);
              }
            }
          }, [h("ul", [optionItem.children])])])]), optionItem.divider ? h(Divider) : null]);
        } else {
          renderContent = h("div", {
            "key": idx
          }, [h(_DropdownItem, {
            "class": ["".concat(_this.dropdownClass, "__item"), optionItem["class"]],
            "props": _objectSpread({}, {
              value: optionItem.value,
              theme: optionItem.theme,
              active: optionItem.active,
              prefixIcon: optionItem.prefixIcon,
              disabled: optionItem.disabled,
              minColumnWidth: _this.minColumnWidth,
              maxColumnWidth: _this.maxColumnWidth,
              onClick: function onClick(value, context) {
                return _this.handleItemClick({
                  disabled: optionItem.disabled,
                  children: optionItem.children
                }, {
                  data: optionItem,
                  context: context
                });
              }
            })
          }, [h("span", {
            "class": "".concat(_this.dropdownClass, "__item-text")
          }, [_this.renderOptionContent(optionItem.content)])]), optionItem.divider ? h(Divider) : null]);
        }
        arr.push(renderContent);
      });
      return arr;
    }
  },
  render: function render() {
    var _this2 = this;
    var h = arguments[0];
    return h("div", {
      "class": [this.dropdownMenuClass, "".concat(this.dropdownMenuClass, "--").concat(this.direction), _defineProperty({}, "".concat(this.dropdownMenuClass, "--overflow"), this.isOverMaxHeight)],
      "style": {
        maxHeight: "".concat(this.maxHeight, "px")
      },
      "ref": "menuRef",
      "on": {
        "scroll": function scroll(e) {
          return _this2.handleScroll(e, 0);
        }
      }
    }, [this.renderOptions(this.options, 0)]);
  }
});

export { _DropdownMenu as default };
//# sourceMappingURL=dropdown-menu.js.map
