import { extractFilename } from "../libs/extractAttachmentFilename.js";
import { computed, isRef } from "vue";
import MimeType from "whatwg-mimetype";
//#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 MimeType(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 { useResponseBody };

//# sourceMappingURL=useResponseBody.js.map