/** biome-ignore-all lint/complexity/noBannedTypes: {} is the "empty props" type in React */
import type { Node, NodePathMap } from "godot"
import * as React from "react"
import * as Reconciler from "./Reconciler.js"


const ConcurrentRoot = 1

export const renderComponent: {
    (
        container: Node<NodePathMap>,
        component: React.FC<{}>,
    ): void
    <P>(
        container: Node<NodePathMap>,
        component: React.FC<P>,
        props: NoInfer<P>,
    ): void
} = (
    container: Node<NodePathMap>,
    component: React.FC<{}>,
    props?: Record<string, unknown>,
): void => {
    const reconciler = Reconciler.make()

    if (!(container as any)._rootContainer)
        (container as any)._rootContainer = (reconciler as any).createContainer(
            container,
            ConcurrentRoot,
            null,
            false,
            false,
            "",
            console.error,
        )

    reconciler.updateContainer(
        React.createElement(component, props),
        (container as any)._rootContainer,
        null,
    )
}
