import React, {createContext, useContext} from 'react';
import {SwipeableListItemContextValue} from './types';

/**
 * Context for sharing state across SwipeableListItem compound components
 */
export const SwipeableListItemContext = createContext<SwipeableListItemContextValue | null>(null);

/**
 * Hook to access SwipeableListItem context
 * @throws Error if used outside of SwipeableListItem provider
 */
export const useSwipeableListItemContext = (): SwipeableListItemContextValue => {
  const context = useContext(SwipeableListItemContext);
  if (!context) {
    throw new Error(
      'SwipeableListItem compound components must be used within a SwipeableListItem component'
    );
  }
  return context;
};
