"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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

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

var _DistractionFreeModeContext = _interopRequireDefault(require("../DistractionFreeModeContext"));

var _ToolbarItem = require("./ToolbarItem");

var _Squeezer = _interopRequireDefault(require("../Squeezer"));

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/**
 * `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 _react.Component {
  constructor(props) {
    super(props);
    (0, _defineProperty2.default)(this, "state", {
      distractionFreeMode: {
        enabled: false,
        toggle: () => {
          this.setState(prevState => ({
            distractionFreeMode: { ...prevState.distractionFreeMode,
              enabled: !prevState.distractionFreeMode.enabled
            }
          }), () => {
            this.measure();
          });
        }
      }
    });
    this.registry = (0, _RegistryContext.createRegistry)();

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

  render() {
    const {
      baseClassName,
      className,
      children,
      ...props
    } = this.props;
    const toolbarItems = (0, _ToolbarItem.toToolbarItems)(children);
    return /*#__PURE__*/_react.default.createElement(_Squeezer.default, {
      compact: () => this.registry.compact(),
      uncompact: () => this.registry.uncompact(),
      innerMeasure: measure => {
        this.measure = measure;
      }
    }, ({
      ref
    }) => /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      ref: ref,
      className: (0, _classnames.default)(baseClassName, className)
    }, props), /*#__PURE__*/_react.default.createElement(_RegistryContext.default.Provider, {
      value: this.registry
    }, /*#__PURE__*/_react.default.createElement(_DistractionFreeModeContext.default.Provider, {
      value: this.state.distractionFreeMode
    }, toolbarItems))));
  }

}

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

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string
};
Toolbar.defaultProps = {
  children: null,
  className: null,
  baseClassName: "".concat(_constants.CLS_PREFIX, "toolbar")
};
var _default = Toolbar;
exports.default = _default;