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

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.

/**
 * `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: `${_constants.CLS_PREFIX}toolbar`
};
var _default = Toolbar;
exports.default = _default;