import { UMB_MEDIA_ENTITY_TYPE } from '../../entity.js';
import { UmbManagementApiMediaItemDataRequestManager } from './media-item.server.request-manager.js';
import { UmbItemServerDataSourceBase } from '../../../../core/repository/index.js';
/**
 * A data source for Media items that fetches data from the server
 * @class UmbMediaItemServerDataSource
 * @implements {MediaTreeDataSource}
 */
export class UmbMediaItemServerDataSource extends UmbItemServerDataSourceBase {
    #itemRequestManager = new UmbManagementApiMediaItemDataRequestManager(this);
    /**
     * Creates an instance of UmbMediaItemServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbMediaItemServerDataSource
     */
    constructor(host) {
        super(host, {
            mapper,
        });
    }
    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_MEDIA_ENTITY_TYPE,
        hasChildren: item.hasChildren,
        isTrashed: item.isTrashed,
        unique: item.id,
        mediaType: {
            unique: item.mediaType.id,
            icon: item.mediaType.icon,
            collection: item.mediaType.collection ? { unique: item.mediaType.collection.id } : null,
        },
        name: item.variants[0]?.name, // TODO: get correct variant name
        parent: item.parent ? { unique: item.parent.id } : null,
        variants: item.variants.map((variant) => {
            return {
                culture: variant.culture || null,
                name: variant.name,
            };
        }),
        flags: item.flags,
    };
};
