import { useContext, useLayoutEffect } from 'react';
import { ListItemContext, type ListItemContextData } from './ListItemContext';
import type { ListItemTypes, ListItemControlProps } from './ListItem';

export function useListItemControl(controlType: ListItemTypes, controlProps: ListItemControlProps) {
  const {
    setControlType,
    setControlProps,
    props: baseItemProps,
  } = useContext<ListItemContextData>(ListItemContext);

  useLayoutEffect(() => {
    setControlType(controlType);
    setControlProps(controlProps);
  }, [controlType, controlProps, setControlType, setControlProps]);

  return { baseItemProps };
}
