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 { UMB_DOCUMENT_BLUEPRINT_WORKSPACE_CONTEXT } from './document-blueprint-workspace.context-token.js';
import { UmbDocumentBlueprintWorkspaceSplitViewElement } from './document-blueprint-workspace-split-view.element.js';
import { customElement, state, css, html } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { UmbTextStyles } from '../../../core/style/index.js';
let UmbDocumentBlueprintWorkspaceEditorElement = class UmbDocumentBlueprintWorkspaceEditorElement extends UmbLitElement {
    #workspaceContext;
    #variants;
    #isForbidden;
    constructor() {
        super();
        //
        // TODO: Refactor: when having a split view/variants context token, we can rename the split view/variants component to a generic and make this component generic as well. [NL]
        this._splitViewElement = new UmbDocumentBlueprintWorkspaceSplitViewElement();
        this.#isForbidden = false;
        this._gotWorkspaceRoute = (e) => {
            this.#workspaceContext?.splitView.setWorkspaceRoute(e.target.absoluteRouterPath);
        };
        this.consumeContext(UMB_DOCUMENT_BLUEPRINT_WORKSPACE_CONTEXT, (instance) => {
            this.#workspaceContext = instance;
            this.#observeVariants();
            this.#observeForbidden();
        });
    }
    #observeVariants() {
        if (!this.#workspaceContext)
            return;
        this.observe(this.#workspaceContext.variantOptions, (variants) => {
            this.#variants = variants;
            this._generateRoutes();
        }, '_observeVariants');
    }
    #observeForbidden() {
        this.observe(this.#workspaceContext?.forbidden.isOn, (isForbidden) => {
            this.#isForbidden = isForbidden ?? false;
            this._generateRoutes();
        }, '_observeForbidden');
    }
    async _generateRoutes() {
        // Generate split view routes for all available routes
        const routes = [];
        // Split view routes:
        this.#variants?.forEach((variantA) => {
            this.#variants?.forEach((variantB) => {
                routes.push({
                    // TODO: When implementing Segments, be aware if using the unique is URL Safe... [NL]
                    path: variantA.unique + '_&_' + variantB.unique,
                    component: this._splitViewElement,
                    setup: (_component, info) => {
                        // Set split view/active info..
                        this.#workspaceContext?.splitView.setVariantParts(info.match.fragments.consumed);
                    },
                });
            });
        });
        // Single view:
        this.#variants?.forEach((variant) => {
            routes.push({
                // TODO: When implementing Segments, be aware if using the unique is URL Safe... [NL]
                path: variant.unique,
                component: this._splitViewElement,
                setup: (_component, info) => {
                    // cause we might come from a split-view, we need to reset index 1.
                    this.#workspaceContext?.splitView.removeActiveVariant(1);
                    this.#workspaceContext?.splitView.handleVariantFolderPart(0, info.match.fragments.consumed);
                },
            });
        });
        if (routes.length !== 0 && this.#variants?.length) {
            // Using first single view as the default route for now (hence the math below):
            routes.push({
                path: '',
                pathMatch: 'full',
                redirectTo: routes[this.#variants.length * this.#variants.length]?.path,
            });
        }
        routes.push({
            path: `**`,
            component: async () => {
                const router = await import('../../../core/router/index.js');
                return this.#isForbidden ? router.UmbRouteForbiddenElement : router.UmbRouteNotFoundElement;
            },
        });
        this._routes = routes;
    }
    render() {
        return this._routes
            ? html `<umb-router-slot .routes=${this._routes} @init=${this._gotWorkspaceRoute}></umb-router-slot>`
            : '';
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: block;
				width: 100%;
				height: 100%;

				--uui-color-invalid: var(--uui-color-warning);
				--uui-color-invalid-emphasis: var(--uui-color-warning-emphasis);
				--uui-color-invalid-standalone: var(--uui-color-warning-standalone);
				--uui-color-invalid-contrast: var(--uui-color-warning-contrast);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbDocumentBlueprintWorkspaceEditorElement.prototype, "_routes", void 0);
UmbDocumentBlueprintWorkspaceEditorElement = __decorate([
    customElement('umb-document-blueprint-workspace-editor')
], UmbDocumentBlueprintWorkspaceEditorElement);
export { UmbDocumentBlueprintWorkspaceEditorElement };
export default UmbDocumentBlueprintWorkspaceEditorElement;
