"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AccordionTrigger = exports.AccordionItem = exports.AccordionContent = exports.Accordion = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _useInteractionState = require("../hooks/useInteractionState.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); }
// ---- Context ----

const AccordionContext = /*#__PURE__*/(0, _react.createContext)({
  expandedValues: new Set(),
  toggle: () => {},
  type: 'single'
});
const AccordionItemContext = /*#__PURE__*/(0, _react.createContext)({
  value: '',
  isExpanded: false,
  disabled: false
});

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

const AccordionComponent = ({
  value,
  onValueChange,
  type = 'single',
  children,
  style,
  testID
}) => {
  const expandedValues = (0, _react.useMemo)(() => {
    if (value == null) return new Set();
    if (Array.isArray(value)) return new Set(value);
    return new Set([value]);
  }, [value]);
  const toggle = (0, _react.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 = (0, _react.useMemo)(() => ({
    expandedValues,
    toggle,
    type
  }), [expandedValues, toggle, type]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(AccordionContext.Provider, {
    value: contextValue,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: style,
      testID: testID,
      children: children
    })
  });
};

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

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

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

const AccordionTriggerComponent = ({
  children,
  icon,
  style,
  textStyle
}) => {
  const theme = (0, _useTheme.useTheme)();
  const {
    toggle
  } = (0, _react.useContext)(AccordionContext);
  const {
    value,
    isExpanded,
    disabled
  } = (0, _react.useContext)(AccordionItemContext);
  const rotateAnim = (0, _react.useRef)(new _reactNative.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
  } = (0, _useInteractionState.useInteractionState)();
  (0, _react.useEffect)(() => {
    _reactNative.Animated.spring(rotateAnim, {
      toValue: isExpanded ? 1 : 0,
      useNativeDriver: _nativeDriver.SUPPORTS_NATIVE_DRIVER,
      ..._tokens.animation.spring.snappy
    }).start();
  }, [isExpanded, rotateAnim]);
  const handlePress = (0, _react.useCallback)(() => {
    if (!disabled) {
      toggle(value);
    }
  }, [value, disabled, toggle]);
  const rotation = rotateAnim.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '180deg']
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Pressable, {
    style: [{
      flexDirection: 'row',
      alignItems: 'center',
      paddingVertical: _tokens.space.md,
      paddingHorizontal: _tokens.space.xs,
      gap: _tokens.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__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        flex: 1
      },
      children: typeof children === 'string' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: [{
          fontSize: 15,
          fontWeight: '600',
          color: theme.colors.text
        }, textStyle],
        children: children
      }) : children
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, {
      style: {
        transform: [{
          rotate: rotation
        }]
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: {
          fontSize: 16,
          color: theme.colors.textSecondary,
          lineHeight: 18
        },
        children: '\u25BE'
      })
    })]
  });
};

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

const AccordionContentComponent = ({
  children,
  style
}) => {
  const {
    isExpanded
  } = (0, _react.useContext)(AccordionItemContext);
  const heightAnim = (0, _react.useRef)(new _reactNative.Animated.Value(isExpanded ? 1 : 0)).current;
  (0, _react.useEffect)(() => {
    _reactNative.Animated.spring(heightAnim, {
      toValue: isExpanded ? 1 : 0,
      useNativeDriver: false,
      ..._tokens.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__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, {
    style: [{
      overflow: 'hidden',
      opacity,
      maxHeight
    }, style],
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        paddingBottom: _tokens.space.md,
        paddingHorizontal: _tokens.space.xs
      },
      children: children
    })
  });
};
const Accordion = exports.Accordion = /*#__PURE__*/(0, _react.memo)(AccordionComponent);
Accordion.displayName = 'Accordion';
const AccordionItem = exports.AccordionItem = /*#__PURE__*/(0, _react.memo)(AccordionItemComponent);
AccordionItem.displayName = 'AccordionItem';
const AccordionTrigger = exports.AccordionTrigger = /*#__PURE__*/(0, _react.memo)(AccordionTriggerComponent);
AccordionTrigger.displayName = 'AccordionTrigger';
const AccordionContent = exports.AccordionContent = /*#__PURE__*/(0, _react.memo)(AccordionContentComponent);
AccordionContent.displayName = 'AccordionContent';
//# sourceMappingURL=Accordion.js.map