"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isChecked = exports.handlePress = exports.default = exports.RadioButton = void 0;
var React = _interopRequireWildcard(require("react"));
var _theming = require("../../core/theming");
var _reactNative = require("react-native");
var _color = _interopRequireDefault(require("color"));
var _RadioButtonGroup = require("./RadioButtonGroup");
var _TouchableRipple = _interopRequireDefault(require("../TouchableRipple/TouchableRipple.native"));
var _tokens = require("../../styles/tokens");
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 (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 ? 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); }
const handlePress = _ref => {
  let {
    onPress,
    value,
    onValueChange
  } = _ref;
  if (onPress && onValueChange) {
    console.warn(`onPress in the scope of RadioButtonGroup will not be executed, use onValueChange instead`);
  }
  onValueChange ? onValueChange(value) : onPress === null || onPress === void 0 ? void 0 : onPress();
};
exports.handlePress = handlePress;
const isChecked = _ref2 => {
  let {
    value,
    status,
    contextValue
  } = _ref2;
  if (contextValue !== undefined && contextValue !== null) {
    return contextValue === value ? 'checked' : 'unchecked';
  } else {
    return status;
  }
};
exports.isChecked = isChecked;
const BORDER_WIDTH = 2;
const RadioButton = _ref3 => {
  let {
    disabled,
    onPress,
    theme,
    value,
    status,
    testID,
    ...rest
  } = _ref3;
  const {
    current: borderAnim
  } = React.useRef(new _reactNative.Animated.Value(BORDER_WIDTH));
  const {
    current: radioAnim
  } = React.useRef(new _reactNative.Animated.Value(1));
  const isFirstRendering = React.useRef(true);
  const {
    scale
  } = theme.animation;
  React.useEffect(() => {
    // Do not run animation on very first rendering
    if (isFirstRendering.current) {
      isFirstRendering.current = false;
      return;
    }
    if (status === 'checked') {
      radioAnim.setValue(1.2);
      _reactNative.Animated.timing(radioAnim, {
        toValue: 1,
        duration: 150 * scale,
        useNativeDriver: true
      }).start();
    } else {
      borderAnim.setValue(10);
      _reactNative.Animated.timing(borderAnim, {
        toValue: BORDER_WIDTH,
        duration: 150 * scale,
        useNativeDriver: false
      }).start();
    }
  }, [status, borderAnim, radioAnim, scale]);
  const checkedColor = rest.color || theme.colors.primary.default;
  const uncheckedColor = rest.uncheckedColor || '#CCCDD3';
  let rippleColor, radioColor;
  return /*#__PURE__*/React.createElement(_RadioButtonGroup.RadioButtonContext.Consumer, null, context => {
    const checked = isChecked({
      contextValue: context === null || context === void 0 ? void 0 : context.value,
      status,
      value
    }) === 'checked';
    if (disabled) {
      rippleColor = (0, _color.default)(theme.colors.text).alpha(0.16).rgb().string();
      radioColor = theme.colors.disabled;
    } else {
      rippleColor = (0, _color.default)(checkedColor).fade(0.32).rgb().string();
      radioColor = checked ? checkedColor : uncheckedColor;
    }
    return /*#__PURE__*/React.createElement(_TouchableRipple.default, _extends({}, rest, {
      borderless: true,
      rippleColor: rippleColor,
      onPress: disabled ? undefined : () => {
        handlePress({
          onPress,
          onValueChange: context === null || context === void 0 ? void 0 : context.onValueChange,
          value
        });
      }
      // @ts-expect-error We keep old a11y props for backwards compat with old RN versions
      ,
      accessibilityTraits: disabled ? ['button', 'disabled'] : 'button',
      accessibilityComponentType: checked ? 'radiobutton_checked' : 'radiobutton_unchecked',
      accessibilityRole: "radio",
      accessibilityState: {
        disabled,
        checked
      },
      accessibilityLiveRegion: "polite",
      style: styles.container,
      testID: testID
    }), /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
      style: [styles.radio, {
        borderColor: radioColor,
        borderWidth: borderAnim,
        backgroundColor: radioColor
      }]
    }, checked ? /*#__PURE__*/React.createElement(_reactNative.View, {
      style: [_reactNative.StyleSheet.absoluteFill, styles.radioContainer]
    }, /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
      style: [styles.dot, {
        backgroundColor: _tokens.colors.white,
        transform: [{
          scale: radioAnim
        }]
      }]
    })) : null));
  });
};
RadioButton.displayName = 'RadioButton';
const styles = _reactNative.StyleSheet.create({
  container: {
    borderRadius: 18
  },
  radioContainer: {
    alignItems: 'center',
    justifyContent: 'center'
  },
  radio: {
    height: 20,
    width: 20,
    borderRadius: 10,
    margin: 8
  },
  dot: {
    height: 10,
    width: 10,
    borderRadius: 5
  }
});
var _default = (0, _theming.withTheme)(RadioButton); // @component-docs ignore-next-line
exports.default = _default;
const RadioButtonWithTheme = (0, _theming.withTheme)(RadioButton);
// @component-docs ignore-next-line
exports.RadioButton = RadioButtonWithTheme;
//# sourceMappingURL=Radio.js.map