"use strict";

import { useState } from "react";
import { Pressable, View } from "react-native";
import { useIOTheme } from "../../context";
import { IOSelectionTickVisualParams } from "../../core";
import { triggerHaptic } from "../../functions/haptic-feedback/hapticFeedback";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { HStack } from "../layout";
import { H6 } from "../typography/H6";
import { AnimatedCheckbox } from "./AnimatedCheckbox";
import { jsx as _jsx, jsxs as _jsxs } from "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
 */
export const CheckboxLabel = ({
  label,
  checked,
  disabled,
  onValueChange
}) => {
  const theme = useIOTheme();
  const {
    dynamicFontScale
  } = useIOFontDynamicScale();
  const [toggleValue, setToggleValue] = useState(checked ?? false);
  const toggleCheckbox = () => {
    triggerHaptic("impactLight");
    setToggleValue(!toggleValue);
    if (onValueChange !== undefined) {
      onValueChange(!toggleValue);
    }
  };
  return /*#__PURE__*/_jsx(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__*/_jsxs(HStack, {
      allowScaleSpacing: true,
      style: {
        alignItems: "center",
        width: "100%"
      },
      space: CHECKBOX_MARGIN,
      children: [/*#__PURE__*/_jsx(View, {
        pointerEvents: "none",
        accessibilityElementsHidden: true,
        importantForAccessibility: "no-hide-descendants",
        style: {
          alignSelf: "flex-start"
        },
        children: /*#__PURE__*/_jsx(AnimatedCheckbox, {
          size: IOSelectionTickVisualParams.size * dynamicFontScale,
          checked: checked ?? toggleValue
        })
      }), /*#__PURE__*/_jsx(H6, {
        style: {
          flexShrink: 1
        },
        color: theme["textBody-default"],
        children: label
      })]
    })
  });
};
//# sourceMappingURL=CheckboxLabel.js.map