"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.RadioGroupDataTids = exports.RadioGroup = void 0;var _createForOfIteratorHelperLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelperLoose"));var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _invariant = _interopRequireDefault(require("invariant"));

var _utils = require("../../lib/utils");
var _Radio = require("../Radio");
var _createPropsGetter = require("../../lib/createPropsGetter");

var _FocusTrap = require("../../internal/FocusTrap");

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

var _rootNode = require("../../lib/rootNode");
var _getVisualStateDataAttributes = require("../../internal/CommonWrapper/utils/getVisualStateDataAttributes");

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

var _RadioGroup2 = require("./RadioGroup.styles");
var _Prevent = require("./Prevent");

var _RadioGroupContext = require("./RadioGroupContext");var _class, _RadioGroup;

































































var RadioGroupDataTids = exports.RadioGroupDataTids = {
  root: 'RadioGroup__root'
};



/** Группирует радиокнопки. Контейнер для компонента [Radio](https://tech.skbkontur.ru/kontur-ui/?path=/docs/react-ui_input-data-radio--docs). */var

RadioGroup = exports.RadioGroup = (0, _rootNode.rootNode)(_class = (_RadioGroup = /*#__PURE__*/function (_React$Component) {
















  function RadioGroup(props) {var _this;
    _this = _React$Component.call(this, props) || this;_this.name = (0, _utils.getRandomID)();_this.getProps = (0, _createPropsGetter.createPropsGetter)(RadioGroup.defaultProps);_this.






    getRadioGroupContextValue = function () {
      return {
        activeItem: _this.getValue(),
        onSelect: _this.handleSelect,
        name: _this.getName(),
        disabled: _this.props.disabled,
        error: _this.props.error,
        warning: _this.props.warning
      };
    };_this.












































































    getValue = function () {return _this.isControlled() ? _this.props.value : _this.state.activeItem;};_this.

    getName = function () {return _this.props.name || _this.name;};_this.

    isControlled = function () {return (0, _utils.isNonNullable)(_this.props.value);};_this.

    handleSelect = function (value) {
      if (!_this.isControlled()) {
        _this.setState({ activeItem: value });
      }
      if (_this.props.onValueChange) {
        _this.props.onValueChange(value);
      }
    };_this.







    renderRadio = function (itemValue, data, index) {var _cx;
      var itemProps = {
        key: _this.getKeyByItem(itemValue),
        className: (0, _Emotion.cx)((_cx = {}, _cx[
        _RadioGroup2.styles.item()] = true, _cx[
        _RadioGroup2.styles.itemFirst()] = index === 0, _cx[
        _RadioGroup2.styles.itemInline()] = !!_this.props.inline, _cx)
        )
      };

      return /*#__PURE__*/(
        _react.default.createElement("span", (0, _extends2.default)({}, itemProps, { role: "presentation" }), /*#__PURE__*/
        _react.default.createElement(_Radio.Radio, { value: itemValue }, _this.getProps().renderItem(itemValue, data))
        ));

    };_this.

    getKeyByItem = function (itemValue) {
      if (_this.props.toKey) {
        return _this.props.toKey(itemValue);
      }
      return typeof itemValue === 'string' || typeof itemValue === 'number' ? itemValue : undefined;
    };_this.

    ref = function (element) {
      _this.node = element;
    };_this.state = { activeItem: _this.props.defaultValue };return _this;}(0, _inheritsLoose2.default)(RadioGroup, _React$Component);var _proto = RadioGroup.prototype;_proto.render = function render() {var _this2 = this;var _this$props = this.props,width = _this$props.width,onMouseLeave = _this$props.onMouseLeave,onMouseOver = _this$props.onMouseOver,onMouseEnter = _this$props.onMouseEnter,onBlur = _this$props.onBlur,ariaDescribedby = _this$props['aria-describedby'],disabled = _this$props.disabled;var style = { width: width != null ? width : 'auto' };var handlers = { onMouseOver: onMouseOver, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave };return /*#__PURE__*/_react.default.createElement(_featureFlagsContext.ReactUIFeatureFlagsContext.Consumer, null, function (flags) {var _cx2;_this2.featureFlags = (0, _featureFlagsContext.getFullReactUIFlagsContext)(flags);return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: _this2.setRootNode }, _this2.props, (0, _getVisualStateDataAttributes.getVisualStateDataAttributes)({ disabled: disabled })), /*#__PURE__*/_react.default.createElement(_FocusTrap.FocusTrap, { onBlur: onBlur }, /*#__PURE__*/_react.default.createElement("span", (0, _extends2.default)({ "data-tid": RadioGroupDataTids.root, ref: _this2.ref, style: style, className: (0, _Emotion.cx)((_cx2 = {}, _cx2[_RadioGroup2.styles.root()] = true, _cx2[_RadioGroup2.styles.removeBaselineSpacer()] = _this2.featureFlags.radioGroupRemoveBaselineSpacer, _cx2)), role: "radiogroup" }, handlers, { "aria-describedby": ariaDescribedby }), /*#__PURE__*/_react.default.createElement(_RadioGroupContext.RadioGroupContext.Provider, { value: _this2.getRadioGroupContextValue() }, _this2.renderChildren()))));});} /** Программно устанавливает фокус на радиокнопку. Появляется фокусная обводка, элемент получает клавиатурные события и воспринимается как текущий элемент для чтения скринридерами.
   * @public
   */;_proto.focus = function focus() {var node = this.node;if (!node) {return;}var radio = node.querySelector('input[type="radio"]:checked'); // If no checked radios, try get first radio
    if (!radio || radio.disabled) {radio = node.querySelector('input[type="radio"]:not([disabled])');}if (radio) {radio.focus();}};_proto.renderChildren = function renderChildren() {var _this$props2 = this.props,items = _this$props2.items,children = _this$props2.children;(0, _invariant.default)(!items && children || items && !children, 'Either items or children must be passed, not both');return items ? mapItems(this.renderRadio, items) : children;};return RadioGroup;}(_react.default.Component), _RadioGroup.__KONTUR_REACT_UI__ = 'RadioGroup', _RadioGroup.displayName = 'RadioGroup', _RadioGroup.defaultProps = { renderItem: renderItem }, _RadioGroup.Prevent = _Prevent.Prevent, _RadioGroup)) || _class;function renderItem(_value, data) {
  return data;
}

function mapItems(
fn,
items)
{
  var result = [];
  var index = 0;
  for (var _iterator = (0, _createForOfIteratorHelperLoose2.default)(items), _step; !(_step = _iterator()).done;) {var entry = _step.value;
    var _normalizeEntry = normalizeEntry(entry),value = _normalizeEntry[0],data = _normalizeEntry[1];
    result.push(fn(value, data, index));
    ++index;
  }
  return result;
}

function normalizeEntry(entry) {
  if (!Array.isArray(entry)) {
    return [entry, entry];
  }
  return entry;
}