"use strict";

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"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// 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 = `${_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: `${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: `${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: `${baseClassName}__menu ${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;