"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
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"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// Copyright 1999-2023. 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
 */
const ToolbarGroup = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}toolbar__group`,
    className,
    title,
    children,
    groupable = true,
    ...props
  } = _ref;
  const [compact] = (0, _RegistryContext.useRegistryItem)();
  const registry = (0, _RegistryContext.useRegistry)();
  const [availableInDistractionFreeMode, setAvailableInDistractionFreeMode] = (0, _react.useState)(false);
  const distractionFreeMode = (0, _react.useContext)(_DistractionFreeModeContext.default);
  const distractionFreeModeContextValue = {
    enabled: distractionFreeMode?.enabled ?? false,
    toggle: () => {
      if (distractionFreeMode) {
        distractionFreeMode.toggle();
        setAvailableInDistractionFreeMode(current => !current);
      }
    }
  };
  let hasExpander = _react.Children.toArray(children).some(child => /*#__PURE__*/(0, _react.isValidElement)(child) && child.type === _ToolbarExpander.default);
  if (groupable && compact && _react.Children.count(children) > 1) {
    hasExpander = false;
    children = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.default, {
      menu: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ToolbarMenu.default, {
        onItemClick: () => {},
        children: children
      }),
      children: title
    });
  } else {
    children = /*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistryContext.default.Provider, {
      value: registry,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_DistractionFreeModeContext.default.Provider, {
        value: distractionFreeModeContextValue,
        children: (0, _ToolbarItem.toToolbarItems)(children)
      })
    });
  }
  if (distractionFreeModeContextValue.enabled && !availableInDistractionFreeMode) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)(baseClassName, className, {
      [`${baseClassName}--grow`]: hasExpander || distractionFreeModeContextValue.enabled && availableInDistractionFreeMode
    }),
    ...props,
    children: children
  });
};
var _default = exports.default = ToolbarGroup;