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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var iconsVue = require('@ldesign/icons-vue');
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_dropdownItem = require('./dropdown-item.js');
var dropdown_props = require('./props.js');
require('../utils/withInstall.js');
require('vue');
require('lodash/capitalize');
require('../divider/divider.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../_chunks/dep-7c884501.js');
require('dayjs');
require('../utils/mixins.js');
require('../divider/props.js');
require('../config-provider/useConfig.js');
require('../hooks/tnode.js');
require('lodash/isEmpty');
require('../hooks/render-tnode.js');
require('lodash/isString');
require('lodash/isObject');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../utils/ripple.js');
require('../_common/js/utils/set-style.js');
require('../config.js');
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 = VueCompositionAPI.defineComponent({
  name: "TDropdownMenu",
  props: _objectSpread({}, dropdown_props["default"]),
  setup: function setup(props, _ref) {
    var emit = _ref.emit;
    var dropdownClass = hooks_useConfig.usePrefixClass("dropdown");
    var dropdownMenuClass = hooks_useConfig.usePrefixClass("dropdown__menu");
    var menuRef = VueCompositionAPI.ref();
    var isOverMaxHeight = VueCompositionAPI.ref(false);
    var scrollTopMap = VueCompositionAPI.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;
      VueCompositionAPI.set(scrollTopMap, deep, scrollTop);
    };
    VueCompositionAPI.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__default["default"](content)) return content(VueCompositionAPI.h);
      return content;
    },
    renderOptions: function renderOptions(data, deep) {
      var _data$forEach,
        _this = this;
      var h = this.$createElement;
      var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
          ChevronRightIcon: iconsVue.ChevronRightIcon,
          ChevronLeftIcon: iconsVue.ChevronLeftIcon
        }),
        ChevronRightIcon = _useGlobalIcon.ChevronRightIcon,
        ChevronLeftIcon = _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(dropdown_dropdownItem["default"], {
            "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, {
            "class": "".concat(_this.dropdownClass, "__item-direction"),
            "attrs": {
              "size": "16"
            }
          })]) : h("div", {
            "class": "".concat(_this.dropdownClass, "__item-content")
          }, [h(ChevronLeftIcon, {
            "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__default["default"]({}, "".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__default["default"]({}, "".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_index.Divider) : null]);
        } else {
          renderContent = h("div", {
            "key": idx
          }, [h(dropdown_dropdownItem["default"], {
            "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_index.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__default["default"]({}, "".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)]);
  }
});

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