"use strict";

import { useState } from "react";
import { Pressable, 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 { AnimatedCheckbox } from "../checkbox/AnimatedCheckbox";
import { Icon } from "../icons";
import { HSpacer, VSpacer } from "../layout";
import { BodySmall, H6 } from "../typography";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DISABLED_OPACITY = 0.5;
/**
 *  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 ListItemCheckbox = ({
  value,
  description,
  icon,
  selected,
  accessibilityLabel,
  accessibilityHint,
  disabled,
  onValueChange
}) => {
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = useIOFontDynamicScale();
  const [toggleValue, setToggleValue] = useState(selected ?? false);
  const {
    onPressIn,
    onPressOut,
    scaleAnimatedStyle,
    backgroundAnimatedStyle
  } = useListItemAnimation();

  // Theme
  const theme = useIOTheme();

  // Accessibility
  // Comma = Small pause when announcing content
  const fallbackAccessibilityLabel = description ? `${value}, ${description}` : value;
  const toggleCheckbox = () => {
    ReactNativeHapticFeedback.trigger("impactLight");
    setToggleValue(!toggleValue);
    if (onValueChange !== undefined) {
      onValueChange(!toggleValue);
    }
  };
  return /*#__PURE__*/_jsx(Pressable, {
    onPress: toggleCheckbox,
    onPressIn: onPressIn,
    onPressOut: onPressOut,
    onTouchEnd: onPressOut,
    testID: "ListItemCheckbox",
    accessible: true,
    accessibilityLabel: accessibilityLabel || fallbackAccessibilityLabel,
    accessibilityHint: accessibilityHint,
    accessibilityRole: "checkbox",
    accessibilityState: {
      checked: selected ?? toggleValue,
      disabled: !!disabled
    },
    disabled: disabled,
    children: /*#__PURE__*/_jsx(Animated.View, {
      style: [IOSelectionListItemStyles.listItem, backgroundAnimatedStyle, {
        opacity: disabled ? DISABLED_OPACITY : 1
      }]
      // 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: [icon && !hugeFontEnabled && /*#__PURE__*/_jsx(Icon, {
              allowFontScaling: true,
              name: icon,
              color: theme["icon-decorative"],
              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(AnimatedCheckbox, {
              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=ListItemCheckbox.js.map