"use client";
const require_use_isomorphic_effect = require("../use-isomorphic-effect/use-isomorphic-effect.cjs");
let react = require("react");
//#region packages/@mantine/hooks/src/use-file-dialog/use-file-dialog.ts
const defaultOptions = {
	multiple: true,
	accept: "*"
};
function getInitialFilesList(files) {
	if (!files) return null;
	if (files instanceof FileList) return files;
	const result = new DataTransfer();
	for (const file of files) result.items.add(file);
	return result.files;
}
function createInput(options) {
	if (typeof document === "undefined") return null;
	const input = document.createElement("input");
	input.type = "file";
	if (options.accept) input.accept = options.accept;
	if (options.multiple) input.multiple = options.multiple;
	if (options.capture) input.capture = options.capture;
	if (options.directory) input.webkitdirectory = options.directory;
	input.style.display = "none";
	return input;
}
function useFileDialog(input = {}) {
	const options = {
		...defaultOptions,
		...input
	};
	const [files, setFiles] = (0, react.useState)(getInitialFilesList(options.initialFiles));
	const inputRef = (0, react.useRef)(null);
	const handleChange = (0, react.useCallback)((event) => {
		const target = event.target;
		if (target?.files) {
			setFiles(target.files);
			options.onChange?.(target.files);
		}
	}, [options.onChange]);
	const createAndSetupInput = (0, react.useCallback)(() => {
		inputRef.current?.remove();
		inputRef.current = createInput(options);
		if (inputRef.current) {
			inputRef.current.addEventListener("change", handleChange, { once: true });
			if (options.onCancel) inputRef.current.addEventListener("cancel", options.onCancel, { once: true });
			document.body.appendChild(inputRef.current);
		}
	}, [options, handleChange]);
	require_use_isomorphic_effect.useIsomorphicEffect(() => {
		createAndSetupInput();
		return () => inputRef.current?.remove();
	}, []);
	const reset = (0, react.useCallback)(() => {
		setFiles(null);
		options.onChange?.(null);
	}, [options.onChange]);
	return {
		files,
		open: (0, react.useCallback)(() => {
			if (options.resetOnOpen) reset();
			createAndSetupInput();
			inputRef.current?.click();
		}, [
			options.resetOnOpen,
			reset,
			createAndSetupInput
		]),
		reset
	};
}
//#endregion
exports.useFileDialog = useFileDialog;

//# sourceMappingURL=use-file-dialog.cjs.map