/**
 * ldesign v0.2.3
 * (c) 2022年9月24日 swimly
 * @license MIT
 */

import Vue from 'vue';
import _DropdownItem from './dropdown-item.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import { pxCompat } from '../../utils/helper.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import '@ldesign/icons-vue';
import '../divider/index.js';
import '../divider/divider.js';
import '../divider/props.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-b1584b65.js';
import '../../utils/with-install.js';
import '../../config.js';
import './props.js';
import '../../utils/event.js';
import '../../utils/ripple.js';
import '../../_chunks/dep-1aed710a.js';

const classPrefixMixins = getClassPrefixMixins("dropdown");
var _DropdownMenu = mixins(Vue, classPrefixMixins).extend({
  name: "LDropdownMenu",
  inject: {
    dropdown: {
      default: void 0
    }
  },
  components: {
    LDropdownItem: _DropdownItem
  },

  data() {
    return {
      path: ""
    };
  },

  methods: {
    isActive(item, pathPrefix) {
      let excludeSelf = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
      const itemPath = `${pathPrefix}/${item.value}`;

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

      return this.path.indexOf(itemPath) === 0;
    },

    handleHoverItem(path) {
      this.path = path;
    },

    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(children, showSubmenu, pathPrefix) {
      const h = this.$createElement;
      const menuClass = [`${this.componentName}__menu-column`, "narrow-scrollbar", {
        submenu__visible: showSubmenu
      }];
      const {
        maxHeight,
        maxColumnWidth,
        minColumnWidth
      } = this.dropdown;
      return h("div", {
        "class": menuClass,
        "style": {
          maxHeight: `${maxHeight}px`,
          maxWidth: pxCompat(maxColumnWidth),
          minWidth: pxCompat(minColumnWidth)
        }
      }, [children.map((item, idx) => h("l-dropdown-item", {
        "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": `${pathPrefix}/${item.value}`
        },
        "on": {
          "click": (data, context) => this.handleItemClick(data, context, idx),
          "hover": this.handleHoverItem
        }
      }))]);
    }

  },

  render() {
    const h = arguments[0];
    const columns = [];
    let menuItems = this.dropdown.options;
    let pathPrefix = "";

    if (this.$scopedSlots.default) {
      return h("div", {
        "class": `${this.componentName}__menu`
      }, [h("div", {
        "class": [`${this.componentName}__menu-column`, "narrow-scrollbar"],
        "style": {
          maxHeight: `${this.dropdown.maxHeight}px`,
          maxWidth: `${this.dropdown.maxColumnWidth}px`,
          minWidth: `${this.dropdown.minColumnWidth}px`
        }
      }, [renderLNodeJSX(this, "default")])]);
    }

    while (menuItems && menuItems.length) {
      const activeItem = menuItems.find(item => this.isActive(item, pathPrefix, false));
      columns.push(this.renderMenuColumn(menuItems, !!activeItem, pathPrefix));

      if (activeItem) {
        pathPrefix = `${pathPrefix}/${activeItem.value}`;
        menuItems = activeItem.children || [];
      } else {
        menuItems = [];
      }
    }

    return h("div", {
      "class": `${this.componentName}__menu`
    }, [columns]);
  }

});

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