"use client";
import * as React from 'react';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';

function cn(...inputs) {
  return twMerge(clsx(inputs));
}
function filterFiles(files, { accept, maxSize, maxFiles }) {
  const list = Array.from(files);
  const acceptList = accept ? accept.split(",").map((s) => s.trim()).filter(Boolean) : void 0;
  const matchAccept = (file) => {
    if (!acceptList) return true;
    return acceptList.some((entry) => {
      if (entry.startsWith(".")) {
        return file.name.toLowerCase().endsWith(entry.toLowerCase());
      }
      if (entry.endsWith("/*")) {
        const prefix = entry.slice(0, -1);
        return file.type.startsWith(prefix);
      }
      return file.type === entry;
    });
  };
  const sized = typeof maxSize === "number" ? list.filter((f) => f.size <= maxSize) : list;
  const accepted = sized.filter(matchAccept);
  if (typeof maxFiles === "number") return accepted.slice(0, maxFiles);
  return accepted;
}
function Dropzone({
  onFilesSelected,
  onFilesRejected,
  accept,
  multiple = true,
  maxFiles,
  maxSize,
  disabled = false,
  children,
  className,
  "aria-label": ariaLabel,
  ...rest
}) {
  const inputRef = React.useRef(null);
  const [isDragOver, setIsDragOver] = React.useState(false);
  const dragCounter = React.useRef(0);
  const emit = React.useCallback(
    (files) => {
      if (!files || disabled) return;
      const all = Array.from(files);
      if (all.length === 0) return;
      const accepted = filterFiles(all, { accept, maxSize, maxFiles });
      if (accepted.length === 0) {
        onFilesRejected?.(all);
        return;
      }
      const finalAccepted = !multiple ? accepted.slice(0, 1) : accepted;
      const rejected = all.filter((f) => !finalAccepted.includes(f));
      onFilesSelected?.(finalAccepted);
      if (rejected.length > 0) onFilesRejected?.(rejected);
    },
    [
      accept,
      disabled,
      maxFiles,
      maxSize,
      multiple,
      onFilesSelected,
      onFilesRejected
    ]
  );
  React.useEffect(() => {
    const reset = () => {
      dragCounter.current = 0;
      setIsDragOver(false);
    };
    window.addEventListener("dragend", reset);
    window.addEventListener("drop", reset);
    return () => {
      window.removeEventListener("dragend", reset);
      window.removeEventListener("drop", reset);
    };
  }, []);
  const openFileDialog = React.useCallback(() => {
    if (disabled) return;
    inputRef.current?.click();
  }, [disabled]);
  const handleDragEnter = (e) => {
    if (disabled) return;
    e.preventDefault();
    dragCounter.current += 1;
    if (e.dataTransfer.types.includes("Files")) setIsDragOver(true);
  };
  const handleDragOver = (e) => {
    if (disabled) return;
    e.preventDefault();
    e.dataTransfer.dropEffect = "copy";
  };
  const handleDragLeave = (e) => {
    if (disabled) return;
    e.preventDefault();
    dragCounter.current = Math.max(0, dragCounter.current - 1);
    if (dragCounter.current === 0) setIsDragOver(false);
  };
  const handleDrop = (e) => {
    if (disabled) return;
    e.preventDefault();
    dragCounter.current = 0;
    setIsDragOver(false);
    emit(e.dataTransfer.files);
  };
  const handleKeyDown = (e) => {
    if (disabled) return;
    if (e.key === "Enter" || e.key === " ") {
      e.preventDefault();
      openFileDialog();
    }
  };
  const renderState = {
    isDragOver,
    isDisabled: disabled,
    openFileDialog
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      "div",
      {
        role: "button",
        tabIndex: disabled ? -1 : 0,
        "aria-label": ariaLabel,
        "aria-disabled": disabled || void 0,
        "data-drag-over": isDragOver || void 0,
        onClick: openFileDialog,
        onKeyDown: handleKeyDown,
        onDragEnter: handleDragEnter,
        onDragOver: handleDragOver,
        onDragLeave: handleDragLeave,
        onDrop: handleDrop,
        className: cn(
          "flex w-full cursor-pointer select-none flex-col items-center justify-center gap-[var(--space-2,0.5rem)] rounded-md border-2 border-dashed border-input bg-background px-[var(--space-6,1.5rem)] py-[var(--space-8,2rem)] text-center text-sm transition-all duration-[var(--duration-normal,200ms)] ease-out",
          "hover:bg-accent hover:text-accent-foreground",
          "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
          isDragOver && "border-primary bg-accent text-accent-foreground",
          disabled && "pointer-events-none opacity-50",
          className
        ),
        ...rest,
        children: typeof children === "function" ? children(renderState) : children ?? /* @__PURE__ */ jsxs(Fragment, { children: [
          /* @__PURE__ */ jsxs(
            "svg",
            {
              xmlns: "http://www.w3.org/2000/svg",
              viewBox: "0 0 24 24",
              fill: "none",
              stroke: "currentColor",
              strokeWidth: "2",
              strokeLinecap: "round",
              strokeLinejoin: "round",
              className: "h-6 w-6 text-muted-foreground",
              "aria-hidden": "true",
              children: [
                /* @__PURE__ */ jsx("path", { d: "M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" }),
                /* @__PURE__ */ jsx("polyline", { points: "17 8 12 3 7 8" }),
                /* @__PURE__ */ jsx("line", { x1: "12", y1: "3", x2: "12", y2: "15" })
              ]
            }
          ),
          /* @__PURE__ */ jsx("span", { className: "font-medium", children: isDragOver ? "Drop files to upload" : "Drag files here or click to browse" }),
          accept ? /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: accept }) : null
        ] })
      }
    ),
    /* @__PURE__ */ jsx(
      "input",
      {
        ref: inputRef,
        type: "file",
        accept,
        multiple,
        disabled,
        "aria-hidden": "true",
        tabIndex: -1,
        className: "sr-only",
        onChange: (e) => {
          emit(e.target.files);
          e.target.value = "";
        }
      }
    )
  ] });
}
Dropzone.displayName = "Dropzone";

export { Dropzone };
//# sourceMappingURL=dropzone.js.map
//# sourceMappingURL=dropzone.js.map