import { ArrowChevronDown16SecondaryIcon, MoreEllipsis316BoldBlueIcon, XmarkRemove16SecondaryIcon, XmarkRemove16BlueIcon } from '@cloudtower/icons-react';
import Button from '../../core/Button/index.js';
import Icon from '../../core/Icon/index.js';
import { ExtraOverflow } from '../../core/Overflow/index.js';
import Tooltip from '../../core/Tooltip/index.js';
import { Typo } from '../../core/Typo/index.js';
import useParrotTranslation from '../../hooks/useParrotTranslation.js';
import { Menu, Dropdown } from 'antd';
import cs from 'classnames';
import React__default, { useMemo, useCallback } from '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 renderBatchOperationMenuItem = (act, idx = 0) => {
  if (act === "divider") {
    return /* @__PURE__ */React__default.createElement(Menu.Divider, {
      key: `divider-${idx}`
    });
  }
  if ("children" in act) {
    return /* @__PURE__ */React__default.createElement(Menu.SubMenu, {
      className: cs(Typo.Label.l4_regular),
      key: act.key,
      title: (act == null ? void 0 : act.icon) ? React__default.cloneElement(act.icon, {}, act.title) : /* @__PURE__ */React__default.createElement("span", null, act.title)
    }, act.children.map(renderBatchOperationMenuItem));
  }
  const Inner = props => {
    return /* @__PURE__ */React__default.createElement("span", __spreadValues({}, props), act.icon ? React__default.cloneElement(act.icon, {}, act.title) : null, (act == null ? void 0 : act.count) && /* @__PURE__ */React__default.createElement("span", null, act.count));
  };
  return /* @__PURE__ */React__default.createElement(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.createElement(Tooltip, {
    title: act.tooltip
  }, /* @__PURE__ */React__default.createElement(Inner, null)) : /* @__PURE__ */React__default.createElement(Inner, null));
};
const BatchOperation = props => {
  const {
    count,
    onClearSelection,
    actions
  } = props;
  const {
    t
  } = useParrotTranslation();
  const els = useMemo(() => {
    return actions.map((act, idx) => {
      if (act === "divider") {
        return /* @__PURE__ */React__default.createElement(Menu.Divider, {
          key: `divider-${idx}`
        });
      }
      if ("children" in act) {
        return /* @__PURE__ */React__default.createElement(Dropdown, {
          key: act.key,
          trigger: ["click"],
          placement: "bottomRight",
          overlay: /* @__PURE__ */React__default.createElement(Menu, null, act.children.map(renderBatchOperationMenuItem))
        }, /* @__PURE__ */React__default.createElement(Button, {
          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.createElement(Icon, {
            src: ArrowChevronDown16SecondaryIcon
          })
        }, act.title));
      }
      if (act.tooltip) {
        return /* @__PURE__ */React__default.createElement(Tooltip, {
          title: act.tooltip
        }, /* @__PURE__ */React__default.createElement(Button, {
          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.createElement(Button, {
        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 = useCallback(end => /* @__PURE__ */React__default.createElement(Dropdown, {
    overlay: /* @__PURE__ */React__default.createElement(Menu, null, actions.slice(end).map((act, idx) => {
      return act === "divider" ? /* @__PURE__ */React__default.createElement(Menu.Divider, {
        key: `divider-${idx}`
      }) : "children" in act ? /* @__PURE__ */React__default.createElement(Menu.SubMenu, {
        className: cs(Typo.Label.l4_regular),
        key: act.key,
        title: (act == null ? void 0 : act.icon) ? React__default.cloneElement(act.icon, {}, act.title) : /* @__PURE__ */React__default.createElement("span", null, act.title)
      }, act.children.map(renderBatchOperationMenuItem)) : renderBatchOperationMenuItem(act);
    }))
  }, /* @__PURE__ */React__default.createElement(Icon, {
    className: "more-btn",
    src: MoreEllipsis316BoldBlueIcon
  })), [actions]);
  return /* @__PURE__ */React__default.createElement("div", {
    className: SelectToolbar
  }, /* @__PURE__ */React__default.createElement("span", {
    className: "select-total"
  }, t("components.selected_item_with_count", {
    count
  }), /* @__PURE__ */React__default.createElement(Icon, {
    src: XmarkRemove16SecondaryIcon,
    hoverSrc: XmarkRemove16BlueIcon,
    onClick: onClearSelection
  })), /* @__PURE__ */React__default.createElement(ExtraOverflow, {
    className: "action-group",
    reverse: true,
    els,
    extraEl: getExtraEl
  }));
};
var BatchOperation$1 = BatchOperation;

export { BatchOperation$1 as default, renderBatchOperationMenuItem };
