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 { useRegistryItem } from './RegistryContextBeta';
import { jsx as _jsx } from "react/jsx-runtime";
class ToolbarItem extends Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      compact: false
    });
    _defineProperty(this, "context", void 0);
  }
  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;
const ToolbarItemBeta = _ref => {
  let {
    children
  } = _ref;
  const [isCompact] = useRegistryItem();
  return /*#__PURE__*/_jsx(ResponsiveContext.Provider, {
    value: isCompact,
    children: children
  });
};
export const toToolbarItems = function (children) {
  let isBeta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  return Children.map(children, child => {
    if (! /*#__PURE__*/isValidElement(child)) {
      return child;
    }
    if (child.type === ToolbarGroup || child.type === ToolbarExpander) {
      return child;
    }
    const Component = isBeta ? ToolbarItemBeta : ToolbarItem;
    return /*#__PURE__*/_jsx(Component, {
      children: /*#__PURE__*/cloneElement(child, {
        className: classNames(child.props.className, `${CLS_PREFIX}toolbar__group-item`)
      })
    });
  });
};