"use strict";

import React, { memo, useCallback, useEffect, useMemo, useRef } from 'react';
import { View, Text, Pressable, Animated } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { animation, borderRadius, space } from "../styles/tokens.js";
import { SUPPORTS_NATIVE_DRIVER } from "../styles/native-driver.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const SIZE_CONFIG = {
  small: {
    box: 18,
    checkmark: 10,
    fontSize: 14,
    lineHeight: 20,
    descFontSize: 12
  },
  medium: {
    box: 22,
    checkmark: 12,
    fontSize: 15,
    lineHeight: 22,
    descFontSize: 13
  },
  large: {
    box: 26,
    checkmark: 14,
    fontSize: 16,
    lineHeight: 24,
    descFontSize: 14
  }
};
const CheckboxComponent = ({
  checked,
  onCheckedChange,
  label,
  description,
  size = 'medium',
  disabled = false,
  indeterminate = false,
  color,
  style,
  labelStyle,
  accessibilityLabel,
  testID
}) => {
  const theme = useTheme();
  const scaleAnim = useRef(new Animated.Value(checked ? 1 : 0)).current;
  const pressAnim = useRef(new Animated.Value(1)).current;
  const sizeConfig = SIZE_CONFIG[size];
  const checkColor = color ?? theme.colors.primary;
  // The checkmark sits on top of `checkColor`. When the box uses the theme
  // primary, use the preset's readable foreground (white for blue, black for
  // yellow). A caller-supplied custom color falls back to white.
  const checkmarkColor = color == null ? theme.colors.primaryForeground : '#fff';
  useEffect(() => {
    Animated.spring(scaleAnim, {
      toValue: checked || indeterminate ? 1 : 0,
      useNativeDriver: SUPPORTS_NATIVE_DRIVER,
      ...animation.spring.snappy
    }).start();
  }, [checked, indeterminate, scaleAnim]);
  const handlePress = useCallback(() => {
    if (!disabled) {
      onCheckedChange(!checked);
    }
  }, [checked, disabled, onCheckedChange]);
  const onPressIn = useCallback(() => {
    Animated.spring(pressAnim, {
      toValue: 0.9,
      useNativeDriver: SUPPORTS_NATIVE_DRIVER,
      ...animation.spring.snappy
    }).start();
  }, [pressAnim]);
  const onPressOut = useCallback(() => {
    Animated.spring(pressAnim, {
      toValue: 1,
      useNativeDriver: SUPPORTS_NATIVE_DRIVER,
      ...animation.spring.gentle
    }).start();
  }, [pressAnim]);
  const boxStyle = useMemo(() => {
    const base = {
      width: sizeConfig.box,
      height: sizeConfig.box,
      borderRadius: borderRadius._2xs + 2,
      borderWidth: 2,
      alignItems: 'center',
      justifyContent: 'center'
    };
    if (checked || indeterminate) {
      base.backgroundColor = checkColor;
      base.borderColor = checkColor;
    } else {
      base.backgroundColor = 'transparent';
      base.borderColor = theme.colors.border;
    }
    return base;
  }, [sizeConfig, checked, indeterminate, checkColor, theme]);

  // Checkmark using unicode characters for zero-dependency rendering
  const checkmarkContent = indeterminate ? '\u2014' : '\u2713';
  return /*#__PURE__*/_jsxs(Pressable, {
    style: [{
      flexDirection: 'row',
      alignItems: 'flex-start',
      gap: space.sm,
      opacity: disabled ? 0.4 : 1
    }, style],
    onPress: handlePress,
    onPressIn: onPressIn,
    onPressOut: onPressOut,
    disabled: disabled,
    accessibilityRole: "checkbox",
    accessibilityState: {
      checked: indeterminate ? 'mixed' : checked,
      disabled
    },
    accessibilityLabel: accessibilityLabel ?? label,
    hitSlop: {
      top: 8,
      bottom: 8,
      left: 8,
      right: 8
    },
    testID: testID,
    children: [/*#__PURE__*/_jsx(Animated.View, {
      style: [boxStyle, {
        transform: [{
          scale: pressAnim
        }]
      }],
      children: /*#__PURE__*/_jsx(Animated.View, {
        style: {
          opacity: scaleAnim,
          transform: [{
            scale: scaleAnim
          }]
        },
        children: /*#__PURE__*/_jsx(Text, {
          style: {
            fontSize: sizeConfig.checkmark,
            color: checkmarkColor,
            fontWeight: '700',
            lineHeight: sizeConfig.checkmark + 2,
            textAlign: 'center'
          },
          children: checkmarkContent
        })
      })
    }), (label || description) && /*#__PURE__*/_jsxs(View, {
      style: {
        flex: 1,
        paddingTop: 1
      },
      children: [label && /*#__PURE__*/_jsx(Text, {
        style: [{
          fontSize: sizeConfig.fontSize,
          lineHeight: sizeConfig.lineHeight,
          color: theme.colors.text,
          fontWeight: '500'
        }, labelStyle],
        children: label
      }), description && /*#__PURE__*/_jsx(Text, {
        style: {
          fontSize: sizeConfig.descFontSize,
          color: theme.colors.textSecondary,
          lineHeight: sizeConfig.descFontSize + 6,
          marginTop: 2
        },
        children: description
      })]
    })]
  });
};
export const Checkbox = /*#__PURE__*/memo(CheckboxComponent);
Checkbox.displayName = 'Checkbox';
//# sourceMappingURL=Checkbox.js.map