import { Renderer } from './Renderer.js';
import { ModalActivity } from '../ModalActivity.js';
export class ApplicationRenderer extends Renderer {
    app;
    renderedFirst;
    constructor(app) {
        super();
        this.app = app;
        this.renderedFirst = new WeakMap();
    }
    _handleFirstRender(target, clearRoot, root) {
        if (!this.renderedFirst.has(target)) {
            this.renderedFirst.set(target, true);
            if (clearRoot) {
                this._clearRoot(root);
            }
            queueMicrotask(() => target.onFirstRender());
        }
    }
    renderer() {
        const { app, renderRoot: defaultRenderRoot } = this;
        const activity = app.manager.getTopActivity();
        const host = activity || app;
        const root = activity?.renderRoot || defaultRenderRoot;
        let content;
        if (activity instanceof ModalActivity) {
            content = activity.renderDialog();
        }
        else {
            content = host.render();
        }
        if (!root) {
            // eslint-disable-next-line no-console
            console.warn(`The "renderRoot" is not set up.`);
            return;
        }
        this._handleFirstRender(app, true, root);
        if (!this.firstRendered) {
            this.firstRenderedFlag = true;
        }
        if (host !== app) {
            this._handleFirstRender(host, false, root);
        }
        this._render(content, root, host);
        this.app.onRendered();
    }
}
//# sourceMappingURL=ApplicationRenderer.js.map