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_BLOCK_WORKSPACE_CONTEXT } from '../../block-workspace.context-token.js';
import './block-workspace-view-edit-tab.element.js';
import { css, html, customElement, state, repeat, nothing } from '../../../../../../external/lit/index.js';
import { UmbTextStyles } from '../../../../../core/style/index.js';
import { UmbContentTypeContainerStructureHelper } from '../../../../../content/content-type/index.js';
import { UmbLitElement } from '../../../../../core/lit-element/index.js';
import { UmbViewController } from '../../../../../core/view/index.js';
import { encodeFolderName } from '../../../../../core/router/index.js';
/**
 * @internal only for use inside this class.
 * Gets the view alias for a given tab. This is used to create a unique view context for each tab in the block workspace.
 * @param {UmbPropertyTypeContainerMergedModel} tab - The tab to get the view alias for.
 * @returns {string} The view alias for the tab.
 */
function getViewAliasForTab(tab) {
    return 'tab/' + encodeFolderName(tab.name ?? '');
}
/**
 * @element umb-block-workspace-view-edit-content-no-router
 * @description
 * A specific view for editing content in a block workspace placed inline within a block view/element.
 */
let UmbBlockWorkspaceViewEditContentNoRouterElement = class UmbBlockWorkspaceViewEditContentNoRouterElement extends UmbLitElement {
    #blockWorkspace;
    #blockManager;
    #tabsStructureHelper;
    #tabViewContexts;
    constructor() {
        super();
        this.#tabsStructureHelper = new UmbContentTypeContainerStructureHelper(this);
        this._hintMap = new Map();
        this.#tabViewContexts = [];
        this.#tabsStructureHelper.setIsRoot(true);
        this.#tabsStructureHelper.setContainerChildType('Tab');
        this.observe(this.#tabsStructureHelper.childContainers, (tabs) => {
            this._tabs = tabs;
            this.#setupViewContexts();
        });
        this.observe(this.#tabsStructureHelper.hasProperties, (hasRootProperties) => {
            this._hasRootProperties = hasRootProperties;
            this.#setupViewContexts();
        }, 'observeRootProperties');
        this.consumeContext(UMB_BLOCK_WORKSPACE_CONTEXT, (context) => {
            this.#blockWorkspace = context;
            this.#blockManager = context?.content;
            // block manager does not need to be setup this in file as that it being done by the implementation of this element.
            this.#tabsStructureHelper.setStructureManager(context?.content.structure);
            this.#observeRootGroups();
        });
    }
    async #observeRootGroups() {
        if (!this.#blockWorkspace)
            return;
        this.observe(await this.#blockManager?.structure.hasRootContainers('Group'), (hasRootGroups) => {
            this._hasRootGroups = hasRootGroups ?? false;
            this.#setupViewContexts();
        }, 'observeGroups');
    }
    #setupViewContexts() {
        if (!this._tabs || !this.#blockManager)
            return;
        // Create view contexts for root groups/properties
        if (this._hasRootGroups || this._hasRootProperties) {
            this.#createViewContext(null, '#general_generic');
        }
        // Create view contexts for all tabs
        this._tabs.forEach((tab) => {
            const viewAlias = getViewAliasForTab(tab);
            this.#createViewContext(viewAlias, tab.name ?? '');
        });
        this.#checkDefaultTabName();
    }
    #createViewContext(viewAlias, tabName) {
        if (!this.#blockManager) {
            throw new Error('Block Manager not found');
        }
        if (!this.#tabViewContexts.find((context) => context.viewAlias === viewAlias)) {
            const view = new UmbViewController(this, viewAlias);
            this.#tabViewContexts.push(view);
            if (viewAlias === null) {
                // for the root tab, we need to filter hints
                view.hints.setPathFilter((paths) => {
                    const firstPath = paths[0];
                    // Treat empty paths as "not in a tab", so they belong to the root tab
                    if (!firstPath) {
                        return true;
                    }
                    return firstPath.includes('tab/') === false;
                });
            }
            view.setTitle(tabName);
            view.inheritFrom(this.#blockManager.view);
            this.observe(view.firstHintOfVariant, (hint) => {
                if (hint) {
                    this._hintMap.set(viewAlias, hint);
                }
                else {
                    this._hintMap.delete(viewAlias);
                }
                this.requestUpdate('_hintMap');
            }, 'umbObserveState_' + viewAlias);
        }
    }
    #checkDefaultTabName() {
        if (!this._tabs ||
            !this.#blockWorkspace ||
            this._hasRootGroups === undefined ||
            this._hasRootProperties === undefined) {
            return;
        }
        // Find the default tab to grab
        if (this._activeTabKey === undefined) {
            if (this._hasRootGroups || this._hasRootProperties) {
                const context = this.#tabViewContexts.find((context) => context.viewAlias === null);
                if (context) {
                    this._activeTabKey = null;
                    this.#provideViewContext(null);
                }
            }
            else if (this._tabs.length > 0) {
                const tab = this._tabs[0];
                if (tab) {
                    this._activeTabKey = tab.ownerId ?? tab.ids[0];
                    this.#provideViewContext(getViewAliasForTab(tab));
                }
            }
        }
    }
    #setCurrentTabPath(tabKey, viewAlias) {
        // find the key of the view context that we want to show based on the path, and set it to active
        const context = this.#tabViewContexts.find((context) => context.viewAlias === viewAlias);
        if (context) {
            this._activeTabKey = tabKey;
            this.#provideViewContext(viewAlias);
        }
    }
    #currentProvidedView;
    #provideViewContext(viewAlias) {
        const view = this.#tabViewContexts.find((context) => context.viewAlias === viewAlias);
        if (this.#currentProvidedView === view) {
            return;
        }
        this.#currentProvidedView?.unprovide();
        if (!view) {
            throw new Error(`View context with alias ${viewAlias} not found`);
        }
        this.#currentProvidedView = view;
        // ViewAlias null is only for the root tab, therefor we can implement this hack.
        if (viewAlias === null) {
            // Specific hack for the Generic tab to only show its name if there are other tabs.
            view.setTitle(this._tabs && this._tabs?.length > 0 ? '#general_generic' : undefined);
        }
        view.provideAt(this);
    }
    render() {
        if (!this._tabs)
            return;
        return html `
			${this._tabs.length > 1 || (this._tabs.length === 1 && (this._hasRootGroups || this._hasRootProperties))
            ? html `<uui-tab-group slot="header">
						${(this._hasRootGroups || this._hasRootProperties) && this._tabs.length > 0
                ? this.#renderTab(null, null, '#general_generic')
                : nothing}
						${repeat(this._tabs, (tab) => tab.name, (tab) => {
                const tabKey = tab.ownerId ?? tab.ids[0];
                const viewAlias = 'tab/' + encodeFolderName(tab.name || '');
                return this.#renderTab(tabKey, viewAlias, tab.name);
            })}
					</uui-tab-group>`
            : nothing}
			${this._activeTabKey !== undefined
            ? html `<umb-block-workspace-view-edit-tab
						.managerName=${'content'}
						.hideSingleGroup=${true}
						.containerId=${this._activeTabKey}>
					</umb-block-workspace-view-edit-tab>`
            : nothing}
		`;
    }
    #renderTab(tabKey, viewAlias, name) {
        const hint = this._hintMap.get(viewAlias);
        const active = this._activeTabKey === tabKey;
        return html `<uui-tab
			label=${this.localize.string(name ?? '#general_unnamed')}
			.active=${active}
			@click=${() => this.#setCurrentTabPath(tabKey, viewAlias)}
			data-mark="content-tab:${viewAlias ?? 'root'}"
			>${hint && !active
            ? html `<umb-badge slot="extra" .color=${hint.color ?? 'default'} ?attention=${hint.color === 'invalid'}
						>${hint.text}</umb-badge
					>`
            : nothing}</uui-tab
		>`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				position: relative;
				display: block;
				height: 100%;
				--uui-tab-background: var(--uui-color-surface);

				padding: calc(var(--uui-size-layout-1));
			}
			umb-badge {
				--uui-badge-inset: 0 0 auto auto;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbBlockWorkspaceViewEditContentNoRouterElement.prototype, "_hasRootProperties", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditContentNoRouterElement.prototype, "_hasRootGroups", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditContentNoRouterElement.prototype, "_tabs", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditContentNoRouterElement.prototype, "_activeTabKey", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditContentNoRouterElement.prototype, "_hintMap", void 0);
UmbBlockWorkspaceViewEditContentNoRouterElement = __decorate([
    customElement('umb-block-workspace-view-edit-content-no-router')
], UmbBlockWorkspaceViewEditContentNoRouterElement);
export { UmbBlockWorkspaceViewEditContentNoRouterElement };
export default UmbBlockWorkspaceViewEditContentNoRouterElement;
