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_MEDIA_WORKSPACE_CONTEXT } from '../../constants.js';
import { UMB_CREATE_MEDIA_WORKSPACE_PATH_PATTERN } from '../../paths.js';
import { UMB_MEDIA_ENTITY_TYPE, UMB_MEDIA_ROOT_ENTITY_TYPE } from '../../entity.js';
import { html, customElement, property, state, map } from '../../../../../external/lit/index.js';
import { UmbMediaTypeStructureRepository } from '../../../media-types/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
let UmbCreateMediaCollectionActionElement = class UmbCreateMediaCollectionActionElement extends UmbLitElement {
    #mediaTypeStructureRepository;
    constructor() {
        super();
        this._allowedMediaTypes = [];
        this._popoverOpen = false;
        this.#mediaTypeStructureRepository = new UmbMediaTypeStructureRepository(this);
        this.consumeContext(UMB_MEDIA_WORKSPACE_CONTEXT, (workspaceContext) => {
            this.observe(workspaceContext?.unique, (unique) => {
                this._mediaUnique = unique;
            });
            this.observe(workspaceContext?.contentTypeUnique, (mediaTypeUnique) => {
                this._mediaTypeUnique = mediaTypeUnique;
            });
        });
    }
    async firstUpdated() {
        this.#retrieveAllowedMediaTypesOf(this._mediaTypeUnique ?? '', this._mediaUnique || null);
    }
    async #retrieveAllowedMediaTypesOf(unique, parentContentUnique) {
        const { data } = await this.#mediaTypeStructureRepository.requestAllowedChildrenOf(unique, parentContentUnique);
        if (data && data.items) {
            this._allowedMediaTypes = data.items;
        }
    }
    #onPopoverToggle(event) {
        // TODO: This ignorer is just neede 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 does not have a unique identifier');
        }
        return UMB_CREATE_MEDIA_WORKSPACE_PATH_PATTERN.generateAbsolute({
            parentEntityType: this._mediaUnique ? UMB_MEDIA_ENTITY_TYPE : UMB_MEDIA_ROOT_ENTITY_TYPE,
            parentUnique: this._mediaUnique ?? 'null',
            mediaTypeUnique: item.unique,
        });
    }
    render() {
        return this._allowedMediaTypes.length !== 1 ? this.#renderDropdown() : this.#renderCreateButton();
    }
    #renderCreateButton() {
        if (this._allowedMediaTypes.length !== 1)
            return;
        const item = this._allowedMediaTypes[0];
        const label = (this.manifest?.meta.label
            ? this.localize.string(this.manifest?.meta.label)
            : this.localize.term('general_create')) +
            ' ' +
            item.name;
        return html `<uui-button
			color="default"
			href=${this.#getCreateUrl(item)}
			label=${label}
			look="outline"></uui-button>`;
    }
    #renderDropdown() {
        if (!this._allowedMediaTypes.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._allowedMediaTypes, (item) => html `
								<uui-menu-item label=${item.name} href=${this.#getCreateUrl(item)}>
									<umb-icon slot="icon" name=${item.icon ?? 'icon-picture'}></umb-icon>
								</uui-menu-item>
							`)}
					</uui-scroll-container>
				</umb-popover-layout>
			</uui-popover-container>
		`;
    }
};
__decorate([
    state()
], UmbCreateMediaCollectionActionElement.prototype, "_allowedMediaTypes", void 0);
__decorate([
    state()
], UmbCreateMediaCollectionActionElement.prototype, "_mediaUnique", void 0);
__decorate([
    state()
], UmbCreateMediaCollectionActionElement.prototype, "_mediaTypeUnique", void 0);
__decorate([
    state()
], UmbCreateMediaCollectionActionElement.prototype, "_popoverOpen", void 0);
__decorate([
    property({ attribute: false })
], UmbCreateMediaCollectionActionElement.prototype, "manifest", void 0);
UmbCreateMediaCollectionActionElement = __decorate([
    customElement('umb-create-media-collection-action')
], UmbCreateMediaCollectionActionElement);
export { UmbCreateMediaCollectionActionElement };
export default UmbCreateMediaCollectionActionElement;
