"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
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(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// 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
 */

class ToolbarGroup extends _react.Component {
  constructor(props) {
    super(props);
    (0, _defineProperty2.default)(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
            });
          }
        }
      }
    });
    (0, _defineProperty2.default)(this, "registry", void 0);
    (0, _defineProperty2.default)(this, "selfApi", void 0);
    (0, _defineProperty2.default)(this, "childrenApi", void 0);
    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 => /*#__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: this.registry,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_DistractionFreeModeContext.default.Provider, {
          value: distractionFreeMode,
          children: (0, _ToolbarItem.toToolbarItems)(children)
        })
      });
    }
    if (distractionFreeMode.enabled && !availableInDistractionFreeMode) {
      return null;
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(baseClassName, className, {
        [`${baseClassName}--grow`]: hasExpander || distractionFreeMode.enabled && availableInDistractionFreeMode
      }),
      ...props,
      children: children
    });
  }
}
(0, _defineProperty2.default)(ToolbarGroup, "defaultProps", {
  registry: undefined,
  distractionFreeMode: undefined
});
const ToolbarGroupWrapper = _ref => {
  let {
    groupable = true,
    baseClassName = `${_constants.CLS_PREFIX}toolbar__group`,
    ...props
  } = _ref;
  const registry = (0, _react.useContext)(_RegistryContext.default);
  const distractionFreeMode = (0, _react.useContext)(_DistractionFreeModeContext.default);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(ToolbarGroup, {
    groupable: groupable,
    baseClassName: baseClassName,
    ...props,
    registry: registry,
    distractionFreeMode: distractionFreeMode
  });
};
ToolbarGroupWrapper.displayName = 'ToolbarGroup';
var _default = exports.default = ToolbarGroupWrapper;