"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Radio = Radio;
exports.RadioGroupContext = RadioGroupContext;
exports.RadioGroupLayer = RadioGroupLayer;
exports["default"] = RadioGroup;

var _react = _interopRequireWildcard(require("react"));

var _RadioGroupModule = _interopRequireDefault(require("./RadioGroup.module.css"));

var _getCompliedClassName11 = _interopRequireDefault(require("../../utils/getCompliedClassName"));

var _getCompliedCustomId = _interopRequireDefault(require("../../utils/getCompliedCustomId"));

var _RadioGroup = _interopRequireWildcard(require("./RadioGroup.propTypes"));

var _RadioGroup2 = _interopRequireWildcard(require("./RadioGroup.constantProps"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function RadioGroup(props) {
  var children = props.children,
      selectedValue = props.selectedValue,
      onChange = props.onChange,
      name = props.name,
      onChangeCallbackHandler = props.onChangeCallbackHandler,
      radioCondition = props.radioCondition;

  var renderChildren = _react["default"].Children.map(children, function (child, i) {
    return /*#__PURE__*/_react["default"].cloneElement(child, {
      key: i,
      name: name,
      selectedValue: selectedValue,
      onChange: onChangeCallbackHandler(onChange, child.props.onChange),
      radioCondition: radioCondition
    });
  });

  return /*#__PURE__*/_react["default"].createElement(RadioGroupLayer, props, " ", renderChildren, " ");
}

RadioGroup.propTypes = _RadioGroup.radioGroupProps;
RadioGroup.defaultProps = _RadioGroup.radioGroupDefaultProps;
RadioGroup.propsDescription = _RadioGroup.radioGroupPropsDescription;
RadioGroup.constantProps = _RadioGroup2.RadioGroupConstant;

function RadioGroupContext(props) {
  return /*#__PURE__*/_react["default"].createElement(RadioGroupLayer, props, " ", props.children, " ");
}

RadioGroupContext.propTypes = _RadioGroup.radioContextProps;
RadioGroupContext.defaultProps = _RadioGroup.radioContextDefaultProps;
RadioGroupContext.propsDescription = _RadioGroup.radioContextPropsDescription;
RadioGroupContext.constantProps = _RadioGroup2.RadioGroupConstant;

function RadioGroupLayer(props) {
  var _getCompliedClassName, _getCompliedClassName2;

  var _props$customClassNam = props.customClassName,
      customClassName = _props$customClassNam === void 0 ? {} : _props$customClassNam,
      children = props.children,
      direction = props.direction,
      tagAttributeProps = props.tagAttributeProps,
      _props$customId = props.customId,
      customId = _props$customId === void 0 ? {} : _props$customId;
  var radioGroupParent = (0, _getCompliedClassName11["default"])(customClassName, (_getCompliedClassName = {}, _defineProperty(_getCompliedClassName, _RadioGroupModule["default"].radioGroup, true), _defineProperty(_getCompliedClassName, _RadioGroupModule["default"][direction], !!direction), _getCompliedClassName), (_getCompliedClassName2 = {}, _defineProperty(_getCompliedClassName2, customClassName.radioGroup, !!customClassName.radioGroup), _defineProperty(_getCompliedClassName2, customClassName.direction, !!customClassName.direction), _getCompliedClassName2));
  return /*#__PURE__*/_react["default"].createElement("div", _extends({
    role: "radiogroup",
    className: radioGroupParent
  }, tagAttributeProps, {
    "data-id": (0, _getCompliedCustomId["default"])(props, customId.radio_group)
  }), " ", children, " ");
}

function Radio(props) {
  var _getCompliedClassName5, _getCompliedClassName6, _getCompliedClassName7, _getCompliedClassName8, _getCompliedClassName9, _getCompliedClassName10;

  var text = props.text,
      value = props.value,
      variant = props.variant,
      size = props.size,
      color = props.color,
      disabled = props.disabled,
      children = props.children,
      tagAttributeProps = props.tagAttributeProps,
      customClassName = props.customClassName,
      customId = props.customId,
      customIdPrefix = props.customIdPrefix,
      _props$customIdSuffix = props.customIdSuffix,
      customIdSuffix = _props$customIdSuffix === void 0 ? '' : _props$customIdSuffix,
      selectedValue = props.selectedValue,
      onChange = props.onChange,
      name = props.name,
      radioCondition = props.radioCondition,
      focusVisible = props.focusVisible,
      focusProps = props.focusProps;
  var isChecked = radioCondition(selectedValue, value);
  var baseClassName = (0, _getCompliedClassName11["default"])(customClassName, _defineProperty({}, _RadioGroupModule["default"].base, true), _defineProperty({}, customClassName.base, customClassName.base));
  var inputClassName = (0, _getCompliedClassName11["default"])(customClassName, (_getCompliedClassName5 = {}, _defineProperty(_getCompliedClassName5, _RadioGroupModule["default"].inputradio, true), _defineProperty(_getCompliedClassName5, _RadioGroupModule["default"].disabledInputRadio, !!disabled && !customClassName.disabledInputRadio), _getCompliedClassName5), (_getCompliedClassName6 = {}, _defineProperty(_getCompliedClassName6, customClassName.inputradio, !!customClassName.inputradio), _defineProperty(_getCompliedClassName6, customClassName.disabledInputRadio, !!customClassName.disabledInputRadio), _getCompliedClassName6));
  var radioBtnClassName = (0, _getCompliedClassName11["default"])(customClassName, (_getCompliedClassName7 = {}, _defineProperty(_getCompliedClassName7, _RadioGroupModule["default"].button, true), _defineProperty(_getCompliedClassName7, _RadioGroupModule["default"][variant], isChecked && !!variant && !customClassName.checked), _defineProperty(_getCompliedClassName7, _RadioGroupModule["default"][size], !!size && !customClassName.size), _defineProperty(_getCompliedClassName7, _RadioGroupModule["default"].disabledRadio, !!disabled && !customClassName.disabledRadio), _defineProperty(_getCompliedClassName7, _RadioGroupModule["default"][color], !!color && !customClassName.color), _defineProperty(_getCompliedClassName7, _RadioGroupModule["default"].focusVisible, focusVisible && !customClassName.focusVisible), _getCompliedClassName7), (_getCompliedClassName8 = {}, _defineProperty(_getCompliedClassName8, customClassName.button, !!customClassName.button), _defineProperty(_getCompliedClassName8, customClassName.checked, isChecked && !!customClassName.checked), _defineProperty(_getCompliedClassName8, customClassName.size, !!customClassName.size), _defineProperty(_getCompliedClassName8, customClassName.disabledRadio, !!customClassName.disabledRadio), _defineProperty(_getCompliedClassName8, customClassName.color, !!customClassName.color), _defineProperty(_getCompliedClassName8, customClassName.focusVisible, focusVisible && !!customClassName.focusVisible), _getCompliedClassName8));
  var labelClassName = (0, _getCompliedClassName11["default"])(customClassName, (_getCompliedClassName9 = {}, _defineProperty(_getCompliedClassName9, _RadioGroupModule["default"].label, true), _defineProperty(_getCompliedClassName9, _RadioGroupModule["default"].disabledLabel, !!disabled && !customClassName.disabledLabel), _getCompliedClassName9), (_getCompliedClassName10 = {}, _defineProperty(_getCompliedClassName10, customClassName.label, !!customClassName.labe), _defineProperty(_getCompliedClassName10, customClassName.disabledLabel, !!customClassName.disabledLabel), _getCompliedClassName10));
  return /*#__PURE__*/_react["default"].createElement("div", _extends({
    className: baseClassName
  }, tagAttributeProps), /*#__PURE__*/_react["default"].createElement("span", {
    className: radioBtnClassName
  }, /*#__PURE__*/_react["default"].createElement("input", _extends({
    type: "radio",
    value: value,
    id: value,
    "data-aria-focusstyle": _RadioGroupModule["default"].focusVisibile,
    name: name,
    className: inputClassName,
    checked: isChecked,
    disabled: disabled,
    "data-id": (0, _getCompliedCustomId["default"])({
      customId: customId,
      customIdPrefix: customIdPrefix,
      customIdSuffix: customIdSuffix
    }, customId.radio_group_item, [value]),
    onChange: !disabled ? onChange && onChange.bind(null, value) : null
  }, focusProps))), text && /*#__PURE__*/_react["default"].createElement("label", {
    className: labelClassName,
    htmlFor: value
  }, text));
}

Radio.propTypes = _RadioGroup["default"];
Radio.defaultProps = _RadioGroup.radioDefaultProps;
Radio.propsDescription = _RadioGroup.radioPropsDescription;
Radio.constantProps = _RadioGroup2["default"];