"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 _Dropdown = _interopRequireDefault(require("../Dropdown"));

var _ToolbarMenu = _interopRequireDefault(require("./ToolbarMenu"));

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

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

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

var _ToolbarExpander = _interopRequireDefault(require("./ToolbarExpander"));

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

/**
 * `ToolbarGroup` is a part of [Toolbar](#!/Toolbar) component.
 * It is used for grouping several toolbar controls together.
 * @since 0.0.57
 */
class ToolbarGroup extends _react.Component {
  constructor(props) {
    super(props);
    (0, _defineProperty2.default)(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 = (0, _RegistryContext.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 = _react.Children.toArray(children).some(child => child && child.type === _ToolbarExpander.default);

    if (groupable && compact && _react.Children.count(children) > 1) {
      hasExpander = false;
      children = /*#__PURE__*/_react.default.createElement(_Dropdown.default, {
        menu: /*#__PURE__*/_react.default.createElement(_ToolbarMenu.default, null, children)
      }, title);
    } else {
      children = /*#__PURE__*/_react.default.createElement(_RegistryContext.default.Provider, {
        value: this.registry
      }, /*#__PURE__*/_react.default.createElement(_DistractionFreeModeContext.default.Provider, {
        value: distractionFreeMode
      }, (0, _ToolbarItem.toToolbarItems)(children)));
    }

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

    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, className, {
        ["".concat(baseClassName, "--grow")]: hasExpander || distractionFreeMode.enabled && availableInDistractionFreeMode
      })
    }, props), children);
  }

}

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

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

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

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

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

const ToolbarGroupWrapper = props => {
  const registry = (0, _react.useContext)(_RegistryContext.default);
  const distractionFreeMode = (0, _react.useContext)(_DistractionFreeModeContext.default);
  return /*#__PURE__*/_react.default.createElement(ToolbarGroup, (0, _extends2.default)({}, props, {
    registry: registry,
    distractionFreeMode: distractionFreeMode
  }));
};

ToolbarGroupWrapper.displayName = 'ToolbarGroup';
ToolbarGroupWrapper.propTypes = propTypes;
ToolbarGroupWrapper.defaultProps = defaultProps;
var _default = ToolbarGroupWrapper;
exports.default = _default;