"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.getButtonCorners = exports.GroupDataTids = exports.Group = void 0;var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _react = _interopRequireDefault(require("react"));

var _client = require("../../lib/client");

var _CommonWrapper = require("../../internal/CommonWrapper");
var _Emotion = require("../../lib/theming/Emotion");

var _rootNode = require("../../lib/rootNode");
var _utils = require("../../lib/utils");













var _featureFlagsContext = require("../../lib/featureFlagsContext");

var _Group2 = require("./Group.styles");var _class, _Group;











var getFirstChild = function getFirstChild(children) {
  if (!Array.isArray(children)) {
    return children;
  }

  return children == null ? void 0 : children[0];
};

var getLastChild = function getLastChild(children) {
  if (!Array.isArray(children)) {
    return children;
  }

  var numberOfChildren = _react.default.Children.count(children);

  return children == null ? void 0 : children[numberOfChildren - 1];
};

var getButtonCorners = exports.getButtonCorners = function getButtonCorners(isFirstChild, isLastChild) {
  if (isFirstChild && isLastChild) {
    return {};
  }

  if (isFirstChild) {
    return {
      borderTopRightRadius: 0,
      borderBottomRightRadius: 0
    };
  }

  if (isLastChild) {
    return {
      borderTopLeftRadius: 0,
      borderBottomLeftRadius: 0
    };
  }

  return {
    borderRadius: 0
  };
};

var tryPassCorners = function tryPassCorners(child, isFirstChild, isLastChild) {
  var corners = getButtonCorners(isFirstChild, isLastChild);

  if (
  (0, _utils.isButton)(child) ||
  (0, _utils.isInput)(child) ||
  (0, _utils.isFxInput)(child) ||
  (0, _utils.isAutocomplete)(child) ||
  (0, _utils.isPasswordInput)(child) ||
  (0, _utils.isCurrencyInput)(child) ||
  (0, _utils.isSelect)(child) ||
  (0, _utils.isDropdown)(child) ||
  (0, _utils.isDropdownMenu)(child))
  {
    return /*#__PURE__*/_react.default.cloneElement(child, { corners: (0, _extends2.default)({}, corners, child.props.corners) });
  }

  return child;
};

var hasWidthInPercent = function hasWidthInPercent(child) {return String(child.props.width).includes('%');};

var GroupDataTids = exports.GroupDataTids = {
  root: 'Group__root'
};

/**
 *  Компонент `Group` используется для создания логически сгруппированных элементов, выровненных по горизонтали.
 *
 *  Длина контейнера настраивается с помощью пропа `width`.
 */var

Group = exports.Group = (0, _rootNode.rootNode)(_class = (_Group = /*#__PURE__*/function (_React$Component) {function Group() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;_this.













































    renderChild = function (
    child,
    isFirstChild,
    isLastChild,
    isWidthInPercent) {var _cx, _cx2;return /*#__PURE__*/(

        _react.default.createElement("div", {
          className: (0, _Emotion.cx)((_cx = {}, _cx[
          _Group2.styles.fixed()] = !isWidthInPercent, _cx[
          _Group2.styles.stretch()] = isWidthInPercent, _cx[
          _Group2.styles.stretchFallback()] = Boolean(isWidthInPercent && _this.props.width && (_client.isIE11 || _client.isEdge)), _cx)
          ) }, /*#__PURE__*/

        _react.default.createElement("div", {
          className: (0, _Emotion.cx)((_cx2 = {}, _cx2[
          _Group2.styles.item()] = true, _cx2[
          _Group2.styles.itemFirst()] = isFirstChild, _cx2)
          ) },

        tryPassCorners(child, isFirstChild, isLastChild)
        )
        ));};return _this;}(0, _inheritsLoose2.default)(Group, _React$Component);var _proto = Group.prototype;_proto.render = function render() {var _this2 = this;var style = { width: this.props.width };var childrenArray = _react.default.Children.toArray(this.props.children);var firstChild = getFirstChild(childrenArray);var lastChild = getLastChild(childrenArray);return /*#__PURE__*/_react.default.createElement(_featureFlagsContext.ReactUIFeatureFlagsContext.Consumer, null, function (flags) {_this2.featureFlags = (0, _featureFlagsContext.getFullReactUIFlagsContext)(flags);return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: _this2.setRootNode }, _this2.props), /*#__PURE__*/_react.default.createElement("span", { "data-tid": GroupDataTids.root, className: _Group2.styles.root(), style: style }, _react.default.Children.map(childrenArray, function (child) {if (!child || ! /*#__PURE__*/_react.default.isValidElement(child)) {return null;}var isFirstChild = child === firstChild;var isLastChild = child === lastChild;if (_this2.featureFlags.groupAddHintAndTooltipSupport && ((0, _utils.isHint)(child) || (0, _utils.isTooltip)(child))) {return _this2.renderWrappedChildren(child, isFirstChild, isLastChild);}return _this2.renderChild(child, isFirstChild, isLastChild, hasWidthInPercent(child));})));});};_proto.


  renderWrappedChildren = function renderWrappedChildren(
  parent,
  isParentFirst,
  isParentLast)
  {var _this3 = this,_cx3;
    var shouldStretchParent = false;
    var nestedChildren = _react.default.Children.toArray(parent.props.children);

    var modifiedChildren = nestedChildren.map(function (nestedChild, index) {
      if (!nestedChild || ! /*#__PURE__*/_react.default.isValidElement(nestedChild)) {
        return null;
      }

      var isFirstChild = isParentFirst && index === 0;
      var isLastChild = isParentLast && index === nestedChildren.length - 1;
      if (hasWidthInPercent(nestedChild)) {
        shouldStretchParent = true;
      }

      return _this3.renderChild(nestedChild, isFirstChild, isLastChild, hasWidthInPercent(nestedChild));
    });

    var wrappedChildren = /*#__PURE__*/
    _react.default.createElement("div", { className: (0, _Emotion.cx)(_Group2.styles.wrappedChildren(), (_cx3 = {}, _cx3[_Group2.styles.stretch()] = shouldStretchParent, _cx3)) },
    modifiedChildren
    );


    return /*#__PURE__*/_react.default.cloneElement(parent, {
      children: wrappedChildren
    });
  };return Group;}(_react.default.Component), _Group.__KONTUR_REACT_UI__ = 'Group', _Group.displayName = 'Group', _Group)) || _class;