import QuizAnswerer from "@/components/QuizAnswerer";
import { Root, createRoot } from "react-dom/client";


let root: Root | undefined = undefined;

const widget = {
    create({ selector, quizId, changeFaviconAndTitle = true }: {
        selector: string;
        quizId: string;
        changeFaviconAndTitle: boolean;
    }) {
        const element = document.getElementById(selector);
        if (!element) throw new Error("Element for widget doesn't exist");

        root = createRoot(element);

        root.render(<QuizAnswerer quizId={quizId} changeFaviconAndTitle={changeFaviconAndTitle} />);
    },
    unmount() {
        if (root) root.unmount();
    }
};

export default widget;
