import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { Component } from 'react';
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
 */
import { jsx as _jsx } from "react/jsx-runtime";
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();
          });
        }
      }
    });
    _defineProperty(this, "registry", void 0);
    _defineProperty(this, "measure", void 0);
    this.registry = createRegistry();
    this.measure = () => {};
  }
  render() {
    const {
      baseClassName,
      className,
      children,
      ...props
    } = this.props;
    const toolbarItems = toToolbarItems(children);
    return /*#__PURE__*/_jsx(Squeezer, {
      compact: () => this.registry.compact(),
      uncompact: () => this.registry.uncompact(),
      innerMeasure: measure => {
        this.measure = measure;
      },
      children: _ref => {
        let {
          ref
        } = _ref;
        return /*#__PURE__*/_jsx("div", {
          ref: ref,
          className: classNames(baseClassName, className),
          ...props,
          children: /*#__PURE__*/_jsx(RegistryContext.Provider, {
            value: this.registry,
            children: /*#__PURE__*/_jsx(DistractionFreeModeContext.Provider, {
              value: this.state.distractionFreeMode,
              children: toolbarItems
            })
          })
        });
      }
    });
  }
}
_defineProperty(Toolbar, "defaultProps", {
  children: null,
  className: null,
  baseClassName: `${CLS_PREFIX}toolbar`
});
export default Toolbar;