

import { neo__ } from "./_global--translation.js";
import { pluginUrl } from "./_global-plugin-and-uploads-url.js";
import { isNeoMotionFrameElement } from "./neo-motion--draw-export.js";

export async function interfaceNeoDrawMotionPreviewButtonSuppressErrorPopup20260611() {
    return ({ elements, exportSvgAsString, onError, buttonClassName, buttonLoadingClassName, spinnerClassName, previewPopupId }) => {
        const [motionPreviewDialogLoading, setMotionPreviewDialogLoading] = React.useState(false);
        const [motionPreviewDialogUrl, setMotionPreviewDialogUrl] = React.useState(null);
        const hasMotionPreviewFrames = elements.some(isNeoMotionFrameElement);
        React.useEffect(() => () => { document.querySelector("#" + previewPopupId)?.remove(); }, [previewPopupId]);
        React.useEffect(() => {
            if (!motionPreviewDialogUrl) { return; }
            const motionPreviewPopup = document.querySelector("#" + previewPopupId) ?? document.body.appendChild(Object.assign(document.createElement("neo-media-preview-popup-neo-motion-beta"), { id: previewPopupId }));
            if (motionPreviewPopup?.open) { motionPreviewPopup.open(motionPreviewDialogUrl); return; }
            motionPreviewPopup?.setAttribute("src", motionPreviewDialogUrl); motionPreviewPopup?.setAttribute("open", "");
        }, [motionPreviewDialogUrl, previewPopupId]);
        React.useEffect(() => () => { if (motionPreviewDialogUrl) { URL.revokeObjectURL(motionPreviewDialogUrl); } }, [motionPreviewDialogUrl]);
        if (!hasMotionPreviewFrames) { return null; }
        return React.createElement(
            React.Fragment,
            null,
            React.createElement(
                "button",
                {
                    className: buttonClassName + (motionPreviewDialogLoading ? " " + buttonLoadingClassName : ""),
                    key: "motion-preview-button",
                    title: neo__("neoMotion preview", "neoMotion Vorschau"),
                    onClick: async () => {
                        setMotionPreviewDialogLoading(true);
                        try {
                            const motionPreviewBlobUrl = URL.createObjectURL(new Blob([await exportSvgAsString()], { type: "image/svg+xml" }));
                            setMotionPreviewDialogUrl((oldMotionPreviewDialogUrl) => { if (oldMotionPreviewDialogUrl) { URL.revokeObjectURL(oldMotionPreviewDialogUrl); } return motionPreviewBlobUrl; });
                        } catch (e) {
                            onError(e);
                        }
                        setMotionPreviewDialogLoading(false);
                    },
                },
                React.createElement(
                    "img",
                    { src: pluginUrl() + "/img/neo-motion--play-icon.svg" }
                ),
                React.createElement(
                    "div",
                    {
                        key: "motion-preview-button-spinner",
                        className: spinnerClassName,
                    }
                )
            )
        );
    };
}
