import { createContext, useContext } from 'react';

import { type ItemContextValue, type SortableContextValue } from './types';

export const SortableListContext = createContext<SortableContextValue | null>(
  null,
);

export const useSortableListContext = () => {
  const context = useContext(SortableListContext);
  if (!context) {
    throw new Error(
      'SortableList components must be used within SortableList.Root',
    );
  }
  return context;
};

export const ItemContext = createContext<ItemContextValue | null>(null);

export const useItemContext = () => {
  const context = useContext(ItemContext);
  if (!context) {
    throw new Error('DragHandle must be used within SortableList.Item');
  }
  return context;
};
