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 { html, nothing, customElement, property, state } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../lit-element/index.js';
import { UMB_MENU_ITEM_CONTEXT } from '../../menu/index.js';
import { UmbExpansionEntryCollapsedEvent, UmbExpansionEntryExpandedEvent, } from '../../utils/index.js';
let UmbMenuItemTreeDefaultElement = class UmbMenuItemTreeDefaultElement extends UmbLitElement {
    #menuItemContext;
    #muteStateUpdate;
    constructor() {
        super();
        this._menuItemExpansion = [];
        this.#muteStateUpdate = false;
        this.consumeContext(UMB_MENU_ITEM_CONTEXT, (context) => {
            this.#menuItemContext = context;
            this.#observeExpansion();
        });
    }
    #observeExpansion() {
        this.observe(this.#menuItemContext?.expansion.expansion, (items) => {
            if (this.#muteStateUpdate)
                return;
            this._menuItemExpansion = items || [];
        });
    }
    #onExpansionChange(event) {
        event.stopPropagation();
        const eventEntry = event.entry;
        if (!eventEntry) {
            throw new Error('Entry is required to toggle expansion.');
        }
        if (!this.manifest) {
            throw new Error('Manifest is required to toggle expansion.');
        }
        if (event.type === UmbExpansionEntryExpandedEvent.TYPE) {
            this.#muteStateUpdate = true;
            this.#menuItemContext?.expansion.expandItem({ ...eventEntry, menuItemAlias: this.manifest.alias });
            this.#muteStateUpdate = false;
        }
        else if (event.type === UmbExpansionEntryCollapsedEvent.TYPE) {
            this.#muteStateUpdate = true;
            this.#menuItemContext?.expansion.collapseItem({ ...eventEntry, menuItemAlias: this.manifest.alias });
            this.#muteStateUpdate = false;
        }
    }
    render() {
        return this.manifest
            ? html `
					<umb-tree
						alias=${this.manifest?.meta.treeAlias}
						.props=${{
                hideTreeRoot: this.manifest?.meta.hideTreeRoot === true,
                selectionConfiguration: {
                    selectable: false,
                    multiple: false,
                },
                expansion: this._menuItemExpansion,
                isMenu: true,
            }}
						@expansion-entry-expanded=${this.#onExpansionChange}
						@expansion-entry-collapsed=${this.#onExpansionChange}></umb-tree>
				`
            : nothing;
    }
};
__decorate([
    property({ type: Object })
], UmbMenuItemTreeDefaultElement.prototype, "manifest", void 0);
__decorate([
    state()
], UmbMenuItemTreeDefaultElement.prototype, "_menuItemExpansion", void 0);
UmbMenuItemTreeDefaultElement = __decorate([
    customElement('umb-menu-item-tree-default')
], UmbMenuItemTreeDefaultElement);
export { UmbMenuItemTreeDefaultElement };
export default UmbMenuItemTreeDefaultElement;
