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 { UmbDocumentItemDataResolver, UmbDocumentItemRepository, UMB_DOCUMENT_ENTITY_TYPE, } from '../../../documents/documents/index.js';
import { UmbId } from '../../../core/id/index.js';
import { UmbMediaItemRepository, UMB_MEDIA_ENTITY_TYPE } from '../../../media/media/index.js';
import { UmbMemberItemRepository, UMB_MEMBER_ENTITY_TYPE } from '../../../members/member/index.js';
let UmbUfmContentNameElement = class UmbUfmContentNameElement extends UmbUfmElementBase {
    #documentRepository;
    #mediaRepository;
    #memberRepository;
    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 entityType = this.#getEntityType(temp);
                const uniques = this.#getUniques(temp);
                this.value = await this.#getNames(entityType, uniques);
            }, 'observeValue');
        });
    }
    #getEntityType(value) {
        if (Array.isArray(value) && value.length > 0) {
            const item = value[0];
            if (item.type)
                return item.type;
            if (item.mediaKey)
                return UMB_MEDIA_ENTITY_TYPE;
        }
        return UMB_DOCUMENT_ENTITY_TYPE;
    }
    #getUniques(value) {
        if (Array.isArray(value)) {
            return value.map((x) => x.unique ?? x.mediaKey ?? x).filter((x) => UmbId.validate(x));
        }
        return typeof value === 'string' && UmbId.validate(value) ? [value] : [];
    }
    async #getNames(entityType, uniques) {
        if (uniques?.length) {
            const repository = this.#getRepository(entityType);
            if (repository) {
                const { data } = await repository.requestItems(uniques);
                if (Array.isArray(data) && data.length > 0) {
                    if (entityType === UMB_DOCUMENT_ENTITY_TYPE) {
                        const namePromises = data.map(async (item) => {
                            const resolver = new UmbDocumentItemDataResolver(this);
                            resolver.setData(item);
                            return await resolver.getName();
                        });
                        const names = await Promise.all(namePromises);
                        return names.join(', ');
                    }
                    // TODO: Review usage of `item.variants[0].name` as this needs to be implemented properly for media/member items [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_MEMBER_ENTITY_TYPE:
                if (!this.#memberRepository)
                    this.#memberRepository = new UmbMemberItemRepository(this);
                return this.#memberRepository;
            case UMB_DOCUMENT_ENTITY_TYPE:
            default:
                if (!this.#documentRepository)
                    this.#documentRepository = new UmbDocumentItemRepository(this);
                return this.#documentRepository;
        }
    }
};
__decorate([
    property()
], UmbUfmContentNameElement.prototype, "alias", void 0);
UmbUfmContentNameElement = __decorate([
    customElement('ufm-content-name')
], UmbUfmContentNameElement);
export { UmbUfmContentNameElement };
export { UmbUfmContentNameElement as element };
