"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ListItemRadio = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeHapticFeedback = _interopRequireDefault(require("react-native-haptic-feedback"));
var _reactNativeReanimated = _interopRequireDefault(require("react-native-reanimated"));
var _context = require("../../context");
var _core = require("../../core");
var _hooks = require("../../hooks");
var _accessibility = require("../../utils/accessibility");
var _icons = require("../icons");
var _layout = require("../layout");
var _logos = require("../logos");
var _AnimatedRadio = require("../radio/AnimatedRadio");
var _skeleton = require("../skeleton");
var _typography = require("../typography");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const DISABLED_OPACITY = 0.5;
const styles = _reactNative.StyleSheet.create({
  imageSize: {
    width: _core.IOSelectionListItemVisualParams.iconSize,
    height: _core.IOSelectionListItemVisualParams.iconSize,
    resizeMode: "contain"
  }
});

/**
 * `ListItemRadio` component with the automatic state management that uses a {@link AnimatedCheckBox}
 * The toggleValue change when a `onPress` event is received and dispatch the `onValueChange`.
 *
 * @param props
 * @constructor
 */
const ListItemRadio = ({
  value,
  description,
  startImage,
  selected,
  disabled,
  onValueChange,
  accessibilityLabel,
  accessibilityHint,
  loadingProps,
  testID
}) => {
  const [toggleValue, setToggleValue] = (0, _react.useState)(selected ?? false);
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = (0, _accessibility.useIOFontDynamicScale)();
  const {
    onPressIn,
    onPressOut,
    scaleAnimatedStyle,
    backgroundAnimatedStyle
  } = (0, _hooks.useListItemAnimation)();
  const theme = (0, _context.useIOTheme)();
  const toggleRadioItem = () => {
    _reactNativeHapticFeedback.default.trigger("impactLight");
    setToggleValue(!toggleValue);
    if (onValueChange !== undefined) {
      onValueChange(!toggleValue);
    }
  };
  const disabledStyle = {
    opacity: disabled ? DISABLED_OPACITY : 1
  };
  const SkeletonDescriptionLines = () => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_layout.VStack, {
    space: 8,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.IOSkeleton, {
      shape: "rectangle",
      width: "100%",
      height: 8,
      radius: 8
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.IOSkeleton, {
      shape: "rectangle",
      width: "100%",
      height: 8,
      radius: 8
    })]
  });
  const SkeletonIcon = () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: {
      marginRight: _core.IOSelectionListItemVisualParams.iconMargin
    },
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.IOSkeleton, {
      shape: "square",
      size: _core.IOSelectionListItemVisualParams.iconSize,
      radius: 8
    })
  });
  const ListItemRadioSkeleton = ({
    loadingAccessibilityLabel
  }) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: [_core.IOSelectionListItemStyles.listItem, {
      rowGap: 8
    }],
    accessible: true,
    accessibilityLabel: loadingAccessibilityLabel,
    accessibilityState: {
      busy: true
    },
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: _core.IOSelectionListItemStyles.listItemInner,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: {
          flex: 1,
          flexDirection: "row",
          justifyContent: "space-between",
          alignItems: "center"
        },
        children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
          style: {
            flexDirection: "row",
            alignItems: "center"
          },
          children: [loadingProps?.skeletonIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(SkeletonIcon, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.IOSkeleton, {
            shape: "rectangle",
            width: 180,
            height: 16,
            radius: 8
          })]
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.HSpacer, {
          size: 8
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          pointerEvents: "none",
          style: disabledStyle,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_AnimatedRadio.AnimatedRadio, {
            size: _core.IOSelectionTickVisualParams.size * dynamicFontScale,
            checked: toggleValue
          })
        })]
      })
    }), loadingProps?.skeletonDescription && /*#__PURE__*/(0, _jsxRuntime.jsx)(SkeletonDescriptionLines, {})]
  });
  return loadingProps?.state ? /*#__PURE__*/(0, _jsxRuntime.jsx)(ListItemRadioSkeleton, {
    loadingAccessibilityLabel: loadingProps?.loadingAccessibilityLabel
  }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
    accessibilityRole: "radio",
    accessibilityState: {
      checked: selected ?? toggleValue,
      disabled: !!disabled
    },
    accessibilityLabel: accessibilityLabel,
    accessibilityHint: accessibilityHint,
    onPress: toggleRadioItem,
    onPressIn: onPressIn,
    onPressOut: onPressOut,
    onTouchEnd: onPressOut,
    testID: testID,
    disabled: disabled,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
      style: [_core.IOSelectionListItemStyles.listItem, backgroundAnimatedStyle, disabledStyle]
      // This is required to avoid opacity
      // inheritance on Android
      ,
      needsOffscreenAlphaCompositing: true,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNativeReanimated.default.View, {
        style: scaleAnimatedStyle,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
          style: _core.IOSelectionListItemStyles.listItemInner,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
            style: {
              flexDirection: "row",
              flexShrink: 1,
              columnGap: _core.IOSelectionListItemVisualParams.iconMargin * dynamicFontScale * spacingScaleMultiplier
            },
            children: [startImage?.icon && !hugeFontEnabled && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
              allowFontScaling: true,
              name: startImage.icon,
              color: theme["icon-decorative"],
              size: _core.IOSelectionListItemVisualParams.iconSize
            }), startImage?.uri && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
              style: {
                alignSelf: "center"
              },
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Image, {
                accessibilityIgnoresInvertColors: true,
                source: startImage,
                style: styles.imageSize
              })
            }), startImage?.paymentLogo && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
              style: {
                alignSelf: "center"
              },
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_logos.LogoPayment, {
                name: startImage.paymentLogo,
                size: _core.IOSelectionListItemVisualParams.iconSize
              })
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.H6, {
              color: theme["textBody-default"],
              style: {
                flexShrink: 1
              },
              children: value
            })]
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.HSpacer, {
            size: 8
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
            pointerEvents: "none",
            accessibilityElementsHidden: true,
            importantForAccessibility: "no-hide-descendants",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_AnimatedRadio.AnimatedRadio, {
              size: _core.IOSelectionTickVisualParams.size * dynamicFontScale,
              checked: selected ?? toggleValue
            })
          })]
        }), description && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
            size: _core.IOSelectionListItemVisualParams.descriptionMargin
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_typography.BodySmall, {
            weight: "Regular",
            color: theme["textBody-tertiary"],
            children: description
          })]
        })]
      })
    })
  });
};
exports.ListItemRadio = ListItemRadio;
//# sourceMappingURL=ListItemRadio.js.map