import { useEffect, useState } from "react";
import { useStliteKernel } from "./StliteKernelProvider";
/**
 * Converts a raw media URL into an object URL
 * if it points to an in-memory file
 * by downloading the data from the stlite kernel.
 */
export function useStliteMediaObjectUrl(rawUrl) {
    const kernel = useStliteKernel();
    const [url, setUrl] = useState(rawUrl);
    useEffect(() => {
        if (kernel == null) {
            return;
        }
        if (!rawUrl.startsWith("/media")) {
            return;
        }
        let released = false;
        let objectUrl;
        kernel
            .sendHttpRequest({
            method: "GET",
            path: rawUrl,
            headers: {},
            body: "",
        })
            .then(({ statusCode, headers, body }) => {
            if (released) {
                return;
            }
            if (statusCode !== 200) {
                return;
            }
            const blob = new Blob([body], { type: headers.get("Content-Type") });
            objectUrl = URL.createObjectURL(blob);
            setUrl(objectUrl);
        });
        return () => {
            released = true;
            if (objectUrl != null) {
                URL.revokeObjectURL(objectUrl);
            }
        };
    }, [kernel, rawUrl]);
    return url;
}
//# sourceMappingURL=media.js.map