"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Collapsible = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _tokens = require("../styles/tokens.js");
var _nativeDriver = require("../styles/native-driver.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
if (_reactNative.Platform.OS === 'android' && _reactNative.UIManager.setLayoutAnimationEnabledExperimental) {
  _reactNative.UIManager.setLayoutAnimationEnabledExperimental(true);
}
const CHEVRON_CLOSED = '0deg';
const CHEVRON_OPEN = '90deg';
const CollapsibleComponent = ({
  title,
  children,
  defaultOpen = false,
  chevronIcon,
  style,
  titleStyle,
  testID
}) => {
  const [isOpen, setIsOpen] = (0, _react.useState)(defaultOpen);
  const theme = (0, _useTheme.useTheme)();
  const chevronAnim = (0, _react.useRef)(new _reactNative.Animated.Value(defaultOpen ? 1 : 0)).current;
  const handleToggle = (0, _react.useCallback)(() => {
    const nextOpen = !isOpen;

    // Animate chevron rotation in the event handler, not via useEffect
    _reactNative.Animated.spring(chevronAnim, {
      toValue: nextOpen ? 1 : 0,
      useNativeDriver: _nativeDriver.SUPPORTS_NATIVE_DRIVER,
      ..._tokens.animation.spring.gentle
    }).start();
    _reactNative.LayoutAnimation.configureNext({
      duration: _tokens.animation.duration.normal,
      update: {
        type: _reactNative.LayoutAnimation.Types.easeInEaseOut
      },
      create: {
        type: _reactNative.LayoutAnimation.Types.easeInEaseOut,
        property: _reactNative.LayoutAnimation.Properties.opacity
      },
      delete: {
        type: _reactNative.LayoutAnimation.Types.easeInEaseOut,
        property: _reactNative.LayoutAnimation.Properties.opacity
      }
    });
    setIsOpen(nextOpen);
  }, [isOpen, chevronAnim]);
  const chevronRotation = chevronAnim.interpolate({
    inputRange: [0, 1],
    outputRange: [CHEVRON_CLOSED, CHEVRON_OPEN]
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: style,
    testID: testID,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.TouchableOpacity, {
      onPress: handleToggle,
      activeOpacity: 0.7,
      accessibilityRole: "button",
      accessibilityState: {
        expanded: isOpen
      },
      style: styles.trigger,
      children: [chevronIcon ?? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.Text, {
        style: [styles.chevron, {
          color: theme.colors.textSecondary,
          transform: [{
            rotate: chevronRotation
          }]
        }],
        children: "\u203A"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: [styles.title, {
          color: theme.colors.text
        }, titleStyle],
        children: title
      })]
    }), isOpen && children]
  });
};
const Collapsible = exports.Collapsible = /*#__PURE__*/(0, _react.memo)(CollapsibleComponent);
Collapsible.displayName = 'Collapsible';
const styles = _reactNative.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