import { UMB_DATA_TYPE_ENTITY_TYPE } from '../../entity.js';
import { UmbManagementApiDataTypeItemDataRequestManager } from './data-type-item.server.request-manager.js';
import { UmbItemServerDataSourceBase } from '../../../core/repository/index.js';
import { umbExtensionsRegistry } from '../../../core/extension-registry/index.js';
let manifestPropertyEditorUis = [];
/**
 * A server data source for Data Type items
 * @class UmbDataTypeItemServerDataSource
 * @implements {DocumentTreeDataSource}
 */
export class UmbDataTypeItemServerDataSource extends UmbItemServerDataSourceBase {
    #itemRequestManager = new UmbManagementApiDataTypeItemDataRequestManager(this);
    /**
     * Creates an instance of UmbDataTypeItemServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbDataTypeItemServerDataSource
     */
    constructor(host) {
        super(host, {
            mapper,
        });
        umbExtensionsRegistry
            .byType('propertyEditorUi')
            .subscribe((manifestPropertyEditorUIs) => {
            manifestPropertyEditorUis = manifestPropertyEditorUIs;
        })
            .unsubscribe();
    }
    async getItems(uniques) {
        if (!uniques)
            throw new Error('Uniques are missing');
        const { data, error } = await this.#itemRequestManager.getItems(uniques);
        return { data: this._getMappedItems(data), error };
    }
}
const mapper = (item) => {
    return {
        entityType: UMB_DATA_TYPE_ENTITY_TYPE,
        unique: item.id,
        name: item.name,
        propertyEditorSchemaAlias: item.editorAlias,
        propertyEditorUiAlias: item.editorUiAlias || '', // TODO: why can this be undefined or null on the server?
        icon: manifestPropertyEditorUis.find((ui) => ui.alias === item.editorUiAlias)?.meta.icon,
        isDeletable: item.isDeletable,
    };
};
