"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ListItemCheckbox = 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 _AnimatedCheckbox = require("../checkbox/AnimatedCheckbox");
var _icons = require("../icons");
var _layout = require("../layout");
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;
/**
 *  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 ListItemCheckbox = ({
  value,
  description,
  icon,
  selected,
  accessibilityLabel,
  accessibilityHint,
  disabled,
  onValueChange
}) => {
  const {
    dynamicFontScale,
    spacingScaleMultiplier,
    hugeFontEnabled
  } = (0, _accessibility.useIOFontDynamicScale)();
  const [toggleValue, setToggleValue] = (0, _react.useState)(selected ?? false);
  const {
    onPressIn,
    onPressOut,
    scaleAnimatedStyle,
    backgroundAnimatedStyle
  } = (0, _hooks.useListItemAnimation)();

  // Theme
  const theme = (0, _context.useIOTheme)();

  // Accessibility
  // Comma = Small pause when announcing content
  const fallbackAccessibilityLabel = description ? `${value}, ${description}` : value;
  const toggleCheckbox = () => {
    _reactNativeHapticFeedback.default.trigger("impactLight");
    setToggleValue(!toggleValue);
    if (onValueChange !== undefined) {
      onValueChange(!toggleValue);
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.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__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
      style: [_core.IOSelectionListItemStyles.listItem, backgroundAnimatedStyle, {
        opacity: disabled ? DISABLED_OPACITY : 1
      }]
      // 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: [icon && !hugeFontEnabled && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
              allowFontScaling: true,
              name: icon,
              color: theme["icon-decorative"],
              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)(_AnimatedCheckbox.AnimatedCheckbox, {
              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.ListItemCheckbox = ListItemCheckbox;
//# sourceMappingURL=ListItemCheckbox.js.map