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

import { Component, isValidElement, Children, cloneElement } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import ResponsiveContext from '../ResponsiveContext';
import RegistryContext from './RegistryContext';
import ToolbarGroup from './ToolbarGroup';
import ToolbarExpander from './ToolbarExpander';
import { jsx as _jsx } from "react/jsx-runtime";
class ToolbarItem extends Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      compact: false
    });
  }
  componentDidMount() {
    const registry = this.context;
    registry.register(this);
  }
  componentWillUnmount() {
    const registry = this.context;
    registry.unregister(this);
  }
  isCompact() {
    return this.state.compact;
  }
  setCompact(compact) {
    this.setState({
      compact
    });
  }
  render() {
    const {
      children
    } = this.props;
    const {
      compact
    } = this.state;
    return /*#__PURE__*/_jsx(ResponsiveContext.Provider, {
      value: compact,
      children: children
    });
  }
}
ToolbarItem.contextType = RegistryContext;
export const toToolbarItems = children => Children.map(children, child => {
  if (! /*#__PURE__*/isValidElement(child)) {
    return child;
  }
  if (child.type === ToolbarGroup || child.type === ToolbarExpander) {
    return child;
  }
  return /*#__PURE__*/_jsx(ToolbarItem, {
    children: /*#__PURE__*/cloneElement(child, {
      className: classNames(child.props.className, `${CLS_PREFIX}toolbar__group-item`)
    })
  });
});