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 { css, customElement, html, state, when } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import '../cascading-menu-popover/cascading-menu-popover.element.js';
let UmbTiptapToolbarMenuElement = class UmbTiptapToolbarMenuElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        this.#menu = [];
        this.isActive = false;
        this.#onEditorUpdate = () => {
            if (this.api && this.editor && this.manifest) {
                this.isActive = this.api.isActive(this.editor) || this.#isMenuActive(this.#menu) || false;
            }
        };
    }
    #menu;
    set manifest(value) {
        this.#manifest = value;
        this.#setMenu();
    }
    get manifest() {
        return this.#manifest;
    }
    #manifest;
    connectedCallback() {
        super.connectedCallback();
        if (this.editor) {
            this.editor.on('selectionUpdate', this.#onEditorUpdate);
            this.editor.on('update', this.#onEditorUpdate);
        }
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        if (this.editor) {
            this.editor.off('selectionUpdate', this.#onEditorUpdate);
            this.editor.off('update', this.#onEditorUpdate);
        }
    }
    async #setMenu() {
        const items = this.#manifest?.items;
        if (!items)
            return;
        this.#menu = await this.#getMenuItems(items);
    }
    async #getMenuItems(items) {
        const menu = [];
        for (const item of items) {
            const menuItem = await this.#getMenuItem(item);
            menu.push(menuItem);
        }
        return menu;
    }
    async #getMenuItem(item) {
        let element;
        // TODO: Commented out as needs review of how async/await is being handled here. [LK]
        // if (item.element) {
        // 	const elementConstructor = await loadManifestElement(item.element);
        // 	if (elementConstructor) {
        // 		element = new elementConstructor();
        // 	}
        // }
        if (!element && item.elementName) {
            element = document.createElement(item.elementName);
        }
        if (element) {
            // TODO: Enforce a type for the element, that has an `editor` property. [LK]
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            element.editor = this.editor;
        }
        let items;
        if (item.items) {
            items = await this.#getMenuItems(item.items);
        }
        return {
            icon: item.appearance?.icon,
            items,
            label: item.label,
            menu: item.menu,
            style: item.appearance?.style,
            separatorAfter: item.separatorAfter,
            element,
            isActive: () => this.api?.isActive(this.editor, item),
            execute: () => this.api?.execute(this.editor, item),
        };
    }
    #isMenuActive(items) {
        return !!items?.some((item) => item.isActive?.() || this.#isMenuActive(item.items));
    }
    #onEditorUpdate;
    render() {
        const label = this.localize.string(this.manifest?.meta.label);
        const disabled = this.api?.isDisabled(this.editor);
        return html `
			${when(this.manifest?.meta.look === 'icon', () => html `
					<uui-button
						compact
						label=${label}
						look=${this.isActive ? 'outline' : 'default'}
						title=${label}
						popovertarget="popover-menu"
						?disabled=${disabled}>
						${when(this.manifest?.meta.icon, (icon) => html `<umb-icon name=${icon}></umb-icon>`, () => html `<span>${label}</span>`)}
						<uui-symbol-expand slot="extra" open></uui-symbol-expand>
					</uui-button>
				`, () => html `
					<uui-button
						compact
						label=${label}
						look=${this.isActive ? 'outline' : 'default'}
						popovertarget="popover-menu"
						?disabled=${disabled}>
						<span>${label}</span>
						<uui-symbol-expand slot="extra" open></uui-symbol-expand>
					</uui-button>
				`)}
			${this.renderMenu()}
		`;
    }
    renderMenu() {
        return html `
			<umb-cascading-menu-popover id="popover-menu" placement="bottom-start" .items=${this.#menu}>
				${when(this.#manifest?.menu, (menuAlias) => html `
						<umb-extension-slot
							type="menu"
							default-element="umb-tiptap-menu"
							single
							.filter=${(menu) => menu.alias === menuAlias}></umb-extension-slot>
					`)}
			</umb-cascading-menu-popover>
		`;
    }
    static { this.styles = [
        css `
			:host {
				--uui-button-font-weight: normal;
				--uui-menu-item-flat-structure: 1;

				margin-left: var(--uui-size-space-1);
				margin-bottom: var(--uui-size-space-1);
			}

			uui-button > uui-symbol-expand {
				margin-left: var(--uui-size-space-2);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbTiptapToolbarMenuElement.prototype, "isActive", void 0);
UmbTiptapToolbarMenuElement = __decorate([
    customElement('umb-tiptap-toolbar-menu')
], UmbTiptapToolbarMenuElement);
export { UmbTiptapToolbarMenuElement };
export { UmbTiptapToolbarMenuElement as element };
