import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import _extends from "@babel/runtime/helpers/esm/extends";
var _class, _Group;
import React from 'react';
import { isIE11, isEdge } from "../../../lib/client";
import { CommonWrapper } from "../../../internal/CommonWrapper";
import { cx } from "../../../lib/theming/Emotion";
import { rootNode } from "../../../lib/rootNode";
import { isButton, isAutocomplete, isCurrencyInput, isDropdown, isDropdownMenu, isFxInput, isInput, isPasswordInput, isSelect, isTooltip, isHint } from "../../../lib/utils";
import { getFullReactUIFlagsContext, ReactUIFeatureFlagsContext } from "../../../lib/featureFlagsContext";
import { styles } from "../Group.styles";
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.Children.count(children);
  return children == null ? void 0 : children[numberOfChildren - 1];
};
export var 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 (isButton(child) || isInput(child) || isFxInput(child) || isAutocomplete(child) || isPasswordInput(child) || isCurrencyInput(child) || isSelect(child) || isDropdown(child) || isDropdownMenu(child)) {
    return /*#__PURE__*/React.cloneElement(child, {
      corners: _extends({}, corners, child.props.corners)
    });
  }
  return child;
};
var hasWidthInPercent = function hasWidthInPercent(child) {
  return String(child.props.width).includes('%');
};
export var GroupDataTids = {
  root: 'Group__root'
};

/**
 *  Компонент `Group` используется для создания логически сгруппированных элементов, выровненных по горизонтали.
 *
 *  Длина контейнера настраивается с помощью пропа `width`.
 */
export var Group = 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.createElement("div", {
          className: cx((_cx = {}, _cx[styles.fixed()] = !isWidthInPercent, _cx[styles.stretch()] = isWidthInPercent, _cx[styles.stretchFallback()] = Boolean(isWidthInPercent && _this.props.width && (isIE11 || isEdge)), _cx))
        }, /*#__PURE__*/

        React.createElement("div", {
          className: cx((_cx2 = {}, _cx2[styles.item()] = true, _cx2[styles.itemFirst()] = isFirstChild, _cx2))
        }, tryPassCorners(child, isFirstChild, isLastChild)))
      );
    };
    return _this;
  }
  _inheritsLoose(Group, _React$Component);
  var _proto = Group.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var style = {
      width: this.props.width
    };
    var childrenArray = React.Children.toArray(this.props.children);
    var firstChild = getFirstChild(childrenArray);
    var lastChild = getLastChild(childrenArray);
    return /*#__PURE__*/React.createElement(ReactUIFeatureFlagsContext.Consumer, null, function (flags) {
      _this2.featureFlags = getFullReactUIFlagsContext(flags);
      return /*#__PURE__*/React.createElement(CommonWrapper, _extends({
        rootNodeRef: _this2.setRootNode
      }, _this2.props), /*#__PURE__*/React.createElement("span", {
        "data-tid": GroupDataTids.root,
        className: styles.root(),
        style: style
      }, React.Children.map(childrenArray, function (child) {
        if (!child || ! /*#__PURE__*/React.isValidElement(child)) {
          return null;
        }
        var isFirstChild = child === firstChild;
        var isLastChild = child === lastChild;
        if (_this2.featureFlags.groupAddHintAndTooltipSupport && (isHint(child) || 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.Children.toArray(parent.props.children);
    var modifiedChildren = nestedChildren.map(function (nestedChild, index) {
      if (!nestedChild || ! /*#__PURE__*/React.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.createElement("div", {
      className: cx(styles.wrappedChildren(), (_cx3 = {}, _cx3[styles.stretch()] = shouldStretchParent, _cx3))
    }, modifiedChildren);
    return /*#__PURE__*/React.cloneElement(parent, {
      children: wrappedChildren
    });
  };
  return Group;
}(React.Component), _Group.__KONTUR_REACT_UI__ = 'Group', _Group.displayName = 'Group', _Group)) || _class;