import * as React from "react";
//#region src/hooks/use-drop-zone.ts
/** Check whether a file matches a single accept token (exact MIME, wildcard MIME, or extension). */
function matchesAcceptToken(file, token) {
	const trimmed = token.trim();
	if (trimmed.startsWith(".")) return file.name.toLowerCase().endsWith(trimmed.toLowerCase());
	if (trimmed.includes("/*")) {
		const prefix = trimmed.split("/")[0];
		return file.type.startsWith(`${prefix}/`);
	}
	return file.type === trimmed;
}
/** Filter files against a comma-separated accept string. Returns matching files. */
function filterFiles(files, accept) {
	const tokens = accept.split(",");
	return files.filter((file) => tokens.some((token) => matchesAcceptToken(file, token)));
}
/** Build a FileList from an array of Files using DataTransfer. */
function toFileList(files) {
	const dt = new DataTransfer();
	for (const file of files) dt.items.add(file);
	return dt.files;
}
function useDropZone({ onFilesSelected, accept, multiple = true, disabled = false, containerRef }) {
	const [isDragging, setIsDragging] = React.useState(false);
	const dragCounterRef = React.useRef(0);
	const isActive = !disabled && onFilesSelected !== void 0;
	const handleDragEnter = React.useCallback((e) => {
		e.preventDefault();
		e.stopPropagation();
		if (!isActive) return;
		if (!e.dataTransfer.types.includes("Files")) return;
		dragCounterRef.current += 1;
		if (dragCounterRef.current === 1) setIsDragging(true);
	}, [isActive]);
	const handleDragOver = React.useCallback((e) => {
		e.preventDefault();
		e.stopPropagation();
	}, []);
	const handleDragLeave = React.useCallback((e) => {
		e.preventDefault();
		e.stopPropagation();
		if (!isActive) return;
		dragCounterRef.current -= 1;
		if (dragCounterRef.current <= 0) {
			dragCounterRef.current = 0;
			setIsDragging(false);
		}
	}, [isActive]);
	const handleDrop = React.useCallback((e) => {
		e.preventDefault();
		e.stopPropagation();
		dragCounterRef.current = 0;
		setIsDragging(false);
		if (!isActive) return;
		let files = Array.from(e.dataTransfer.files);
		if (accept) files = filterFiles(files, accept);
		if (!multiple && files.length > 1) files = [files[0]];
		if (files.length === 0) return;
		onFilesSelected(toFileList(files));
	}, [
		isActive,
		accept,
		multiple,
		onFilesSelected
	]);
	React.useEffect(() => {
		const doc = containerRef?.current?.ownerDocument ?? document;
		const handleDocDrop = (e) => {
			e.preventDefault();
			if (dragCounterRef.current > 0) {
				dragCounterRef.current = 0;
				setIsDragging(false);
			}
		};
		doc.addEventListener("drop", handleDocDrop);
		return () => {
			doc.removeEventListener("drop", handleDocDrop);
		};
	}, [containerRef]);
	const openFilePicker = React.useCallback(() => {
		if (!isActive) return;
		const doc = containerRef?.current?.ownerDocument ?? document;
		const input = doc.createElement("input");
		input.type = "file";
		if (accept) input.accept = accept;
		input.multiple = multiple;
		input.addEventListener("change", () => {
			if (input.files && input.files.length > 0) onFilesSelected?.(input.files);
			input.remove();
		});
		doc.body.appendChild(input);
		input.click();
	}, [
		isActive,
		accept,
		multiple,
		onFilesSelected,
		containerRef
	]);
	return {
		isDragging,
		dropZoneProps: {
			onDragEnter: handleDragEnter,
			onDragOver: handleDragOver,
			onDragLeave: handleDragLeave,
			onDrop: handleDrop
		},
		openFilePicker
	};
}
//#endregion
export { useDropZone };

//# sourceMappingURL=index384.js.map