import { jsx as _jsx } from "react/jsx-runtime";
import { UrlContentPreview } from "../Components/EmbedContent/UrlContentPreview";
import { findFirstUrl } from "../utils/urlHelpers";
import { findOembedProvider, hasOembedProvider } from "../utils/oembedProviders";
import { getOGContent } from "../utils/DOMUtils";
import { useActions } from "../overmind";
import { useCallback, useEffect, useState } from "react";
import ReactDOMServer from "react-dom/server";
import isEmpty from "lodash/isEmpty";
export const useEmbed = (content, maxHeight, maxWidth) => {
    const [error, setError] = useState("");
    const [url, setUrl] = useState("");
    const [embedContent, setEmbedContent] = useState();
    const [isLoading, setIsLoading] = useState(false);
    const actions = useActions();
    const getUrlContent = useCallback(() => {
        const callPromise = async () => {
            try {
                const data = await actions.api.post.getRemoteMeta({
                    url,
                });
                if (data.text && !isEmpty(data.text)) {
                    const ogContent = getOGContent(data.text);
                    if (ogContent) {
                        setEmbedContent(ReactDOMServer.renderToStaticMarkup(_jsx(UrlContentPreview, { imgSrc: ogContent.image || "", title: ogContent.title || "", description: ogContent.description || "", redirectUrl: url })));
                    }
                    else {
                        setEmbedContent("");
                    }
                }
                setIsLoading(false);
                setError("");
            }
            catch (e) {
                setError(e);
            }
            finally {
                setIsLoading(false);
            }
        };
        callPromise();
    }, [actions.api.post, url]);
    const getEmbedData = useCallback(() => {
        const callPromise = async () => {
            const provider = findOembedProvider(url);
            const params = new URLSearchParams();
            params.set("url", url);
            params.set("format", "json");
            params.set("maxwidth", maxWidth || "");
            params.set("maxheight", maxHeight || "");
            try {
                const data = await actions.api.post.getRemoteMeta({
                    url: provider?.fetchEndpoint + "?" + params.toString(),
                });
                if (data?.text) {
                    setEmbedContent(JSON.parse(data.text).html);
                }
            }
            catch (e) {
                getUrlContent();
            }
            finally {
                setIsLoading(false);
            }
        };
        callPromise();
    }, [actions.api.post, getUrlContent, maxHeight, maxWidth, url]);
    useEffect(() => {
        const url = findFirstUrl(content);
        setUrl(url);
        if (isEmpty(url)) {
            setUrl("");
        }
    }, [content]);
    useEffect(() => {
        if (!isEmpty(url)) {
            setIsLoading(true);
            if (!hasOembedProvider(url)) {
                getUrlContent();
            }
            else {
                getEmbedData();
            }
        }
        else {
            setEmbedContent("");
        }
    }, [getEmbedData, getUrlContent, url]);
    return {
        isLoading,
        error,
        embedContent,
    };
};
//# sourceMappingURL=useEmbed.js.map