/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import Vue from 'vue';
import _DropdownItem from './dropdown-item.js';
import { prefix } from '../config.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { pxCompat } from '../utils/helper.js';
import '@babel/runtime/helpers/defineProperty';
import 'tdesign-icons-vue';
import '../divider/index.js';
import '../divider/divider.js';
import '../divider/props.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/isObject';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../divider/type.js';
import './dropdown-item-props.js';
import '../utils/classnames.js';
import '../utils/event.js';
import '../utils/ripple.js';
import '../_common/js/utils/set-style.js';
import '../utils/mixins.js';
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 '../config-provider/type.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';

var name = "".concat(prefix, "-dropdown__menu");
var _DropdownMenu = Vue.extend({
  name: "TDropdownMenu",
  inject: {
    dropdown: {
      "default": void 0
    }
  },
  data: function data() {
    return {
      path: ""
    };
  },
  methods: {
    isActive: function isActive(item, pathPrefix) {
      var excludeSelf = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
      var itemPath = "".concat(pathPrefix, "/").concat(item.value);

      if (excludeSelf && this.path === itemPath) {
        return false;
      }

      return this.path.indexOf(itemPath) === 0;
    },
    handleHoverItem: function handleHoverItem(path) {
      this.path = path;
    },
    handleItemClick: function handleItemClick(data, context, idx) {
      var _this$dropdown$option, _this$dropdown$option2;

      (_this$dropdown$option = (_this$dropdown$option2 = this.dropdown.options[idx]).onClick) === null || _this$dropdown$option === void 0 ? void 0 : _this$dropdown$option.call(_this$dropdown$option2, data, context);
    },
    renderMenuColumn: function renderMenuColumn(children, showSubmenu, pathPrefix) {
      var _this = this;

      var h = this.$createElement;
      var menuClass = ["".concat(name, "-column"), "narrow-scrollbar", {
        submenu__visible: showSubmenu
      }];
      var _this$dropdown = this.dropdown,
          maxHeight = _this$dropdown.maxHeight,
          maxColumnWidth = _this$dropdown.maxColumnWidth,
          minColumnWidth = _this$dropdown.minColumnWidth;
      return h("div", {
        "class": menuClass,
        "style": {
          maxHeight: "".concat(maxHeight, "px"),
          maxWidth: pxCompat(maxColumnWidth),
          minWidth: pxCompat(minColumnWidth)
        }
      }, [children.map(function (item, idx) {
        return h(_DropdownItem, {
          "key": idx,
          "attrs": {
            "disabled": item.disabled,
            "active": _this.isActive(item, pathPrefix) || item.active,
            "value": item.value,
            "content": item.content,
            "divider": item.divider,
            "hasChildren": item.children && item.children.length > 0,
            "path": "".concat(pathPrefix, "/").concat(item.value)
          },
          "on": {
            "click": function click(data, context) {
              return _this.handleItemClick(data, context, idx);
            },
            "hover": _this.handleHoverItem
          }
        });
      })]);
    }
  },
  render: function render() {
    var _this2 = this;

    var h = arguments[0];
    var columns = [];
    var menuItems = this.dropdown.options;
    var pathPrefix = "";

    if (this.$scopedSlots["default"]) {
      return h("div", {
        "class": name
      }, [h("div", {
        "class": ["".concat(name, "__column"), "narrow-scrollbar"],
        "style": {
          maxHeight: "".concat(this.dropdown.maxHeight, "px"),
          maxWidth: "".concat(this.dropdown.maxColumnWidth, "px"),
          minWidth: "".concat(this.dropdown.minColumnWidth, "px")
        }
      }, [renderTNodeJSX(this, "default")])]);
    }

    while (menuItems && menuItems.length) {
      var activeItem = menuItems.find(function (item) {
        return _this2.isActive(item, pathPrefix, false);
      });
      columns.push(this.renderMenuColumn(menuItems, !!activeItem, pathPrefix));

      if (activeItem) {
        pathPrefix = "".concat(pathPrefix, "/").concat(activeItem.value);
        menuItems = activeItem.children || [];
      } else {
        menuItems = [];
      }
    }

    return h("div", {
      "class": name
    }, [columns]);
  }
});

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