"use strict";

import { useState } from "react";
import { Image, Pressable, StyleSheet, View } from "react-native";
import ReactNativeHapticFeedback from "react-native-haptic-feedback";
import Animated from "react-native-reanimated";
import { useIOTheme } from "../../context";
import { IOSelectionListItemStyles, IOSelectionListItemVisualParams, IOSelectionTickVisualParams } from "../../core";
import { useListItemAnimation } from "../../hooks";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { Icon } from "../icons";
import { HSpacer, VSpacer, VStack } from "../layout";
import { LogoPayment } from "../logos";
import { AnimatedRadio } from "../radio/AnimatedRadio";
import { IOSkeleton } from "../skeleton";
import { BodySmall, H6 } from "../typography";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DISABLED_OPACITY = 0.5;
const styles = StyleSheet.create({
  imageSize: {
    width: IOSelectionListItemVisualParams.iconSize,
    height: 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
 */
export const ListItemRadio = ({
  value,
  description,
  startImage,
  selected,
  disabled,
  onValueChange,
  accessibilityLabel,
  accessibilityHint,
  loadingProps,
  testID
}) => {
  const [toggleValue, setToggleValue] = useState(selected ?? false);
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = useIOFontDynamicScale();
  const {
    onPressIn,
    onPressOut,
    scaleAnimatedStyle,
    backgroundAnimatedStyle
  } = useListItemAnimation();
  const theme = useIOTheme();
  const toggleRadioItem = () => {
    ReactNativeHapticFeedback.trigger("impactLight");
    setToggleValue(!toggleValue);
    if (onValueChange !== undefined) {
      onValueChange(!toggleValue);
    }
  };
  const disabledStyle = {
    opacity: disabled ? DISABLED_OPACITY : 1
  };
  const SkeletonDescriptionLines = () => /*#__PURE__*/_jsxs(VStack, {
    space: 8,
    children: [/*#__PURE__*/_jsx(IOSkeleton, {
      shape: "rectangle",
      width: "100%",
      height: 8,
      radius: 8
    }), /*#__PURE__*/_jsx(IOSkeleton, {
      shape: "rectangle",
      width: "100%",
      height: 8,
      radius: 8
    })]
  });
  const SkeletonIcon = () => /*#__PURE__*/_jsx(View, {
    style: {
      marginRight: IOSelectionListItemVisualParams.iconMargin
    },
    children: /*#__PURE__*/_jsx(IOSkeleton, {
      shape: "square",
      size: IOSelectionListItemVisualParams.iconSize,
      radius: 8
    })
  });
  const ListItemRadioSkeleton = ({
    loadingAccessibilityLabel
  }) => /*#__PURE__*/_jsxs(View, {
    style: [IOSelectionListItemStyles.listItem, {
      rowGap: 8
    }],
    accessible: true,
    accessibilityLabel: loadingAccessibilityLabel,
    accessibilityState: {
      busy: true
    },
    children: [/*#__PURE__*/_jsx(View, {
      style: IOSelectionListItemStyles.listItemInner,
      children: /*#__PURE__*/_jsxs(View, {
        style: {
          flex: 1,
          flexDirection: "row",
          justifyContent: "space-between",
          alignItems: "center"
        },
        children: [/*#__PURE__*/_jsxs(View, {
          style: {
            flexDirection: "row",
            alignItems: "center"
          },
          children: [loadingProps?.skeletonIcon && /*#__PURE__*/_jsx(SkeletonIcon, {}), /*#__PURE__*/_jsx(IOSkeleton, {
            shape: "rectangle",
            width: 180,
            height: 16,
            radius: 8
          })]
        }), /*#__PURE__*/_jsx(HSpacer, {
          size: 8
        }), /*#__PURE__*/_jsx(View, {
          pointerEvents: "none",
          style: disabledStyle,
          children: /*#__PURE__*/_jsx(AnimatedRadio, {
            size: IOSelectionTickVisualParams.size * dynamicFontScale,
            checked: toggleValue
          })
        })]
      })
    }), loadingProps?.skeletonDescription && /*#__PURE__*/_jsx(SkeletonDescriptionLines, {})]
  });
  return loadingProps?.state ? /*#__PURE__*/_jsx(ListItemRadioSkeleton, {
    loadingAccessibilityLabel: loadingProps?.loadingAccessibilityLabel
  }) : /*#__PURE__*/_jsx(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__*/_jsx(Animated.View, {
      style: [IOSelectionListItemStyles.listItem, backgroundAnimatedStyle, disabledStyle]
      // This is required to avoid opacity
      // inheritance on Android
      ,
      needsOffscreenAlphaCompositing: true,
      children: /*#__PURE__*/_jsxs(Animated.View, {
        style: scaleAnimatedStyle,
        children: [/*#__PURE__*/_jsxs(View, {
          style: IOSelectionListItemStyles.listItemInner,
          children: [/*#__PURE__*/_jsxs(View, {
            style: {
              flexDirection: "row",
              flexShrink: 1,
              columnGap: IOSelectionListItemVisualParams.iconMargin * dynamicFontScale * spacingScaleMultiplier
            },
            children: [startImage?.icon && !hugeFontEnabled && /*#__PURE__*/_jsx(Icon, {
              allowFontScaling: true,
              name: startImage.icon,
              color: theme["icon-decorative"],
              size: IOSelectionListItemVisualParams.iconSize
            }), startImage?.uri && /*#__PURE__*/_jsx(View, {
              style: {
                alignSelf: "center"
              },
              children: /*#__PURE__*/_jsx(Image, {
                accessibilityIgnoresInvertColors: true,
                source: startImage,
                style: styles.imageSize
              })
            }), startImage?.paymentLogo && /*#__PURE__*/_jsx(View, {
              style: {
                alignSelf: "center"
              },
              children: /*#__PURE__*/_jsx(LogoPayment, {
                name: startImage.paymentLogo,
                size: IOSelectionListItemVisualParams.iconSize
              })
            }), /*#__PURE__*/_jsx(H6, {
              color: theme["textBody-default"],
              style: {
                flexShrink: 1
              },
              children: value
            })]
          }), /*#__PURE__*/_jsx(HSpacer, {
            size: 8
          }), /*#__PURE__*/_jsx(View, {
            pointerEvents: "none",
            accessibilityElementsHidden: true,
            importantForAccessibility: "no-hide-descendants",
            children: /*#__PURE__*/_jsx(AnimatedRadio, {
              size: IOSelectionTickVisualParams.size * dynamicFontScale,
              checked: selected ?? toggleValue
            })
          })]
        }), description && /*#__PURE__*/_jsxs(View, {
          children: [/*#__PURE__*/_jsx(VSpacer, {
            size: IOSelectionListItemVisualParams.descriptionMargin
          }), /*#__PURE__*/_jsx(BodySmall, {
            weight: "Regular",
            color: theme["textBody-tertiary"],
            children: description
          })]
        })]
      })
    })
  });
};
//# sourceMappingURL=ListItemRadio.js.map