// #region Imports
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { on } from '@breadstone/mosaik-elements';
import { CustomElement } from '../Controls/Components/Abstracts/CustomElement';
import { Component } from '../Controls/Decorators/ComponentDecorator';
import { DomServiceLocator } from '../Dom/Dom';
import { RendererServiceLocator } from '../Dom/Renderer';
import { IRouteView } from './Route/Interfaces/IBaseRouteConfig';
import { RouterElement } from './RouterElement';
import { routerOutletElementStyle } from './RouterOutletElementStyle';
import { routerOutletElementTemplate } from './RouterOutletElementTemplate';
// #endregion
/**
 * The `RouterOutletElement` element.
 *
 * @public
 */
let RouterOutletElement = class RouterOutletElement extends CustomElement {
    // #region Fields
    _router;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._router = null;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     * The `is` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-router-outlet';
    }
    // #endregion
    // #region Methods
    onApplyTemplate() {
        super.onApplyTemplate();
        const slot = DomServiceLocator.current.findDescendant(this.renderRoot, 'slot', { strict: true });
        this._router = DomServiceLocator.current.findAncestor(this, RouterElement, { strict: true });
        on(this._router, 'navigated', () => {
            const view = this._router?.outlet();
            if (view) {
                if (IRouteView.is(view)) {
                    void this.resolveView(view).then((v) => {
                        if (v) {
                            RendererServiceLocator.current.clear(slot);
                            RendererServiceLocator.current.appendChild(slot, v);
                        }
                    });
                }
                else {
                    RendererServiceLocator.current.clear(slot);
                    this.requestUpdate();
                    RendererServiceLocator.current.appendChild(slot, view);
                    this.requestUpdate();
                }
            }
        });
    }
    /**
     * Resolves a route view to an HTMLElement.
     *
     * @private
     * @param view - The view to resolve.
     */
    async resolveView(view) {
        // Resolve a string.
        if (typeof view === 'string') {
            return Promise.resolve(document.createElement(view));
        }
        // Resolve a class.
        if (typeof view === 'function' && view.prototype instanceof HTMLElement) {
            return Promise.resolve(new view());
        }
        // Resolve a promise.
        if (isPromise(view)) {
            return view.then((v) => this.resolveView(v));
        }
        return Promise.resolve(null);
    }
};
RouterOutletElement = __decorate([
    Component({
        selector: 'mosaik-router-outlet',
        template: routerOutletElementTemplate,
        styles: routerOutletElementStyle
    }),
    __metadata("design:paramtypes", [])
], RouterOutletElement);
export { RouterOutletElement };
function isPromise(obj) {
    // allow any Promise/A+ compliant thenable.
    // It's up to the caller to ensure that obj.then conforms to the spec
    return obj && typeof obj.then === 'function';
}
//# sourceMappingURL=RouterOutletElement.js.map