import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { Component, isValidElement, Children, cloneElement } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import ResponsiveContext from '../ResponsiveContext';
import RegistryContext from './RegistryContext';
import ToolbarGroup from './ToolbarGroup';
import ToolbarExpander from './ToolbarExpander';

class ToolbarItem extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(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.createElement(ResponsiveContext.Provider, {
      value: compact
    }, children);
  }

}

ToolbarItem.contextType = RegistryContext;
export const toToolbarItems = children => Children.map(children, child => {
  if (! /*#__PURE__*/isValidElement(child)) {
    return child;
  }

  if (child.type === ToolbarGroup || child.type === ToolbarExpander) {
    return child;
  }

  return /*#__PURE__*/React.createElement(ToolbarItem, null, /*#__PURE__*/cloneElement(child, {
    className: classNames(child.props.className, `${CLS_PREFIX}toolbar__group-item`)
  }));
});