"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(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"));

// Copyright 1999-2020. 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 = ({
  baseClassName = "".concat(_constants.CLS_PREFIX, "list-actions"),
  className,
  children,
  locale,
  ...props
}) => {
  const [overlay, setOverlay] = (0, _react.useState)(false);
  const vertical = (0, _react.useContext)(_VerticalContext.default);
  (0, _react.useEffect)(() => {
    const handler = ({
      matches
    }) => {
      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__*/_react.default.createElement("div", {
          className: "".concat(baseClassName, "__hidden-action"),
          key: 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__*/_react.default.createElement(_Dropdown.default, {
      className: "".concat(baseClassName, "__menu"),
      menu: /*#__PURE__*/_react.default.createElement(_Menu.default, null, secondaryItems),
      menuPlacement: "bottom-end",
      overlay: overlay,
      locale: locale
    }, /*#__PURE__*/_react.default.createElement(_Button.default, {
      ghost: true,
      icon: "kebab"
    }));
  } else if (secondaryCount) {
    secondaryDropdown = /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__menu ").concat(baseClassName, "__hidden-action")
    });
  }

  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    className: (0, _classnames.default)(baseClassName, className)
  }, props), primaryItems, secondaryDropdown);
};

var _default = ListActions;
exports.default = _default;