import { useEffect, useState } from "react";
import micromatch from "micromatch";
import isEmpty from "lodash/isEmpty";
// import fetchJsonp from "fetch-jsonp";
import { OEMBED_PROVIDERS } from "../constants";
async function fetchJson(url) {
    const resp = await fetch(url);
    return await resp.json();
}
export const useOembed = (url, maxHeight, maxWidth) => {
    const [error, setError] = useState("");
    const [oembed, setOembed] = useState();
    const [isLoading, setIsLoading] = useState(true);
    useEffect(() => {
        if (!isEmpty(url)) {
            let apiUrl = "";
            for (const prov of OEMBED_PROVIDERS) {
                for (const endpoint of prov.endpoints) {
                    if (endpoint.schemes && micromatch.isMatch(url, endpoint.schemes)) {
                        apiUrl = endpoint.url.replace("{format}", "json");
                        break;
                    }
                    else if (!endpoint.schemes &&
                        prov.provider_url &&
                        url.startsWith(prov.provider_url)) {
                        apiUrl = prov.provider_url;
                        break;
                    }
                }
            }
            if (isEmpty(apiUrl)) {
                setError(`No Oembed Provider found for: <i>${url}</i>
        This URL may not be publicly embeddable. See the <a href"/">list of support providers on the home page</a>
        `);
            }
            else {
                try {
                    const params = new URLSearchParams();
                    params.set("url", url);
                    params.set("format", "json");
                    params.set("maxwidth", maxWidth || "");
                    params.set("maxheight", maxHeight || "");
                    const fetchOembedData = async () => {
                        setIsLoading(true);
                        // if noembed fails in the future, use this:
                        // const data = await (
                        //   await fetchJsonp(apiUrl + "?" + params.toString(), {
                        //     jsonpCallbackFunction: "json    ",
                        //   })
                        // ).json();
                        //const data = await fetchJson(apiUrl + "?" + params.toString());
                        const data = await fetchJson(
                        // more information: https://noembed.com/
                        "https://noembed.com/embed?" + params.toString());
                        setOembed(data);
                        setIsLoading(false);
                    };
                    fetchOembedData();
                    setError("");
                }
                catch (e) {
                    setError("Oembed API Json could not be loaded: " + apiUrl);
                    setIsLoading(false);
                }
            }
        }
    }, [maxHeight, maxWidth, url]);
    useEffect(() => {
        if (!isEmpty(error)) {
            console.log(error);
        }
    }, [error]);
    return {
        isLoading,
        error,
        oembed,
    };
};
//# sourceMappingURL=useOembed.js.map