import { UMB_DATA_TYPE_ENTITY_TYPE } from '../../entity.js';
import { tryExecute } from '../../../core/resources/index.js';
import { DataTypeService } from '../../../core/backend-api/index.js';
import { umbExtensionsRegistry } from '../../../core/extension-registry/index.js';
/**
 * A data source that fetches the data-type collection data from the server.
 * @class UmbDataTypeCollectionServerDataSource
 * @implements {UmbCollectionDataSource}
 */
export class UmbDataTypeCollectionServerDataSource {
    #host;
    #manifestPropertyEditorUis = [];
    /**
     * Creates an instance of UmbDataTypeCollectionServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @DataTypeof UmbDataTypeCollectionServerDataSource
     */
    constructor(host) {
        this.#host = host;
        umbExtensionsRegistry
            .byType('propertyEditorUi')
            .subscribe((manifestPropertyEditorUIs) => {
            this.#manifestPropertyEditorUis = manifestPropertyEditorUIs;
        })
            .unsubscribe();
    }
    /**
     * Gets the DataType collection filtered by the given filter.
     * @param {UmbDataTypeCollectionFilterModel} query
     * @returns {*}
     * @DataTypeof UmbDataTypeCollectionServerDataSource
     */
    async getCollection(query) {
        const { data, error } = await tryExecute(this.#host, DataTypeService.getFilterDataType({ query }));
        if (error) {
            return { error };
        }
        if (!data) {
            return { data: { items: [], total: 0 } };
        }
        const { items, total } = data;
        const mappedItems = items.map((item) => {
            const dataTypeDetail = {
                entityType: UMB_DATA_TYPE_ENTITY_TYPE,
                unique: item.id,
                name: item.name,
                propertyEditorSchemaAlias: item.editorAlias,
                propertyEditorUiAlias: item.editorUiAlias,
                icon: this.#manifestPropertyEditorUis.find((ui) => ui.alias === item.editorUiAlias)?.meta.icon,
            };
            return dataTypeDetail;
        });
        return { data: { items: mappedItems, total } };
    }
}
