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 { isMenuItemExpansionEntry } from '../components/menu-item/expansion/is-menu-item-expansion-entry.guard.js';
import { UMB_SECTION_SIDEBAR_MENU_SECTION_CONTEXT } from './section-context/section-sidebar-menu.section-context.token.js';
import { UmbTextStyles } from '../../style/index.js';
import { css, html, customElement, property } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../lit-element/index.js';
import { UmbExpansionEntryCollapsedEvent, UmbExpansionEntryExpandedEvent } from '../../utils/index.js';
import { UmbExtensionSlotElement } from '../../extension-registry/index.js';
let UmbSectionSidebarMenuElement = class UmbSectionSidebarMenuElement extends UmbLitElement {
    renderHeader() {
        /*
        Notice we are not using the UUI-H3 here, we would need to wrap it into uui-text for this to take action,
        but it does look odd so lets return to this later. I have made a few corrections especially for this component. [NL]
        */
        return html `<h3>${this.localize.string(this.manifest?.meta?.label ?? '')}</h3>`;
    }
    #sectionSidebarMenuContext;
    #extensionSlotElement = new UmbExtensionSlotElement();
    #muteStateUpdate = false;
    constructor() {
        super();
        this.#initExtensionSlotElement();
        this.consumeContext(UMB_SECTION_SIDEBAR_MENU_SECTION_CONTEXT, (context) => {
            this.#sectionSidebarMenuContext = context;
            this.#observeExpansion();
        });
    }
    #initExtensionSlotElement() {
        /* For better performance and UX we prevent lit from doing unnecessary rerenders we programmatically create the element,
        and manually update the props when needed. */
        this.#extensionSlotElement.type = 'menu';
        this.#extensionSlotElement.filter = (menu) => menu.alias === this.manifest?.meta?.menu;
        this.#extensionSlotElement.defaultElement = 'umb-menu';
        this.#extensionSlotElement.events = {
            [UmbExpansionEntryExpandedEvent.TYPE]: this.#onExpansionChange.bind(this),
            [UmbExpansionEntryCollapsedEvent.TYPE]: this.#onExpansionChange.bind(this),
        };
    }
    #observeExpansion() {
        this.observe(this.#sectionSidebarMenuContext?.expansion.expansion, (items) => {
            if (this.#muteStateUpdate)
                return;
            this.#extensionSlotElement.props = {
                expansion: items || [],
            };
        });
    }
    #onExpansionChange(e) {
        const event = e;
        event.stopPropagation();
        const eventEntry = event.entry;
        if (!eventEntry) {
            throw new Error('Entity is required to toggle expansion.');
        }
        // Only react to the event if it is a valid Menu Item Expansion Entry
        if (isMenuItemExpansionEntry(eventEntry) === false)
            return;
        if (event.type === UmbExpansionEntryExpandedEvent.TYPE) {
            this.#muteStateUpdate = true;
            this.#sectionSidebarMenuContext?.expansion.expandItem(eventEntry);
            this.#muteStateUpdate = false;
        }
        else if (event.type === UmbExpansionEntryCollapsedEvent.TYPE) {
            this.#muteStateUpdate = true;
            this.#sectionSidebarMenuContext?.expansion.collapseItem(eventEntry);
            this.#muteStateUpdate = false;
        }
    }
    render() {
        return html ` ${this.renderHeader()} ${this.#extensionSlotElement}`;
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.#extensionSlotElement.destroy();
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			h3 {
				display: flex;
				align-items: center;
				height: var(--umb-header-layout-height);
				margin: 0;
				padding: var(--uui-size-4) var(--uui-size-8);
				box-sizing: border-box;
				font-size: 14px;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Object, attribute: false })
], UmbSectionSidebarMenuElement.prototype, "manifest", void 0);
UmbSectionSidebarMenuElement = __decorate([
    customElement('umb-section-sidebar-menu')
], UmbSectionSidebarMenuElement);
export { UmbSectionSidebarMenuElement };
export default UmbSectionSidebarMenuElement;
