"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _Button = _interopRequireDefault(require("../Button"));
var _Dropdown = _interopRequireDefault(require("../Dropdown"));
var _Menu = _interopRequireDefault(require("../Menu"));
var _ListAction = _interopRequireDefault(require("./ListAction"));
var _VerticalContext = _interopRequireDefault(require("../VerticalContext"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

/**
 * `ListActions` component is used for performing operations on individual objects in [List](#!/List).
 * @since 3.3.0
 */
const ListActions = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}list-actions`,
    className,
    children,
    locale,
    ...props
  } = _ref;
  const [overlay, setOverlay] = (0, _react.useState)(false);
  const vertical = (0, _react.useContext)(_VerticalContext.default);
  (0, _react.useEffect)(() => {
    const handler = _ref2 => {
      let {
        matches
      } = _ref2;
      setOverlay(!matches);
    };
    const mql = window.matchMedia('(min-width: 768px)');
    handler(mql);
    mql.addListener(handler);
    return () => {
      mql.removeListener(handler);
    };
  }, []);
  const primaryItems = [];
  const secondaryItems = [];
  let secondaryCount = 0;
  _react.Children.toArray(children).forEach((node, index) => {
    if ( /*#__PURE__*/(0, _react.isValidElement)(node) && node.type === _ListAction.default) {
      const {
        primary,
        hidden
      } = node.props;
      const key = index.toString();
      if (primary && !vertical) {
        primaryItems.push(hidden ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${baseClassName}__hidden-action`
        }, key) : node);
      } else {
        secondaryCount++;
        if (!hidden) {
          secondaryItems.push( /*#__PURE__*/(0, _react.cloneElement)(node, {
            primary: false
          }));
        }
      }
    } else {
      secondaryCount++;
      secondaryItems.push(node);
    }
  });
  let secondaryDropdown;
  if (secondaryItems.length > 0) {
    secondaryDropdown = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.default, {
      className: `${baseClassName}__menu`,
      menu: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.default, {
        children: secondaryItems
      }),
      menuPlacement: "bottom-end",
      overlay: overlay,
      locale: locale,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        ghost: true,
        icon: "kebab"
      })
    });
  } else if (secondaryCount) {
    secondaryDropdown = /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${baseClassName}__menu ${baseClassName}__hidden-action`
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)(baseClassName, className),
    ...props,
    children: [primaryItems, secondaryDropdown]
  });
};
var _default = exports.default = ListActions;