"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _text = _interopRequireDefault(require("../../atoms/text/text"));
var _pressable = _interopRequireDefault(require("../../atoms/pressable/pressable"));
var _useMolecularComponentConfig = require("../../../hooks/useMolecularComponentConfig");
var _stack = _interopRequireDefault(require("../../atoms/stack/stack"));
var _styleFunctions = require("../../../theme/src/style-functions");
var _radioGroup = require("./radio-group");
var _useCheckedState = require("../../../hooks/state/useCheckedState");
var _useInvalidState = require("../../../hooks/state/useInvalidState");
var _center = _interopRequireDefault(require("../../atoms/center/center"));
var _box = _interopRequireDefault(require("../../atoms/box/box"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
/** The Radio component is used when only one choice may be selected in a series of options. **/
const Radio = /*#__PURE__*/_react.default.memo( /*#__PURE__*/_react.default.forwardRef(({
  children,
  onPress = () => {},
  ...rest
}, radioRef) => {
  const {
    size,
    variant,
    isDisabled,
    colorScheme,
    radioGroupValue,
    setRadioGroupValue
  } = (0, _radioGroup.useRadioGroup)();

  // Overwrite props from radio group
  rest.size = rest.size ?? size;
  rest.variant = rest.variant ?? variant;
  rest.isDisabled = rest.isDisabled ?? isDisabled;
  rest.colorScheme = rest.colorScheme ?? colorScheme;
  const isRadioInGroup = setRadioGroupValue !== undefined;
  const isRadioChecked = isRadioInGroup ? radioGroupValue === rest.value && rest.value !== undefined : rest.isChecked;
  const molecularProps = (0, _useMolecularComponentConfig.useMolecularComponentConfig)("Radio", rest, {
    size: rest.size,
    variant: rest.variant
  }, rest.colorScheme, _styleFunctions.boxStyleFunctions, "container", "container", "container");
  const {
    atoms
  } = molecularProps;

  // Use state for dynamic style
  const {
    propsWithCheckedStyles: propsWithCheckedStylesForOuterBox
  } = (0, _useCheckedState.useCheckedState)(atoms.outerBox, _styleFunctions.boxStyleFunctions, "molecule", true, isRadioChecked);
  atoms.outerBox = propsWithCheckedStylesForOuterBox;
  const {
    propsWithInvalidStyles
  } = (0, _useInvalidState.useInvalidState)(atoms.outerBox, _styleFunctions.boxStyleFunctions, "molecule", true, rest.isInvalid);
  atoms.outerBox = propsWithInvalidStyles;
  const {
    propsWithCheckedStyles: propsWithCheckedStylesForInnerBox
  } = (0, _useCheckedState.useCheckedState)(atoms.innerBox, _styleFunctions.boxStyleFunctions, "molecule", true, isRadioChecked);
  atoms.innerBox = propsWithCheckedStylesForInnerBox;

  // OTHER METHODS
  const radioPressHandler = () => {
    if (isRadioInGroup) {
      setRadioGroupValue(rest.value);
      if (onPress) onPress();
    }
    if (onPress) onPress();
  };

  // RENDER METHODS
  return /*#__PURE__*/_react.default.createElement(_pressable.default, _extends({}, atoms.container, {
    ref: radioRef,
    onPress: radioPressHandler,
    accessible: true,
    accessibilityRole: "radio",
    isDisabled: rest.isDisabled,
    accessibilityLabel: rest.accessibilityLabel ? rest.accessibilityLabel : children,
    accessibilityState: {
      disabled: rest.isDisabled,
      checked: isRadioChecked
    },
    accessibilityHint: rest.accessibilityHint
  }), /*#__PURE__*/_react.default.createElement(_stack.default, {
    spacing: rest.spacing || atoms.container.spacing,
    direction: "horizontal"
  }, /*#__PURE__*/_react.default.createElement(_center.default, atoms.outerBox, /*#__PURE__*/_react.default.createElement(_box.default, atoms.innerBox)), !!children && /*#__PURE__*/_react.default.createElement(_text.default, atoms.text, children)));
}));
Radio.displayName = "Radio";
var _default = exports.default = Radio;
//# sourceMappingURL=radio.js.map