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 { UmbUfmElementBase } from '../ufm-element-base.js';
import { UMB_UFM_RENDER_CONTEXT } from '../ufm-render/ufm-render.context.js';
import { customElement, property } from '../../../../external/lit/index.js';
import { UmbDocumentItemRepository, UMB_DOCUMENT_ENTITY_TYPE } from '../../../documents/documents/index.js';
import { UmbMediaItemRepository, UMB_MEDIA_ENTITY_TYPE } from '../../../media/media/index.js';
let UmbUfmLinkElement = class UmbUfmLinkElement extends UmbUfmElementBase {
    #documentRepository;
    #mediaRepository;
    constructor() {
        super();
        this.consumeContext(UMB_UFM_RENDER_CONTEXT, (context) => {
            this.observe(context?.value, async (value) => {
                const temp = this.alias && typeof value === 'object'
                    ? value[this.alias]
                    : value;
                if (!temp)
                    return;
                const items = Array.isArray(temp) ? temp : [temp];
                const names = await Promise.all(items.map(async (item) => await this.#getName(item)));
                this.value = names.filter((x) => x).join(', ');
            }, 'observeValue');
        });
    }
    async #getName(item) {
        const link = item;
        if (link.name) {
            return link.name;
        }
        const entityType = link.type;
        const unique = link.unique;
        if (unique) {
            const repository = this.#getRepository(entityType);
            if (repository) {
                const { data } = await repository.requestItems([unique]);
                if (Array.isArray(data) && data.length > 0) {
                    // TODO: [v17] Review usage of `item.variants[0].name` as this needs to be implemented properly! [LK]
                    return data.map((item) => item.variants[0].name).join(', ');
                }
            }
        }
        return '';
    }
    #getRepository(entityType) {
        switch (entityType) {
            case UMB_MEDIA_ENTITY_TYPE:
                if (!this.#mediaRepository)
                    this.#mediaRepository = new UmbMediaItemRepository(this);
                return this.#mediaRepository;
            case UMB_DOCUMENT_ENTITY_TYPE:
            default:
                if (!this.#documentRepository)
                    this.#documentRepository = new UmbDocumentItemRepository(this);
                return this.#documentRepository;
        }
    }
};
__decorate([
    property()
], UmbUfmLinkElement.prototype, "alias", void 0);
UmbUfmLinkElement = __decorate([
    customElement('ufm-link')
], UmbUfmLinkElement);
export { UmbUfmLinkElement };
export { UmbUfmLinkElement as element };
