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;
};
import { UmbRoutePathAddendumResetContext } from '../contexts/route-path-addendum-reset.context.js';
import { UmbRouterSlotInitEvent } from './router-slot-init.event.js';
import { UmbRouterSlotChangeEvent } from './router-slot-change.event.js';
import { UmbRouteContext } from './route.context.js';
import { css, html, customElement, property } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../lit-element/index.js';
/**
 *  @element umb-router-slot
 *  @description - Component for wrapping Router Slot element, providing some local events for implementation.
 *  @augments UmbLitElement
 * @fires {UmbRouterSlotInitEvent} init - fires when the router is connected
 * @fires {UmbRouterSlotChangeEvent} change - fires when a path of this router is changed
 */
let UmbRouterSlotElement = class UmbRouterSlotElement extends UmbLitElement {
    #router;
    #modalRouter;
    #listening;
    get routes() {
        return this.#router.routes;
    }
    set routes(value) {
        value ??= [];
        const oldValue = this.#router.routes;
        if (value.length !== oldValue?.length ||
            value.filter((route) => oldValue?.findIndex((r) => r.path === route.path) === -1).length > 0) {
            this.#router.routes = value;
        }
    }
    get parent() {
        return this.#router.parent;
    }
    set parent(parent) {
        this.#router.parent = parent;
    }
    get absoluteRouterPath() {
        return this._routerPath;
    }
    get localActiveViewPath() {
        return this._activeLocalPath;
    }
    get absoluteActiveViewPath() {
        return this._routerPath + '/' + this._activeLocalPath;
    }
    #routeContext;
    constructor() {
        super();
        this.#router = document.createElement('router-slot');
        this.#modalRouter = document.createElement('router-slot');
        this.#listening = false;
        this.#routeContext = new UmbRouteContext(this, this.#router, this.#modalRouter);
        // Event handler as arrow function to maintain consistent reference
        this._updateRouterPath = () => {
            const newAbsolutePath = this._constructAbsoluteRouterPath();
            if (this._routerPath !== newAbsolutePath) {
                this._routerPath = newAbsolutePath;
                this.#routeContext._internal_routerGotBasePath(this._routerPath);
                this.dispatchEvent(new UmbRouterSlotInitEvent());
                const newActiveLocalPath = this._constructLocalRouterPath();
                if (this._activeLocalPath !== newActiveLocalPath) {
                    this._activeLocalPath = newActiveLocalPath;
                    this.#routeContext._internal_routerGotActiveLocalPath(this._activeLocalPath);
                    this.dispatchEvent(new UmbRouterSlotChangeEvent());
                }
            }
        };
        this._onNavigationChanged = (event) => {
            if (event.detail.slot === this.#router) {
                const newActiveLocalPath = this._constructLocalRouterPath();
                if (this._activeLocalPath !== newActiveLocalPath) {
                    this._activeLocalPath = newActiveLocalPath;
                    this.#routeContext._internal_routerGotActiveLocalPath(newActiveLocalPath);
                    this.dispatchEvent(new UmbRouterSlotChangeEvent());
                }
            }
            else if (event.detail.slot === this.#modalRouter) {
                const newActiveModalLocalPath = this.#modalRouter.match?.route.path ?? '';
                this.#routeContext._internal_modalRouterChanged(newActiveModalLocalPath);
            }
        };
        this.#modalRouter.parent = this.#router;
        this.#modalRouter.style.display = 'none';
        this.#router.addEventListener('changestate', this._updateRouterPath);
        this.#router.appendChild(document.createElement('slot'));
    }
    _constructAbsoluteRouterPath() {
        return this.#router.constructAbsolutePath('') || '';
    }
    _constructLocalRouterPath() {
        return this.#router.match?.fragments.consumed ?? '';
    }
    connectedCallback() {
        if (this.inheritAddendum !== true) {
            new UmbRoutePathAddendumResetContext(this);
        }
        super.connectedCallback();
        // Currently we have to set this every time as RouteSlot looks for its parent every-time it is connected. Aka it has not way to explicitly set the parent.
        // And we cannot insert the modal router as a slotted-child of the router, as it flushes its children on every route change.
        this.#modalRouter.parent = this.#router;
        if (this.#listening === false) {
            window.addEventListener('navigationsuccess', this._onNavigationChanged);
            this.#listening = true;
        }
    }
    disconnectedCallback() {
        this.#router.removeEventListener('changestate', this._updateRouterPath);
        window.removeEventListener('navigationsuccess', this._onNavigationChanged);
        this.#listening = false;
        // Close modals opened by this router slot.
        this.#routeContext._internal_modalRouterChanged(undefined);
        super.disconnectedCallback();
    }
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        this._updateRouterPath();
    }
    render() {
        return html `${this.#router}${this.#modalRouter}`;
    }
    static { this.styles = [
        css `
			:host {
				position: relative;
				height: 100%;
			}

			router-slot {
				height: 100%;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Boolean, attribute: 'inherit-addendum', reflect: false })
], UmbRouterSlotElement.prototype, "inheritAddendum", void 0);
__decorate([
    property({ attribute: false })
], UmbRouterSlotElement.prototype, "routes", null);
__decorate([
    property({ attribute: false })
], UmbRouterSlotElement.prototype, "parent", null);
UmbRouterSlotElement = __decorate([
    customElement('umb-router-slot')
], UmbRouterSlotElement);
export { UmbRouterSlotElement };
