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_WORKSPACE_CONTEXT } from '../../workspace/constants.js';
import { UMB_CREATE_DOCUMENT_WORKSPACE_PATH_PATTERN } from '../../paths.js';
import { UMB_DOCUMENT_ENTITY_TYPE, UMB_DOCUMENT_ROOT_ENTITY_TYPE } from '../../entity.js';
import { css, customElement, html, map, property, state } from '../../../../../external/lit/index.js';
import { UmbDocumentTypeStructureRepository } from '../../../document-types/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
let UmbCreateDocumentCollectionActionElement = class UmbCreateDocumentCollectionActionElement extends UmbLitElement {
    #documentTypeStructureRepository;
    constructor() {
        super();
        this._allowedDocumentTypes = [];
        this._popoverOpen = false;
        this.#documentTypeStructureRepository = new UmbDocumentTypeStructureRepository(this);
        this.consumeContext(UMB_DOCUMENT_WORKSPACE_CONTEXT, (workspaceContext) => {
            this.observe(workspaceContext?.unique, (unique) => {
                this._documentUnique = unique;
            });
            this.observe(workspaceContext?.contentTypeUnique, (documentTypeUnique) => {
                this._documentTypeUnique = documentTypeUnique;
            });
        });
    }
    async firstUpdated() {
        if (this._documentTypeUnique) {
            this.#retrieveAllowedDocumentTypesOf(this._documentTypeUnique, this._documentUnique || null);
        }
    }
    async #retrieveAllowedDocumentTypesOf(unique, parentContentUnique) {
        const { data } = await this.#documentTypeStructureRepository.requestAllowedChildrenOf(unique, parentContentUnique);
        if (data && data.items) {
            this._allowedDocumentTypes = data.items;
        }
    }
    #onPopoverToggle(event) {
        // TODO: This ignorer is just needed for JSON SCHEMA TO WORK, As its not updated with latest TS jet.
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        this._popoverOpen = event.newState === 'open';
    }
    #getCreateUrl(item) {
        if (!item.unique) {
            throw new Error('Item unique is missing');
        }
        return UMB_CREATE_DOCUMENT_WORKSPACE_PATH_PATTERN.generateAbsolute({
            parentEntityType: this._documentUnique ? UMB_DOCUMENT_ENTITY_TYPE : UMB_DOCUMENT_ROOT_ENTITY_TYPE,
            parentUnique: this._documentUnique ?? 'null',
            documentTypeUnique: item.unique,
        });
    }
    render() {
        return this._allowedDocumentTypes.length !== 1 ? this.#renderDropdown() : this.#renderCreateButton();
    }
    #renderCreateButton() {
        if (this._allowedDocumentTypes.length !== 1)
            return;
        const item = this._allowedDocumentTypes[0];
        // TODO: Stop appending values to labels, instead we need to parse the name as a argument to the label. [NL]
        const label = (this.manifest?.meta.label
            ? this.localize.string(this.manifest?.meta.label)
            : this.localize.term('general_create')) +
            ' ' +
            this.localize.string(item.name);
        return html `
			<uui-button color="default" href=${this.#getCreateUrl(item)} label=${label} look="outline"></uui-button>
		`;
    }
    #renderDropdown() {
        if (!this._allowedDocumentTypes.length)
            return;
        const label = this.manifest?.meta.label
            ? this.localize.string(this.manifest?.meta.label)
            : this.localize.term('general_create');
        return html `
			<uui-button popovertarget="collection-action-menu-popover" label=${label} color="default" look="outline">
				${label}
				<uui-symbol-expand .open=${this._popoverOpen}></uui-symbol-expand>
			</uui-button>
			<uui-popover-container
				id="collection-action-menu-popover"
				placement="bottom-start"
				@toggle=${this.#onPopoverToggle}>
				<umb-popover-layout>
					<uui-scroll-container>
						${map(this._allowedDocumentTypes, (item) => html `
								<uui-menu-item label=${this.localize.string(item.name)} href=${this.#getCreateUrl(item)}>
									<umb-icon slot="icon" name=${item.icon ?? 'icon-document'}></umb-icon>
								</uui-menu-item>
							`)}
					</uui-scroll-container>
				</umb-popover-layout>
			</uui-popover-container>
		`;
    }
    static { this.styles = [
        css `
			uui-scroll-container {
				max-height: 500px;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbCreateDocumentCollectionActionElement.prototype, "_allowedDocumentTypes", void 0);
__decorate([
    state()
], UmbCreateDocumentCollectionActionElement.prototype, "_documentUnique", void 0);
__decorate([
    state()
], UmbCreateDocumentCollectionActionElement.prototype, "_documentTypeUnique", void 0);
__decorate([
    state()
], UmbCreateDocumentCollectionActionElement.prototype, "_popoverOpen", void 0);
__decorate([
    property({ attribute: false })
], UmbCreateDocumentCollectionActionElement.prototype, "manifest", void 0);
UmbCreateDocumentCollectionActionElement = __decorate([
    customElement('umb-create-document-collection-action')
], UmbCreateDocumentCollectionActionElement);
export { UmbCreateDocumentCollectionActionElement };
export default UmbCreateDocumentCollectionActionElement;
