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_TYPE_ITEM_REPOSITORY_ALIAS, } from '../../../../documents/document-types/index.js';
import { html, customElement, property, state, ifDefined } from '../../../../../external/lit/index.js';
import { UmbRepositoryItemsManager } from '../../../../core/repository/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
import { transformServerPathToClientPath } from '../../../../core/utils/index.js';
import { UUICardEvent } from '../../../../../external/uui/index.js';
import { UMB_SERVER_CONTEXT } from '../../../../core/server/index.js';
let UmbBlockTypeCardElement = class UmbBlockTypeCardElement extends UmbLitElement {
    //
    #init;
    #serverUrl;
    #itemManager;
    set iconFile(value) {
        value = transformServerPathToClientPath(value);
        if (value) {
            this.#init.then(() => {
                const url = new URL(value, this.#serverUrl);
                this._iconFile = url.href;
            });
        }
        else {
            this._iconFile = undefined;
        }
    }
    get iconFile() {
        return this._iconFile;
    }
    // TODO: support custom icon/image file
    set contentElementTypeKey(value) {
        this._elementTypeKey = value;
        if (value) {
            this.#itemManager.setUniques([value]);
        }
        else {
            this.#itemManager.setUniques([]);
        }
    }
    get contentElementTypeKey() {
        return this._elementTypeKey;
    }
    constructor() {
        super();
        this.#serverUrl = '';
        this.#itemManager = new UmbRepositoryItemsManager(this, UMB_DOCUMENT_TYPE_ITEM_REPOSITORY_ALIAS);
        this._name = '';
        this.#onOpen = () => {
            this.dispatchEvent(new UUICardEvent(UUICardEvent.OPEN));
        };
        this.#init = this.consumeContext(UMB_SERVER_CONTEXT, (instance) => {
            this.#serverUrl = instance?.getServerUrl() ?? '';
        }).asPromise({ preventTimeout: true });
        this.observe(this.#itemManager.statuses, async (statuses) => {
            const status = statuses[0];
            if (status?.state.type === 'success') {
                const item = await this.#itemManager.getItemByUnique(status.unique);
                this._fallbackIcon = item.icon;
                this._name = item.name ? this.localize.string(item.name) : this.localize.term('general_unknown');
                this._description = this.localize.string(item.description);
            }
            else if (status?.state.type === 'error') {
                this._fallbackIcon = 'icon-alert';
                this._name = this.localize.term('blockEditor_elementTypeDoesNotExistHeadline');
                this._description = this.localize.term('blockEditor_elementTypeDoesNotExistDescription');
            }
        });
    }
    #onOpen;
    // TODO: Support image files instead of icons.
    render() {
        return html `
			<uui-card-block-type
				href=${ifDefined(this.href)}
				@open=${this.#onOpen}
				.name=${this._name}
				.description=${this._description}
				.background=${this.backgroundColor}>
				${this._iconFile
            ? html `<img src=${this._iconFile} alt="" />`
            : html `<umb-icon name=${this._fallbackIcon ?? ''} color=${ifDefined(this.iconColor)}></umb-icon>`}
				<slot name="actions" slot="actions"> </slot>
			</uui-card-block-type>
		`;
    }
};
__decorate([
    property({ type: String })
], UmbBlockTypeCardElement.prototype, "href", void 0);
__decorate([
    property({ type: String, attribute: false })
], UmbBlockTypeCardElement.prototype, "iconFile", null);
__decorate([
    state()
], UmbBlockTypeCardElement.prototype, "_iconFile", void 0);
__decorate([
    property({ type: String, attribute: false })
], UmbBlockTypeCardElement.prototype, "iconColor", void 0);
__decorate([
    property({ type: String, attribute: false })
], UmbBlockTypeCardElement.prototype, "backgroundColor", void 0);
__decorate([
    property({ type: String, attribute: false })
], UmbBlockTypeCardElement.prototype, "contentElementTypeKey", null);
__decorate([
    state()
], UmbBlockTypeCardElement.prototype, "_name", void 0);
__decorate([
    state()
], UmbBlockTypeCardElement.prototype, "_description", void 0);
__decorate([
    state()
], UmbBlockTypeCardElement.prototype, "_fallbackIcon", void 0);
UmbBlockTypeCardElement = __decorate([
    customElement('umb-block-type-card')
], UmbBlockTypeCardElement);
export { UmbBlockTypeCardElement };
export default UmbBlockTypeCardElement;
