import { Root, createRoot } from "react-dom/client";
import WidgetApp from "./WidgetApp";


let root: Root | undefined = undefined;

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

        root = createRoot(element);

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

export default widget;
