import { render } from 'lit';
import { Renderer, renderFn } 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();
    }
    [renderFn]() {
        const { app, renderRoot } = this;
        let host;
        let root = renderRoot;
        let content;
        const activity = app.manager.getTopActivity();
        if (activity) {
            host = activity;
            if (activity.renderRoot) {
                root = activity.renderRoot;
            }
            if (activity instanceof ModalActivity) {
                content = activity.renderDialog();
            }
            else {
                content = activity.render();
            }
        }
        else {
            host = app;
            content = app.render();
        }
        if (!root) {
            // eslint-disable-next-line no-console
            console.warn(`The "renderRoot" is not set up.`);
            return;
        }
        if (!this.renderedFirst.has(app)) {
            this.renderedFirst.set(app, true);
            Array.from(root.childNodes).forEach((node) => root.parentNode?.removeChild(node));
            setTimeout(() => app.onFirstRender());
        }
        // host might !== app
        if (!this.renderedFirst.has(host)) {
            setTimeout(() => host.onFirstRender());
        }
        const litPart = root;
        if (litPart._$litPart$ && litPart._$litPart$.options) {
            litPart._$litPart$.options.host = host;
        }
        render(content, root, { host });
        this.resolveUpdatePromise();
        this.app.onRendered();
    }
}
//# sourceMappingURL=ApplicationRenderer.js.map