"use strict";

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DropmenuMemo = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var icon_1 = __importDefault(require("../icon"));

var button_1 = require("../button");

var menu_1 = __importStar(require("../menu"));

var Dropdown_1 = __importDefault(require("./Dropdown"));

require("./style/dropmenu.css");

var prefixCls = 'dmc-dropmenu';

var Dropmenu = function Dropmenu(props) {
  var _a;

  var type = props.type,
      size = props.size,
      _b = props.menus,
      menus = _b === void 0 ? [] : _b,
      values = props.values,
      defaultValues = props.defaultValues,
      onClickMenuItem = props.onClickMenuItem,
      onClickMenu = props.onClickMenu,
      icon = props.icon,
      iconType = props.iconType,
      children = props.children,
      block = props.block,
      className = props.className,
      onlyText = props.onlyText,
      dropIcon = props.dropIcon,
      renderOverlay = props.renderOverlay,
      renderDropNode = props.renderDropNode,
      overlayClassName = props.overlayClassName,
      popupClassName = props.popupClassName,
      dropKey = props.dropKey,
      dropNoBorder = props.dropNoBorder,
      noSelected = props.noSelected,
      disabled = props.disabled,
      trigger = props.trigger,
      noMatch = props.noMatch,
      restProps = __rest(props, ["type", "size", "menus", "values", "defaultValues", "onClickMenuItem", "onClickMenu", "icon", "iconType", "children", "block", "className", "onlyText", "dropIcon", "renderOverlay", "renderDropNode", "overlayClassName", "popupClassName", "dropKey", "dropNoBorder", "noSelected", "disabled", "trigger", "noMatch"]); // 非受控


  var _c = React.useState(defaultValues !== null && defaultValues !== void 0 ? defaultValues : []),
      unControlVal = _c[0],
      setUnControlVal = _c[1]; // 受控校验


  var isControl = ('values' in props); // 受控/非受控的value

  var bindValue = isControl ? values !== null && values !== void 0 ? values : [] : unControlVal; // 不记录选择

  var val = !noSelected ? bindValue : [];

  var _d = React.useState(!!props.visible),
      actived = _d[0],
      setActived = _d[1];

  var cls = classnames_1["default"]("" + prefixCls, (_a = {}, _a[prefixCls + "-compact"] = size === 'compact', _a[prefixCls + "-" + type] = type, _a[prefixCls + "-no-border"] = dropNoBorder, _a[prefixCls + "-actived"] = actived, _a[prefixCls + "-only-icon"] = icon, _a[prefixCls + "-only-text"] = onlyText, _a[prefixCls + "-block"] = block, _a[prefixCls + "-disabled"] = disabled, _a), className);

  var handleClickMenuItem = function handleClickMenuItem(params) {
    setActived(false);
    !isControl && setUnControlVal(params.keyPath);
    onClickMenuItem && onClickMenuItem(params);
  };

  var renderMenuChildren = function renderMenuChildren(item, index, arr) {
    var _a;

    var node;

    if (item.children) {
      var View = item.type === 'group' ? menu_1.MenuItemGroup : menu_1.SubMenu;
      var itemCls = item.type === 'group' ? '' : prefixCls + "-sub-menu-item";
      var cls_1 = item.type === 'group' ? '' : popupClassName || overlayClassName || '';
      node = jsx_runtime_1.jsx(View, __assign({
        className: itemCls,
        popupClassName: cls_1,
        title: jsx_runtime_1.jsx("span", {
          children: item.title
        }, void 0),
        disabled: item.disabled
      }, {
        children: item.children.map(function (it, index) {
          return renderMenuChildren(it, index, item.children);
        })
      }), item.key);
    } else {
      var select = classnames_1["default"](prefixCls + "-menu-item", (_a = {}, _a[prefixCls + "-menu-item-actived"] = val.includes(item.key), _a));
      node = jsx_runtime_1.jsx(menu_1.MenuItem, __assign({
        onClick: handleClickMenuItem,
        disabled: item.disabled,
        selectable: false,
        danger: item.danger
      }, {
        children: jsx_runtime_1.jsxs("div", __assign({
          className: select
        }, {
          children: [jsx_runtime_1.jsx(icon_1["default"], {
            icon: 'done-check',
            className: prefixCls + "-drop-icon"
          }, void 0), item.icon && jsx_runtime_1.jsx(icon_1["default"], {
            icon: item.icon,
            className: prefixCls + "-icon"
          }, void 0), item.title]
        }), void 0)
      }), item.key);
    } // 去掉最后一个item添加的divider


    var currentLength = (arr !== null && arr !== void 0 ? arr : []).length;
    var noDivider = currentLength - 1 === index;

    if (item.divider && !noDivider) {
      return [node, jsx_runtime_1.jsx(menu_1.MenuDivider, {}, item.key + "-divider")];
    }

    return node;
  };

  var overlay = renderOverlay ? renderOverlay() : function () {
    var menuType = type === 'assist-bg' ? 'assist-bg' : undefined;
    var overlayCls = classnames_1["default"](overlayClassName);

    if (!menus.length && noMatch) {
      return noMatch;
    }

    return jsx_runtime_1.jsx(menu_1["default"], __assign({
      type: menuType,
      onClick: onClickMenu,
      className: overlayCls
    }, {
      children: menus.map(function (it, index) {
        return renderMenuChildren(it, index, menus);
      })
    }), void 0);
  };

  var onVisibleChange = function onVisibleChange(visible) {
    props.onVisibleChange && props.onVisibleChange(visible, dropKey);
    !disabled && setActived(visible);
  };

  var dropNode;

  if (icon) {
    if (iconType === 'border') {
      dropNode = jsx_runtime_1.jsxs(button_1.IconButton, __assign({
        type: 'border',
        actived: actived,
        className: className,
        disabled: disabled
      }, {
        children: [jsx_runtime_1.jsx(icon_1["default"], {
          icon: icon,
          size: 18
        }, void 0), jsx_runtime_1.jsx(icon_1["default"], {
          icon: "arrow-down",
          size: 12
        }, void 0)]
      }), void 0);
    } else if (iconType === 'only-icon') {
      dropNode = jsx_runtime_1.jsx(button_1.IconButton, {
        type: 'only-icon',
        icon: icon,
        actived: actived,
        ghost: true,
        className: className,
        disabled: disabled
      }, void 0);
    } else {
      dropNode = jsx_runtime_1.jsx(button_1.IconButton, {
        icon: icon,
        actived: actived,
        ghost: true,
        className: className,
        disabled: disabled
      }, void 0);
    }
  } else if (onlyText) {
    dropNode = jsx_runtime_1.jsxs("div", __assign({
      className: cls
    }, {
      children: [children, dropIcon !== false && jsx_runtime_1.jsx(icon_1["default"], {
        icon: dropIcon || 'chevron-down',
        className: prefixCls + "-icon"
      }, void 0)]
    }), void 0);
  } else if (renderDropNode) {
    dropNode = renderDropNode(actived);
  } else {
    dropNode = jsx_runtime_1.jsxs("div", __assign({
      className: cls
    }, {
      children: [children, dropIcon !== false && jsx_runtime_1.jsx(icon_1["default"], {
        icon: dropIcon || 'arrow-down',
        className: prefixCls + "-icon"
      }, void 0)]
    }), void 0);
  }

  return jsx_runtime_1.jsx(Dropdown_1["default"], __assign({
    overlay: overlay
  }, restProps, {
    onVisibleChange: onVisibleChange,
    trigger: disabled ? [] : trigger
  }, {
    children: dropNode
  }), void 0);
};

Dropmenu.defaultProps = {
  placement: 'bottomLeft',
  trigger: 'click'
};
Dropmenu.displayName = 'Dropmenu';
exports["default"] = Dropmenu;
var DropmenuMemo = React.memo(Dropmenu);
exports.DropmenuMemo = DropmenuMemo;
DropmenuMemo.displayName = 'DropmenuMemo';

