import React from "react";
import { TinyMCE } from "./tinymce";

let interval: any;

export default function useTinyMCE() {
    const [tinyMCE, setTinyMCE] = React.useState<TinyMCE | null>(null);

    React.useEffect(() => {
        // @ts-ignore
        if (window.tinymce) {
            console.log("Tinymce already exists");
            // @ts-ignore
            setTinyMCE(window.tinymce);
            return;
        }

        const script = document.createElement("script");
        script.src =
            "https://www.datasquirel.com/tinymce-public/tinymce.min.js";
        script.async = true;

        document.head.appendChild(script);

        script.onload = () => {
            // @ts-ignore
            if (window.tinymce) {
                // @ts-ignore
                setTinyMCE(window.tinymce);
            }
        };
    }, []);

    return { tinyMCE };
}
