'use strict';

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

var iconsReact = require('@cloudtower/icons-react');
var index$2 = require('../../core/Button/index.js');
var index$3 = require('../../core/Icon/index.js');
var index$4 = require('../../core/Overflow/index.js');
var index$1 = require('../../core/Tooltip/index.js');
var index = require('../../core/Typo/index.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var antd = require('antd');
var cs = require('classnames');
var React = require('react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var cs__default = /*#__PURE__*/_interopDefault(cs);
var React__default = /*#__PURE__*/_interopDefault(React);

var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {
  enumerable: true,
  configurable: true,
  writable: true,
  value
}) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) {
    if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]);
  }
  return a;
};
const SelectToolbar = "E_s1ebs0ra";
const MenuItemInnerStyle = "E_mzzncgx";
const renderBatchOperationMenuItem = (act, idx = 0) => {
  if (act === "divider") {
    return /* @__PURE__ */React__default.default.createElement(antd.Menu.Divider, {
      key: `divider-${idx}`
    });
  }
  if ("children" in act) {
    return /* @__PURE__ */React__default.default.createElement(antd.Menu.SubMenu, {
      className: cs__default.default(index.Typo.Label.l4_regular),
      key: act.key,
      title: (act == null ? void 0 : act.icon) ? React__default.default.cloneElement(act.icon, {}, act.title) : /* @__PURE__ */React__default.default.createElement("span", null, act.title)
    }, act.children.map(renderBatchOperationMenuItem));
  }
  const Inner = props => {
    return /* @__PURE__ */React__default.default.createElement("span", __spreadValues({
      className: MenuItemInnerStyle
    }, props), act.icon ? React__default.default.cloneElement(act.icon, {}, act.title) : null, (act == null ? void 0 : act.count) && /* @__PURE__ */React__default.default.createElement("span", null, act.count));
  };
  return /* @__PURE__ */React__default.default.createElement(antd.Menu.Item, {
    key: act.key,
    onClick: act.onClick,
    danger: act.danger,
    disabled: !!(act == null ? void 0 : act.disabled),
    className: (act == null ? void 0 : act.danger) && act.disabled ? "item-danger-disabled" : ""
  }, "tooltip" in act && act.tooltip ? /* @__PURE__ */React__default.default.createElement(index$1, {
    title: act.tooltip
  }, /* @__PURE__ */React__default.default.createElement(Inner, null)) : /* @__PURE__ */React__default.default.createElement(Inner, null));
};
const BatchOperation = props => {
  const {
    count,
    onClearSelection,
    actions
  } = props;
  const {
    t
  } = useParrotTranslation();
  const els = React.useMemo(() => {
    return actions.map((act, idx) => {
      if (act === "divider") {
        return /* @__PURE__ */React__default.default.createElement(antd.Menu.Divider, {
          key: `divider-${idx}`
        });
      }
      if ("children" in act) {
        return /* @__PURE__ */React__default.default.createElement(antd.Dropdown, {
          key: act.key,
          trigger: ["click"],
          placement: "bottomRight",
          overlay: /* @__PURE__ */React__default.default.createElement(antd.Menu, null, act.children.map(renderBatchOperationMenuItem))
        }, /* @__PURE__ */React__default.default.createElement(index$2, {
          key: act.key,
          type: "ordinary-onTint",
          prefixIcon: act.icon != null ? act.icon : void 0,
          danger: act == null ? void 0 : act.danger,
          className: `sub-menu-${act.key}`,
          suffixIcon: /* @__PURE__ */React__default.default.createElement(index$3, {
            src: iconsReact.ArrowChevronDown16SecondaryIcon
          })
        }, act.title));
      }
      if (act.tooltip) {
        return /* @__PURE__ */React__default.default.createElement(index$1, {
          title: act.tooltip
        }, /* @__PURE__ */React__default.default.createElement(index$2, {
          key: act.key,
          type: "ordinary-onTint",
          danger: act.danger,
          onClick: act.onClick,
          disabled: act.disabled,
          prefixIcon: act.icon != null ? act.icon : void 0
        }, act.title));
      }
      return /* @__PURE__ */React__default.default.createElement(index$2, {
        key: act.key,
        type: "ordinary-onTint",
        danger: act.danger,
        onClick: act.onClick,
        disabled: act.disabled,
        prefixIcon: act.icon != null ? act.icon : void 0
      }, act.title);
    });
  }, [actions]);
  const getExtraEl = React.useCallback(end => /* @__PURE__ */React__default.default.createElement(antd.Dropdown, {
    overlay: /* @__PURE__ */React__default.default.createElement(antd.Menu, null, actions.slice(end).map((act, idx) => {
      return act === "divider" ? /* @__PURE__ */React__default.default.createElement(antd.Menu.Divider, {
        key: `divider-${idx}`
      }) : "children" in act ? /* @__PURE__ */React__default.default.createElement(antd.Menu.SubMenu, {
        className: cs__default.default(index.Typo.Label.l4_regular),
        key: act.key,
        title: (act == null ? void 0 : act.icon) ? React__default.default.cloneElement(act.icon, {}, act.title) : /* @__PURE__ */React__default.default.createElement("span", null, act.title)
      }, act.children.map(renderBatchOperationMenuItem)) : renderBatchOperationMenuItem(act);
    }))
  }, /* @__PURE__ */React__default.default.createElement(index$3, {
    className: "more-btn",
    src: iconsReact.MoreEllipsis316BoldBlueIcon
  })), [actions]);
  return /* @__PURE__ */React__default.default.createElement("div", {
    className: SelectToolbar
  }, /* @__PURE__ */React__default.default.createElement("span", {
    className: "select-total"
  }, t("components.selected_item_with_count", {
    count
  }), /* @__PURE__ */React__default.default.createElement(index$3, {
    src: iconsReact.XmarkRemove16SecondaryIcon,
    hoverSrc: iconsReact.XmarkRemove16BlueIcon,
    onClick: onClearSelection
  })), /* @__PURE__ */React__default.default.createElement(index$4.ExtraOverflow, {
    className: "action-group",
    reverse: true,
    els,
    extraEl: getExtraEl
  }));
};
var BatchOperation$1 = BatchOperation;

exports.default = BatchOperation$1;
exports.renderBatchOperationMenuItem = renderBatchOperationMenuItem;
