"use strict";

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _ResponsiveContext = _interopRequireDefault(require("../ResponsiveContext"));

var _RegistryContext = _interopRequireDefault(require("./RegistryContext"));

var _ToolbarGroup = _interopRequireDefault(require("./ToolbarGroup"));

var _ToolbarExpander = _interopRequireDefault(require("./ToolbarExpander"));

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.
class ToolbarItem extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      compact: false
    });
  }

  componentDidMount() {
    const registry = this.context;
    registry.register(this);
  }

  componentWillUnmount() {
    const registry = this.context;
    registry.unregister(this);
  }

  isCompact() {
    return this.state.compact;
  }

  setCompact(compact) {
    this.setState({
      compact
    });
  }

  render() {
    const {
      children
    } = this.props;
    const {
      compact
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_ResponsiveContext.default.Provider, {
      value: compact
    }, children);
  }

}

ToolbarItem.contextType = _RegistryContext.default;

const toToolbarItems = children => _react.Children.map(children, child => {
  if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
    return child;
  }

  if (child.type === _ToolbarGroup.default || child.type === _ToolbarExpander.default) {
    return child;
  }

  return /*#__PURE__*/_react.default.createElement(ToolbarItem, null, /*#__PURE__*/(0, _react.cloneElement)(child, {
    className: (0, _classnames.default)(child.props.className, `${_constants.CLS_PREFIX}toolbar__group-item`)
  }));
});

exports.toToolbarItems = toToolbarItems;