import { useSortable } from '@dnd-kit/sortable'

import { ItemRenderer } from './item-renderer'
import type { RenderItemType } from './types'

interface SortableItemProps<T> {
  item: T
  id: string | number
  index: number
  renderItem: RenderItemType<T>
  disabled?: boolean
  useDragOverlay?: boolean
  showGhost?: boolean
  itemStyle?: React.CSSProperties
}

export function SortableItem<T>({
  item,
  id,
  index,
  renderItem,
  disabled = false,
  useDragOverlay = true,
  showGhost = true,
  itemStyle
}: SortableItemProps<T>) {
  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
    id,
    disabled
  })

  return (
    <ItemRenderer
      ref={setNodeRef}
      item={item}
      index={index}
      renderItem={renderItem}
      dragging={isDragging}
      dragOverlay={!useDragOverlay && isDragging}
      ghost={showGhost && useDragOverlay && isDragging}
      transform={transform}
      transition={transition}
      listeners={listeners}
      itemStyle={itemStyle}
      {...attributes}
    />
  )
}
