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


let root: Root | undefined = undefined;

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

        root = createRoot(element);

        root.render(<App widget />);
    },
    unmount() {
        if (root) root.unmount();
    }
};

export default widget;

/*
<script type="module">
    import widget from ".../widget.js";

    widget.create({
        selector: "widget-container"
    })
</script>
*/
