{"version":3,"file":"use-slot-drag-select.cjs","names":[],"sources":["../../src/hooks/use-slot-drag-select.ts"],"sourcesContent":["import { useCallback, useEffect, useEffectEvent, useRef, useState } from 'react';\n\ninterface DragState {\n  group: string;\n  startIndex: number;\n  currentIndex: number;\n}\n\ninterface SelectedRange {\n  group: string;\n  start: number;\n  end: number;\n}\n\ninterface UseSlotDragSelectInput {\n  enabled?: boolean;\n  onDragEnd?: (startIndex: number, endIndex: number, group: string) => void;\n}\n\nexport function useSlotDragSelect({ enabled = true, onDragEnd }: UseSlotDragSelectInput) {\n  const [selectedRange, setSelectedRange] = useState<SelectedRange | null>(null);\n  const dragRef = useRef<DragState | null>(null);\n  const stableOnDragEnd = useEffectEvent(onDragEnd || (() => {}));\n\n  const handleSlotPointerDown = useCallback(\n    (_event: React.PointerEvent<HTMLButtonElement>, index: number, group: string) => {\n      if (!enabled) {\n        return;\n      }\n      dragRef.current = { group, startIndex: index, currentIndex: index };\n      setSelectedRange({ group, start: index, end: index });\n    },\n    [enabled]\n  );\n\n  const isDragging = selectedRange !== null;\n\n  useEffect(() => {\n    if (!isDragging) {\n      return undefined;\n    }\n\n    const savedUserSelect = document.body.style.userSelect;\n    document.body.style.userSelect = 'none';\n\n    const handlePointerMove = (e: PointerEvent) => {\n      if (!dragRef.current) {\n        return;\n      }\n\n      const elements = document.elementsFromPoint(e.clientX, e.clientY);\n      for (const el of elements) {\n        const indexAttr = (el as HTMLElement).getAttribute?.('data-drag-slot-index');\n        const groupAttr = (el as HTMLElement).getAttribute?.('data-drag-slot-group');\n        if (indexAttr != null && groupAttr != null) {\n          if (groupAttr === dragRef.current.group) {\n            const newIndex = Number(indexAttr);\n            if (!isNaN(newIndex) && newIndex !== dragRef.current.currentIndex) {\n              dragRef.current.currentIndex = newIndex;\n              setSelectedRange({\n                group: groupAttr,\n                start: Math.min(dragRef.current.startIndex, newIndex),\n                end: Math.max(dragRef.current.startIndex, newIndex),\n              });\n            }\n          }\n          break;\n        }\n      }\n    };\n\n    const handlePointerUp = () => {\n      if (dragRef.current) {\n        const { startIndex, currentIndex, group } = dragRef.current;\n        if (startIndex !== currentIndex) {\n          stableOnDragEnd(\n            Math.min(startIndex, currentIndex),\n            Math.max(startIndex, currentIndex),\n            group\n          );\n        }\n      }\n      dragRef.current = null;\n      setSelectedRange(null);\n    };\n\n    document.addEventListener('pointermove', handlePointerMove);\n    document.addEventListener('pointerup', handlePointerUp);\n\n    return () => {\n      document.body.style.userSelect = savedUserSelect;\n      document.removeEventListener('pointermove', handlePointerMove);\n      document.removeEventListener('pointerup', handlePointerUp);\n    };\n  }, [isDragging]);\n\n  const isSlotSelected = useCallback(\n    (index: number, group: string): boolean => {\n      if (!selectedRange || selectedRange.group !== group) {\n        return false;\n      }\n      return index >= selectedRange.start && index <= selectedRange.end;\n    },\n    [selectedRange]\n  );\n\n  return {\n    handleSlotPointerDown,\n    isSlotSelected,\n    isDragging,\n  };\n}\n"],"mappings":";;;AAmBA,SAAgB,kBAAkB,EAAE,UAAU,MAAM,aAAqC;CACvF,MAAM,CAAC,eAAe,qBAAA,GAAA,MAAA,SAAA,CAAmD,IAAI;CAC7E,MAAM,WAAA,GAAA,MAAA,OAAA,CAAmC,IAAI;CAC7C,MAAM,mBAAA,GAAA,MAAA,eAAA,CAAiC,oBAAoB,CAAC,EAAE;CAE9D,MAAM,yBAAA,GAAA,MAAA,YAAA,EACH,QAA+C,OAAe,UAAkB;EAC/E,IAAI,CAAC,SACH;EAEF,QAAQ,UAAU;GAAE;GAAO,YAAY;GAAO,cAAc;EAAM;EAClE,iBAAiB;GAAE;GAAO,OAAO;GAAO,KAAK;EAAM,CAAC;CACtD,GACA,CAAC,OAAO,CACV;CAEA,MAAM,aAAa,kBAAkB;CAErC,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,IAAI,CAAC,YACH;EAGF,MAAM,kBAAkB,SAAS,KAAK,MAAM;EAC5C,SAAS,KAAK,MAAM,aAAa;EAEjC,MAAM,qBAAqB,MAAoB;GAC7C,I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