import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import RegistryContext, { createRegistry } from './RegistryContext';
import DistractionFreeModeContext from '../DistractionFreeModeContext';
import { toToolbarItems } from './ToolbarItem';
import Squeezer from '../Squeezer';

/**
 * `Toolbar` component is a panel containing various instruments used for managing the contents of a large content block.
 * Toolbar is located on top of the managed content block.
 * It typically consists of three main parts: controls that add new elements, controls that work with existing elements,
 * and controls for changing the way the content block looks.
 * @since 0.0.57
 */
class Toolbar extends Component {
  constructor(props) {
    super(props);

    _defineProperty(this, "state", {
      distractionFreeMode: {
        enabled: false,
        toggle: () => {
          this.setState(prevState => ({
            distractionFreeMode: { ...prevState.distractionFreeMode,
              enabled: !prevState.distractionFreeMode.enabled
            }
          }), () => {
            this.measure();
          });
        }
      }
    });

    this.registry = createRegistry();

    this.measure = () => {};
  }

  render() {
    const {
      baseClassName,
      className,
      children,
      ...props
    } = this.props;
    const toolbarItems = toToolbarItems(children);
    return /*#__PURE__*/React.createElement(Squeezer, {
      compact: () => this.registry.compact(),
      uncompact: () => this.registry.uncompact(),
      innerMeasure: measure => {
        this.measure = measure;
      }
    }, ({
      ref
    }) => /*#__PURE__*/React.createElement("div", _extends({
      ref: ref,
      className: classNames(baseClassName, className)
    }, props), /*#__PURE__*/React.createElement(RegistryContext.Provider, {
      value: this.registry
    }, /*#__PURE__*/React.createElement(DistractionFreeModeContext.Provider, {
      value: this.state.distractionFreeMode
    }, toolbarItems))));
  }

}

Toolbar.propTypes = {
  /**
   * Content of the `Toolbar`.
   * @since 0.0.57
   */
  children: PropTypes.node,

  /**
   * @ignore
   */
  className: PropTypes.string,

  /**
   * @ignore
   */
  baseClassName: PropTypes.string
};
Toolbar.defaultProps = {
  children: null,
  className: null,
  baseClassName: "".concat(CLS_PREFIX, "toolbar")
};
export default Toolbar;