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

import { Component, Children, useContext, isValidElement } from 'react';
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
 */
import { jsx as _jsx } from "react/jsx-runtime";
class ToolbarGroup extends Component {
  constructor(props) {
    super(props);
    _defineProperty(this, "state", {
      compact: false,
      availableInDistractionFreeMode: false,
      distractionFreeMode: {
        enabled: false,
        toggle: () => {
          if (this.props.distractionFreeMode) {
            this.props.distractionFreeMode.toggle();
            this.setState({
              availableInDistractionFreeMode: !this.props.distractionFreeMode.enabled
            });
          }
        }
      }
    });
    _defineProperty(this, "registry", void 0);
    _defineProperty(this, "selfApi", void 0);
    _defineProperty(this, "childrenApi", void 0);
    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 => /*#__PURE__*/isValidElement(child) && child.type === ToolbarExpander);
    if (groupable && compact && Children.count(children) > 1) {
      hasExpander = false;
      children = /*#__PURE__*/_jsx(Dropdown, {
        menu: /*#__PURE__*/_jsx(ToolbarMenu, {
          onItemClick: () => {},
          children: children
        }),
        children: title
      });
    } else {
      children = /*#__PURE__*/_jsx(RegistryContext.Provider, {
        value: this.registry,
        children: /*#__PURE__*/_jsx(DistractionFreeModeContext.Provider, {
          value: distractionFreeMode,
          children: toToolbarItems(children)
        })
      });
    }
    if (distractionFreeMode.enabled && !availableInDistractionFreeMode) {
      return null;
    }
    return /*#__PURE__*/_jsx("div", {
      className: classNames(baseClassName, className, {
        [`${baseClassName}--grow`]: hasExpander || distractionFreeMode.enabled && availableInDistractionFreeMode
      }),
      ...props,
      children: children
    });
  }
}
_defineProperty(ToolbarGroup, "defaultProps", {
  registry: undefined,
  distractionFreeMode: undefined
});
const ToolbarGroupWrapper = _ref => {
  let {
    groupable = true,
    baseClassName = `${CLS_PREFIX}toolbar__group`,
    ...props
  } = _ref;
  const registry = useContext(RegistryContext);
  const distractionFreeMode = useContext(DistractionFreeModeContext);
  return /*#__PURE__*/_jsx(ToolbarGroup, {
    groupable: groupable,
    baseClassName: baseClassName,
    ...props,
    registry: registry,
    distractionFreeMode: distractionFreeMode
  });
};
ToolbarGroupWrapper.displayName = 'ToolbarGroup';
export default ToolbarGroupWrapper;