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

'use strict';

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

var Vue = require('vue');
var components_dropdown_dropdownItem = require('./dropdown-item.js');
var utils_renderNode = require('../../utils/render-node.js');
var utils_helper = require('../../utils/helper.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
require('@ldesign/icons-vue');
require('../divider/index.js');
require('../divider/divider.js');
require('../divider/props.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../../utils/with-install.js');
require('../../config.js');
require('./props.js');
require('../../utils/event.js');
require('../../utils/ripple.js');
require('../../chunks/set-style.js');

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

var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

const classPrefixMixins = components_configProvider_configReceiver.getClassPrefixMixins("dropdown");
var _DropdownMenu = utils_mixins["default"](Vue__default["default"], classPrefixMixins).extend({
  name: "LDropdownMenu",
  inject: {
    dropdown: {
      default: void 0
    }
  },
  components: {
    LDropdownItem: components_dropdown_dropdownItem["default"]
  },

  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: utils_helper.pxCompat(maxColumnWidth),
          minWidth: utils_helper.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`
        }
      }, [utils_renderNode.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]);
  }

});

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