"use strict";

import React, { memo, useCallback, useRef, useState } from 'react';
import { View, Text, TouchableOpacity, Animated, LayoutAnimation, Platform, UIManager, StyleSheet } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { animation } from "../styles/tokens.js";
import { SUPPORTS_NATIVE_DRIVER } from "../styles/native-driver.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
if (Platform.OS === 'android' && UIManager.setLayoutAnimationEnabledExperimental) {
  UIManager.setLayoutAnimationEnabledExperimental(true);
}
const CHEVRON_CLOSED = '0deg';
const CHEVRON_OPEN = '90deg';
const CollapsibleComponent = ({
  title,
  children,
  defaultOpen = false,
  chevronIcon,
  style,
  titleStyle,
  testID
}) => {
  const [isOpen, setIsOpen] = useState(defaultOpen);
  const theme = useTheme();
  const chevronAnim = useRef(new Animated.Value(defaultOpen ? 1 : 0)).current;
  const handleToggle = useCallback(() => {
    const nextOpen = !isOpen;

    // Animate chevron rotation in the event handler, not via useEffect
    Animated.spring(chevronAnim, {
      toValue: nextOpen ? 1 : 0,
      useNativeDriver: SUPPORTS_NATIVE_DRIVER,
      ...animation.spring.gentle
    }).start();
    LayoutAnimation.configureNext({
      duration: animation.duration.normal,
      update: {
        type: LayoutAnimation.Types.easeInEaseOut
      },
      create: {
        type: LayoutAnimation.Types.easeInEaseOut,
        property: LayoutAnimation.Properties.opacity
      },
      delete: {
        type: LayoutAnimation.Types.easeInEaseOut,
        property: LayoutAnimation.Properties.opacity
      }
    });
    setIsOpen(nextOpen);
  }, [isOpen, chevronAnim]);
  const chevronRotation = chevronAnim.interpolate({
    inputRange: [0, 1],
    outputRange: [CHEVRON_CLOSED, CHEVRON_OPEN]
  });
  return /*#__PURE__*/_jsxs(View, {
    style: style,
    testID: testID,
    children: [/*#__PURE__*/_jsxs(TouchableOpacity, {
      onPress: handleToggle,
      activeOpacity: 0.7,
      accessibilityRole: "button",
      accessibilityState: {
        expanded: isOpen
      },
      style: styles.trigger,
      children: [chevronIcon ?? /*#__PURE__*/_jsx(Animated.Text, {
        style: [styles.chevron, {
          color: theme.colors.textSecondary,
          transform: [{
            rotate: chevronRotation
          }]
        }],
        children: "\u203A"
      }), /*#__PURE__*/_jsx(Text, {
        style: [styles.title, {
          color: theme.colors.text
        }, titleStyle],
        children: title
      })]
    }), isOpen && children]
  });
};
export const Collapsible = /*#__PURE__*/memo(CollapsibleComponent);
Collapsible.displayName = 'Collapsible';
const styles = StyleSheet.create({
  trigger: {
    flexDirection: 'row',
    alignItems: 'center',
    paddingVertical: 12,
    gap: 8
  },
  chevron: {
    fontSize: 16
  },
  title: {
    fontSize: 16,
    fontWeight: '600',
    flex: 1
  }
});
//# sourceMappingURL=Collapsible.js.map