"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CheckboxLabel = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _context = require("../../context");
var _core = require("../../core");
var _hapticFeedback = require("../../functions/haptic-feedback/hapticFeedback");
var _accessibility = require("../../utils/accessibility");
var _layout = require("../layout");
var _H = require("../typography/H6");
var _AnimatedCheckbox = require("./AnimatedCheckbox");
var _jsxRuntime = require("react/jsx-runtime");
const DISABLED_OPACITY = 0.5;
const CHECKBOX_MARGIN = 8;

// disabled: the component is no longer touchable
// onPress:

/**
 * A checkbox 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 CheckboxLabel = ({
  label,
  checked,
  disabled,
  onValueChange
}) => {
  const theme = (0, _context.useIOTheme)();
  const {
    dynamicFontScale
  } = (0, _accessibility.useIOFontDynamicScale)();
  const [toggleValue, setToggleValue] = (0, _react.useState)(checked ?? false);
  const toggleCheckbox = () => {
    (0, _hapticFeedback.triggerHaptic)("impactLight");
    setToggleValue(!toggleValue);
    if (onValueChange !== undefined) {
      onValueChange(!toggleValue);
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
    disabled: disabled,
    onPress: toggleCheckbox,
    testID: "AnimatedCheckbox",
    style: {
      alignSelf: "flex-start",
      opacity: disabled ? DISABLED_OPACITY : 1
    },
    accessibilityRole: "checkbox",
    accessibilityState: {
      checked: checked ?? toggleValue,
      disabled: !!disabled
    }
    // This is required to avoid opacity
    // inheritance on Android
    ,
    needsOffscreenAlphaCompositing: true,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_layout.HStack, {
      allowScaleSpacing: true,
      style: {
        alignItems: "center",
        width: "100%"
      },
      space: CHECKBOX_MARGIN,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        pointerEvents: "none",
        accessibilityElementsHidden: true,
        importantForAccessibility: "no-hide-descendants",
        style: {
          alignSelf: "flex-start"
        },
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_AnimatedCheckbox.AnimatedCheckbox, {
          size: _core.IOSelectionTickVisualParams.size * dynamicFontScale,
          checked: checked ?? toggleValue
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_H.H6, {
        style: {
          flexShrink: 1
        },
        color: theme["textBody-default"],
        children: label
      })]
    })
  });
};
exports.CheckboxLabel = CheckboxLabel;
//# sourceMappingURL=CheckboxLabel.js.map