var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { rootNode } from '../../lib/rootNode/index.js';
import { isButton, isAutocomplete, isCurrencyInput, isDropdown, isDropdownMenu, isFxInput, isInput, isPasswordInput, isSelect, isTooltip, isHint, } from '../../lib/utils.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles } from './Group.styles.js';
var getFirstChild = function (children) {
    if (!Array.isArray(children)) {
        return children;
    }
    return children === null || children === void 0 ? void 0 : children[0];
};
var getLastChild = function (children) {
    if (!Array.isArray(children)) {
        return children;
    }
    var numberOfChildren = React.Children.count(children);
    return children === null || children === void 0 ? void 0 : children[numberOfChildren - 1];
};
export var getButtonCorners = function (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 (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 React.cloneElement(child, { corners: __assign(__assign({}, corners), child.props.corners) });
    }
    return child;
};
var hasWidthInPercent = function (child) { return String(child.props.width).includes('%'); };
export var GroupDataTids = {
    root: 'Group__root',
};
/**
 *  Компонент `Group` используется для создания логически сгруппированных элементов, выровненных по горизонтали.
 *
 *  Длина контейнера настраивается с помощью пропа `width`.
 */
var Group = /** @class */ (function (_super) {
    __extends(Group, _super);
    function Group() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.renderChild = function (child, isFirstChild, isLastChild, isWidthInPercent) {
            var _a, _b;
            return (React.createElement("div", { className: _this.cx((_a = {},
                    _a[_this.styles.fixed()] = !isWidthInPercent,
                    _a[_this.styles.stretch()] = isWidthInPercent,
                    _a)) },
                React.createElement("div", { className: _this.cx((_b = {},
                        _b[_this.styles.item()] = true,
                        _b[_this.styles.itemFirst()] = isFirstChild,
                        _b)) }, tryPassCorners(child, isFirstChild, isLastChild))));
        };
        return _this;
    }
    Group.prototype.render = function () {
        var _this = this;
        var style = {
            width: this.props.width,
        };
        var childrenArray = React.Children.toArray(this.props.children);
        var firstChild = getFirstChild(childrenArray);
        var lastChild = getLastChild(childrenArray);
        this.styles = getStyles(this.emotion);
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props),
            React.createElement("span", { "data-tid": GroupDataTids.root, className: this.styles.root(), style: style }, React.Children.map(childrenArray, function (child) {
                if (!child || !React.isValidElement(child)) {
                    return null;
                }
                var isFirstChild = child === firstChild;
                var isLastChild = child === lastChild;
                if (isHint(child) || isTooltip(child)) {
                    return _this.renderWrappedChildren(child, isFirstChild, isLastChild);
                }
                return _this.renderChild(child, isFirstChild, isLastChild, hasWidthInPercent(child));
            }))));
    };
    Group.prototype.renderWrappedChildren = function (parent, isParentFirst, isParentLast) {
        var _a;
        var _this = this;
        var shouldStretchParent = false;
        var nestedChildren = React.Children.toArray(parent.props.children);
        var modifiedChildren = nestedChildren.map(function (nestedChild, index) {
            if (!nestedChild || !React.isValidElement(nestedChild)) {
                return null;
            }
            var isFirstChild = isParentFirst && index === 0;
            var isLastChild = isParentLast && index === nestedChildren.length - 1;
            if (hasWidthInPercent(nestedChild)) {
                shouldStretchParent = true;
            }
            return _this.renderChild(nestedChild, isFirstChild, isLastChild, hasWidthInPercent(nestedChild));
        });
        var wrappedChildren = (React.createElement("div", { className: this.cx(this.styles.wrappedChildren(), (_a = {}, _a[this.styles.stretch()] = shouldStretchParent, _a)) }, modifiedChildren));
        return React.cloneElement(parent, {
            children: wrappedChildren,
        });
    };
    Group.__KONTUR_REACT_UI__ = 'Group';
    Group.displayName = 'Group';
    Group = __decorate([
        withRenderEnvironment,
        rootNode
    ], Group);
    return Group;
}(React.Component));
export { Group };
//# sourceMappingURL=Group.js.map