"use strict";

import React, { createContext, memo, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
import { View, Text, Pressable, Animated } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { useInteractionState } from "../hooks/useInteractionState.js";
import { animation, space } from "../styles/tokens.js";
import { SUPPORTS_NATIVE_DRIVER } from "../styles/native-driver.js";

// ---- Context ----
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const AccordionContext = /*#__PURE__*/createContext({
  expandedValues: new Set(),
  toggle: () => {},
  type: 'single'
});
const AccordionItemContext = /*#__PURE__*/createContext({
  value: '',
  isExpanded: false,
  disabled: false
});

// ---- Accordion Root ----

const AccordionComponent = ({
  value,
  onValueChange,
  type = 'single',
  children,
  style,
  testID
}) => {
  const expandedValues = useMemo(() => {
    if (value == null) return new Set();
    if (Array.isArray(value)) return new Set(value);
    return new Set([value]);
  }, [value]);
  const toggle = useCallback(itemValue => {
    if (type === 'single') {
      const next = expandedValues.has(itemValue) ? undefined : itemValue;
      onValueChange(next);
    } else {
      const next = new Set(expandedValues);
      if (next.has(itemValue)) {
        next.delete(itemValue);
      } else {
        next.add(itemValue);
      }
      onValueChange(Array.from(next));
    }
  }, [type, expandedValues, onValueChange]);
  const contextValue = useMemo(() => ({
    expandedValues,
    toggle,
    type
  }), [expandedValues, toggle, type]);
  return /*#__PURE__*/_jsx(AccordionContext.Provider, {
    value: contextValue,
    children: /*#__PURE__*/_jsx(View, {
      style: style,
      testID: testID,
      children: children
    })
  });
};

// ---- Accordion Item ----

const AccordionItemComponent = ({
  value,
  children,
  disabled = false,
  style
}) => {
  const {
    expandedValues
  } = useContext(AccordionContext);
  const isExpanded = expandedValues.has(value);
  const theme = useTheme();
  const itemContext = useMemo(() => ({
    value,
    isExpanded,
    disabled
  }), [value, isExpanded, disabled]);
  return /*#__PURE__*/_jsx(AccordionItemContext.Provider, {
    value: itemContext,
    children: /*#__PURE__*/_jsx(View, {
      style: [{
        borderBottomWidth: 1,
        borderBottomColor: theme.colors.borderLight
      }, style],
      children: children
    })
  });
};

// ---- Accordion Trigger ----

const AccordionTriggerComponent = ({
  children,
  icon,
  style,
  textStyle
}) => {
  const theme = useTheme();
  const {
    toggle
  } = useContext(AccordionContext);
  const {
    value,
    isExpanded,
    disabled
  } = useContext(AccordionItemContext);
  const rotateAnim = useRef(new Animated.Value(isExpanded ? 1 : 0)).current;
  // Drive press-opacity via state, not Pressable's function-form `style`,
  // which NativeWind v4's css-interop swallows (dropping the trigger's base
  // layout: flexDirection, padding, gap).
  const {
    state: pressed,
    onIn: onPressIn,
    onOut: onPressOut
  } = useInteractionState();
  useEffect(() => {
    Animated.spring(rotateAnim, {
      toValue: isExpanded ? 1 : 0,
      useNativeDriver: SUPPORTS_NATIVE_DRIVER,
      ...animation.spring.snappy
    }).start();
  }, [isExpanded, rotateAnim]);
  const handlePress = useCallback(() => {
    if (!disabled) {
      toggle(value);
    }
  }, [value, disabled, toggle]);
  const rotation = rotateAnim.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '180deg']
  });
  return /*#__PURE__*/_jsxs(Pressable, {
    style: [{
      flexDirection: 'row',
      alignItems: 'center',
      paddingVertical: space.md,
      paddingHorizontal: space.xs,
      gap: space.sm,
      opacity: disabled ? 0.4 : pressed ? 0.7 : 1
    }, style],
    onPress: handlePress,
    onPressIn: disabled ? undefined : onPressIn,
    onPressOut: disabled ? undefined : onPressOut,
    disabled: disabled,
    accessibilityRole: "button",
    accessibilityState: {
      expanded: isExpanded,
      disabled
    },
    children: [icon, /*#__PURE__*/_jsx(View, {
      style: {
        flex: 1
      },
      children: typeof children === 'string' ? /*#__PURE__*/_jsx(Text, {
        style: [{
          fontSize: 15,
          fontWeight: '600',
          color: theme.colors.text
        }, textStyle],
        children: children
      }) : children
    }), /*#__PURE__*/_jsx(Animated.View, {
      style: {
        transform: [{
          rotate: rotation
        }]
      },
      children: /*#__PURE__*/_jsx(Text, {
        style: {
          fontSize: 16,
          color: theme.colors.textSecondary,
          lineHeight: 18
        },
        children: '\u25BE'
      })
    })]
  });
};

// ---- Accordion Content ----

const AccordionContentComponent = ({
  children,
  style
}) => {
  const {
    isExpanded
  } = useContext(AccordionItemContext);
  const heightAnim = useRef(new Animated.Value(isExpanded ? 1 : 0)).current;
  useEffect(() => {
    Animated.spring(heightAnim, {
      toValue: isExpanded ? 1 : 0,
      useNativeDriver: false,
      ...animation.spring.gentle
    }).start();
  }, [isExpanded, heightAnim]);
  const opacity = heightAnim.interpolate({
    inputRange: [0, 1],
    outputRange: [0, 1]
  });
  const maxHeight = heightAnim.interpolate({
    inputRange: [0, 1],
    outputRange: [0, 500] // reasonable max; content will use its natural height
  });
  return /*#__PURE__*/_jsx(Animated.View, {
    style: [{
      overflow: 'hidden',
      opacity,
      maxHeight
    }, style],
    children: /*#__PURE__*/_jsx(View, {
      style: {
        paddingBottom: space.md,
        paddingHorizontal: space.xs
      },
      children: children
    })
  });
};
export const Accordion = /*#__PURE__*/memo(AccordionComponent);
Accordion.displayName = 'Accordion';
export const AccordionItem = /*#__PURE__*/memo(AccordionItemComponent);
AccordionItem.displayName = 'AccordionItem';
export const AccordionTrigger = /*#__PURE__*/memo(AccordionTriggerComponent);
AccordionTrigger.displayName = 'AccordionTrigger';
export const AccordionContent = /*#__PURE__*/memo(AccordionContentComponent);
AccordionContent.displayName = 'AccordionContent';
//# sourceMappingURL=Accordion.js.map