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, Children, useContext } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Dropdown from '../Dropdown';
import ToolbarMenu from './ToolbarMenu';
import RegistryContext, { createRegistry } from './RegistryContext';
import DistractionFreeModeContext from '../DistractionFreeModeContext';
import { toToolbarItems } from './ToolbarItem';
import ToolbarExpander from './ToolbarExpander';
/**
 * `ToolbarGroup` is a part of [Toolbar](#!/Toolbar) component.
 * It is used for grouping several toolbar controls together.
 * @since 0.0.57
 */

class ToolbarGroup extends Component {
  constructor(props) {
    super(props);

    _defineProperty(this, "state", {
      compact: false,
      distractionFreeMode: {
        enabled: false,
        toggle: () => {
          if (this.props.distractionFreeMode) {
            this.props.distractionFreeMode.toggle();
            this.setState({
              availableInDistractionFreeMode: !this.props.distractionFreeMode.enabled
            });
          }
        }
      }
    });

    this.registry = createRegistry();
    this.selfApi = {
      isCompact: () => this.state.compact,
      setCompact: compact => {
        this.setState({
          compact
        });
      }
    };
    this.childrenApi = {
      isCompact: one => this.registry.isCompact(one),
      setCompact: compact => {
        if (compact) {
          this.registry.compact();
        } else {
          this.registry.uncompact();
        }
      }
    };
  }

  static getDerivedStateFromProps(props, state) {
    if (props.distractionFreeMode) {
      return {
        distractionFreeMode: { ...state.distractionFreeMode,
          enabled: props.distractionFreeMode.enabled
        }
      };
    }

    return null;
  }

  componentDidMount() {
    const {
      registry
    } = this.props;

    if (registry) {
      registry.register(this.selfApi);
      registry.register(this.childrenApi);
    }
  }

  componentWillUnmount() {
    const {
      registry
    } = this.props;

    if (registry) {
      registry.unregister(this.selfApi);
      registry.unregister(this.childrenApi);
    }
  }

  render() {
    const {
      baseClassName,
      className,
      title,
      children: childrenProp,
      distractionFreeMode: distractionFreeModeProp,
      registry,
      groupable,
      ...props
    } = this.props;
    let {
      children
    } = this.props;
    const {
      compact,
      distractionFreeMode,
      availableInDistractionFreeMode
    } = this.state;
    let hasExpander = Children.toArray(children).some(child => child && child.type === ToolbarExpander);

    if (groupable && compact && Children.count(children) > 1) {
      hasExpander = false;
      children = /*#__PURE__*/React.createElement(Dropdown, {
        menu: /*#__PURE__*/React.createElement(ToolbarMenu, null, children)
      }, title);
    } else {
      children = /*#__PURE__*/React.createElement(RegistryContext.Provider, {
        value: this.registry
      }, /*#__PURE__*/React.createElement(DistractionFreeModeContext.Provider, {
        value: distractionFreeMode
      }, toToolbarItems(children)));
    }

    if (distractionFreeMode.enabled && !availableInDistractionFreeMode) {
      return null;
    }

    return /*#__PURE__*/React.createElement("div", _extends({
      className: classNames(baseClassName, className, {
        ["".concat(baseClassName, "--grow")]: hasExpander || distractionFreeMode.enabled && availableInDistractionFreeMode
      })
    }, props), children);
  }

}

const propTypes = {
  /**
   * Title.
   * @since 0.0.60
   */
  title: PropTypes.node.isRequired,

  /**
   * Whether to group children into a dropdown or not.
   * @since 3.8.0
   */
  groupable: PropTypes.bool,

  /**
   * Content of the `ToolbarGroup`.
   * @since 0.0.57
   */
  children: PropTypes.node,

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

  /**
   * @ignore
   */
  baseClassName: PropTypes.string
};
const defaultProps = {
  children: undefined,
  className: undefined,
  baseClassName: "".concat(CLS_PREFIX, "toolbar__group"),
  groupable: true
};
ToolbarGroup.propTypes = { ...propTypes,
  registry: PropTypes.shape({
    register: PropTypes.func.isRequired,
    unregister: PropTypes.func.isRequired
  }),
  distractionFreeMode: PropTypes.shape({
    enabled: PropTypes.bool.isRequired,
    toggle: PropTypes.func.isRequired
  })
};
ToolbarGroup.defaultProps = {
  registry: undefined,
  distractionFreeMode: undefined
};

const ToolbarGroupWrapper = props => {
  const registry = useContext(RegistryContext);
  const distractionFreeMode = useContext(DistractionFreeModeContext);
  return /*#__PURE__*/React.createElement(ToolbarGroup, _extends({}, props, {
    registry: registry,
    distractionFreeMode: distractionFreeMode
  }));
};

ToolbarGroupWrapper.displayName = 'ToolbarGroup';
ToolbarGroupWrapper.propTypes = propTypes;
ToolbarGroupWrapper.defaultProps = defaultProps;
export default ToolbarGroupWrapper;