"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

var _Box = _interopRequireDefault(require("../Box"));

var _Center = require("../../composites/Center");

var _useThemeProps = require("../../../hooks/useThemeProps");

var _utils = require("./../../../utils");

var _interactions = require("@react-native-aria/interactions");

var _radio = require("@react-native-aria/radio");

var _visuallyHidden = require("@react-aria/visually-hidden");

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

var _focus = require("@react-native-aria/focus");

var _Icons = require("../Icon/Icons");

var _useHasResponsiveProps = require("../../../hooks/useHasResponsiveProps");

var _utils2 = require("../../../utils");

var _utils3 = require("../../../theme/tools/utils");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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); }

const RadioComponent = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(({
  icon,
  inputProps,
  combinedProps,
  children,
  wrapperRef,
  ...props
}, ref) => {
  const {
    isInvalid,
    isReadOnly,
    isIndeterminate
  } = combinedProps;
  const {
    disabled: isDisabled,
    checked: isChecked
  } = inputProps;

  const _ref = _react.default.useRef(null);

  const {
    isHovered
  } = (0, _interactions.useHover)({}, _ref);
  const mergedRefs = (0, _utils.mergeRefs)([_ref, wrapperRef]);
  const {
    focusProps,
    isFocusVisible
  } = (0, _focus.useFocusRing)(); // const mergedWrapperRef = React.useMemo(() => mergeRefs([wrapperRef, _ref]), []);

  const {
    _interactionBox,
    _icon,
    ...resolvedProps
  } = (0, _useThemeProps.usePropsResolution)('Radio', combinedProps, {
    isInvalid,
    isReadOnly,
    isFocusVisible,
    isDisabled,
    isIndeterminate,
    isChecked,
    isHovered
  });
  const [layoutProps, nonLayoutProps] = (0, _utils3.extractInObject)(resolvedProps, [..._utils3.stylingProps.margin, ..._utils3.stylingProps.layout, ..._utils3.stylingProps.flexbox, ..._utils3.stylingProps.position, '_text']); // only calling below function when icon exist.

  const sizedIcon = () =>
  /*#__PURE__*/
  //@ts-ignore
  _react.default.cloneElement(icon, { ..._icon
  });

  const component = /*#__PURE__*/_react.default.createElement(_Box.default, _extends({
    flexDirection: "row",
    alignItems: "center"
  }, layoutProps, {
    opacity: isDisabled ? 0.4 : 1,
    cursor: isDisabled ? 'not-allowed' : 'pointer'
  }), /*#__PURE__*/_react.default.createElement(_Center.Center, null, /*#__PURE__*/_react.default.createElement(_Box.default, _extends({}, _interactionBox, {
    style: {
      // @ts-ignore - only for web"
      transition: 'height 200ms, width 200ms'
    },
    h: isFocusVisible || isHovered ? '200%' : '100%',
    w: isFocusVisible || isHovered ? '200%' : '100%',
    pointerEvents: "none"
  })), /*#__PURE__*/_react.default.createElement(_Center.Center, nonLayoutProps, icon && sizedIcon && isChecked ? sizedIcon() : /*#__PURE__*/_react.default.createElement(_Icons.CircleIcon, _extends({}, _icon, {
    opacity: isChecked ? 1 : 0
  })))), children); //TODO: refactor for responsive prop


  if ((0, _useHasResponsiveProps.useHasResponsiveProps)(props)) {
    return null;
  } // console.log(inputProps, focusProps, ref);
  // return null;


  return /*#__PURE__*/_react.default.createElement(_Box.default // @ts-ignore - RN web supports accessibilityRole="label"
  , {
    accessibilityRole: "label",
    ref: mergedRefs
  }, /*#__PURE__*/_react.default.createElement(_visuallyHidden.VisuallyHidden, null, /*#__PURE__*/_react.default.createElement("input", _extends({}, inputProps, focusProps, {
    ref: ref
  }))), component);
}));

const Radio = ({
  icon,
  children,
  wrapperRef,
  ...props
}, ref) => {
  var _contextState$state;

  const contextState = _react.default.useContext(_RadioGroup.RadioContext);

  const combinedProps = (0, _utils2.combineContextAndProps)(contextState, props);

  const inputRef = _react.default.useRef(null);

  const radioState = (0, _radio.useRadio)({ ...combinedProps,
    'aria-label': props.accessibilityLabel,
    children
  }, (_contextState$state = contextState.state) !== null && _contextState$state !== void 0 ? _contextState$state : {}, inputRef); // console.log('radio', radioState);
  //@ts-ignore
  // eslint-disable-next-line react-hooks/exhaustive-deps

  const inputProps = _react.default.useMemo(() => radioState.inputProps, [radioState.inputProps.checked, radioState.inputProps.disabled]);

  const [contextCombinedProps] = _react.default.useState({ ...combinedProps
  }); //TODO: refactor for responsive prop


  if ((0, _useHasResponsiveProps.useHasResponsiveProps)(props)) {
    return null;
  }

  if ((0, _utils2.isEmptyObj)(contextState)) {
    console.error('Error: Radio must be wrapped inside a Radio.Group');
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
  }

  return /*#__PURE__*/_react.default.createElement(RadioComponent, {
    inputProps: inputProps,
    combinedProps: contextCombinedProps,
    children: children,
    ref: ref,
    icon: icon,
    wrapperRef: wrapperRef
  });
};

var _default = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(Radio));

exports.default = _default;
//# sourceMappingURL=Radio.web.js.map