import { computed, isRef, readonly, ref } from "vue";
import MimeTypeParser from "whatwg-mimetype";
//#region src/hooks/useFileDialog.ts
/**
* Simplified interface for opening a file picker dialog
* Derived from vueuse @see https://vueuse.org/useFileDialog
*/
function useFileDialog({ multiple, accept, onChange, onError } = {}) {
	const files = ref(null);
	let input;
	if (typeof document !== "undefined") {
		input = document.createElement("input");
		input.type = "file";
		input.onchange = (event) => {
			files.value = event.target.files;
			onChange?.(files.value);
		};
		input.onerror = () => onError?.();
		input.multiple = multiple;
		input.accept = accept;
	}
	const open = () => {
		if (!input) return onError?.();
		input.click();
	};
	return {
		files: readonly(files),
		open
	};
}
//#endregion
//#region src/libs/extractAttachmentFilename.ts
var decodeURIComponentSafe = (str) => {
	try {
		return decodeURIComponent(str);
	} catch {
		return str;
	}
};
/**
* Extract the filename from a content disposition header
*/
function extractFilename(contentDisposition) {
	let filename = "";
	if (contentDisposition) {
		const fileNameMatch = contentDisposition.match(/filename\*=UTF-8''([^;]+)/)?.[1] ?? contentDisposition.match(/filename\s*=\s*"?([^";]+)"?/)?.[1];
		if (fileNameMatch) filename = decodeURIComponentSafe(fileNameMatch.trim());
	}
	return filename;
}
//#endregion
//#region src/hooks/useResponseBody.ts
/**
* Processes the response body of an HTTP request.
* Extracts MIME type, attachment filename, and generates a data URL.
*/
function useResponseBody(props) {
	const isBlob = (b) => b instanceof Blob;
	const dataRef = computed(() => isRef(props.data) ? props.data.value : props.data);
	const headersRef = computed(() => isRef(props.headers) ? props.headers.value : props.headers);
	const mimeType = computed(() => {
		return new MimeTypeParser(headersRef.value.find((header) => header.name.toLowerCase() === "content-type")?.value ?? "");
	});
	return {
		mimeType,
		attachmentFilename: computed(() => {
			return extractFilename(headersRef.value.find((header) => header.name.toLowerCase() === "content-disposition")?.value ?? "");
		}),
		dataUrl: computed(() => {
			if (isBlob(dataRef.value)) return URL.createObjectURL(dataRef.value);
			if (typeof dataRef.value === "string") return URL.createObjectURL(new Blob([dataRef.value], { type: mimeType.value.toString() }));
			if (dataRef.value instanceof Object && Object.keys(dataRef.value).length) return URL.createObjectURL(new Blob([JSON.stringify(dataRef.value)], { type: mimeType.value.toString() }));
			return "";
		})
	};
}
//#endregion
export { useFileDialog as n, useResponseBody as t };

//# sourceMappingURL=hooks-BvZxH4XB.js.map