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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var isFunction = require('lodash/isFunction');
var divider_index = require('../divider/index.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
var dropdown_props = require('./props.js');
var dropdown_dropdownItem = require('./dropdown-item.js');
require('../utils/withInstall.js');
require('../divider/divider.js');
require('../hooks/tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isObject');
require('../divider/props.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-7f0559c3.js');
require('dayjs');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isUndefined');
require('lodash/isNull');
require('lodash/isNumber');
require('./dropdown-item-props.js');

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

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 _DropdownMenu = vue.defineComponent({
  name: "TDropdownMenu",
  props: _objectSpread({}, dropdown_props["default"]),
  setup: function setup(props) {
    var dropdownClass = hooks_useConfig.usePrefixClass("dropdown");
    var dropdownMenuClass = hooks_useConfig.usePrefixClass("dropdown__menu");
    var scrollTopMap = vue.reactive({});
    var menuRef = vue.ref();
    var isOverMaxHeight = vue.ref(false);
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        ChevronRightIcon: iconsVueNext.ChevronRightIcon,
        ChevronLeftIcon: iconsVueNext.ChevronLeftIcon
      }),
      ChevronRightIcon = _useGlobalIcon.ChevronRightIcon,
      ChevronLeftIcon = _useGlobalIcon.ChevronLeftIcon;
    var handleItemClick = function handleItemClick(options) {
      var _data$onClick, _props$onClick;
      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);
    };
    var handleScroll = function handleScroll(e, deep) {
      var scrollTop = e.target.scrollTop;
      scrollTopMap[deep] = scrollTop;
    };
    vue.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;
      }
    });
    var getContent = function getContent(content) {
      if (isFunction__default["default"](content)) return content(vue.h);
      return content;
    };
    var renderOptions = function renderOptions(data, deep) {
      var _data$forEach;
      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(scrollTopMap[deep] / 30);
        var renderIdx = onViewIdx >= 0 ? onViewIdx : idx;
        if (optionItem.children) {
          optionItem.children = renderOptions(optionItem.children, deep + 1);
          renderContent = vue.createVNode("div", {
            "key": idx
          }, [vue.createVNode(dropdown_dropdownItem["default"], {
            "style": optionItem.style,
            "class": ["".concat(dropdownClass.value, "__item"), "".concat(dropdownClass.value, "__item--suffix"), optionItem["class"]],
            "value": optionItem.value,
            "theme": optionItem.theme,
            "active": optionItem.active,
            "prefixIcon": optionItem.prefixIcon,
            "disabled": optionItem.disabled,
            "minColumnWidth": props.minColumnWidth,
            "maxColumnWidth": props.maxColumnWidth,
            "isSubmenu": true
          }, {
            "default": function _default() {
              return [vue.createVNode("div", {
                "class": "".concat(dropdownClass.value, "__item-content")
              }, [props.direction === "right" ? vue.createVNode(vue.Fragment, null, [vue.createVNode("span", {
                "class": "".concat(dropdownClass.value, "__item-text")
              }, [getContent(optionItem.content)]), vue.createVNode(ChevronRightIcon, {
                "class": "".concat(dropdownClass.value, "__item-direction"),
                "size": "16"
              }, null)]) : vue.createVNode(vue.Fragment, null, [vue.createVNode(ChevronLeftIcon, {
                "class": "".concat(dropdownClass.value, "__item-direction"),
                "size": "16"
              }, null), vue.createVNode("span", {
                "class": "".concat(dropdownClass.value, "__item-text")
              }, [getContent(optionItem.content)])])]), vue.createVNode("div", {
                "class": ["".concat(dropdownClass.value, "__submenu-wrapper"), _defineProperty__default["default"]({}, "".concat(dropdownClass.value, "__submenu-wrapper--").concat(props.direction), props.direction)],
                "style": {
                  position: "absolute",
                  top: "".concat(renderIdx * 30, "px")
                }
              }, [vue.createVNode("div", {
                "class": ["".concat(dropdownClass.value, "__submenu"), _defineProperty__default["default"]({}, "".concat(dropdownClass.value, "__submenu--disabled"), optionItem.disabled)],
                "style": {
                  position: "static",
                  maxHeight: "".concat(props.maxHeight, "px")
                },
                "onScroll": function onScroll(e) {
                  return handleScroll(e, deep + 1);
                }
              }, [vue.createVNode("ul", null, [optionItem.children])])])];
            }
          }), optionItem.divider ? vue.createVNode(divider_index.Divider, null, null) : null]);
        } else {
          renderContent = vue.createVNode("div", {
            "key": idx
          }, [vue.createVNode(dropdown_dropdownItem["default"], {
            "style": optionItem.style,
            "class": ["".concat(dropdownClass.value, "__item"), optionItem["class"]],
            "value": optionItem.value,
            "theme": optionItem.theme,
            "active": optionItem.active,
            "prefixIcon": optionItem.prefixIcon,
            "disabled": optionItem.disabled,
            "minColumnWidth": props.minColumnWidth,
            "maxColumnWidth": props.maxColumnWidth,
            "onClick": optionItem.disabled || optionItem.children ? function () {
              return null;
            } : function (value, context) {
              return handleItemClick({
                data: optionItem,
                context: context
              });
            }
          }, {
            "default": function _default() {
              return [vue.createVNode("span", {
                "class": "".concat(dropdownClass.value, "__item-text")
              }, [getContent(optionItem.content)])];
            }
          }), optionItem.divider ? vue.createVNode(divider_index.Divider, null, null) : null]);
        }
        arr.push(renderContent);
      });
      return arr;
    };
    return function () {
      return vue.createVNode("div", {
        "class": [dropdownMenuClass.value, "".concat(dropdownMenuClass.value, "--").concat(props.direction), _defineProperty__default["default"]({}, "".concat(dropdownMenuClass.value, "--overflow"), isOverMaxHeight.value)],
        "style": {
          maxHeight: "".concat(props.maxHeight, "px")
        },
        "ref": menuRef,
        "onScroll": function onScroll(e) {
          return handleScroll(e, 0);
        }
      }, [renderOptions(props.options, 0)]);
    };
  }
});

exports["default"] = _DropdownMenu;
//# sourceMappingURL=dropdown-menu.js.map
