import { UMB_MEDIA_ENTITY_TYPE, UMB_MEDIA_ROOT_ENTITY_TYPE } from '../../entity.js';
import { MediaService } from '../../../../core/backend-api/index.js';
import { UmbTreeServerDataSourceBase } from '../../../../core/tree/index.js';
/**
 * A data source for the Media Recycle Bin tree that fetches data from the server
 * @class UmbMediaRecycleBinTreeServerDataSource
 * @implements {UmbTreeDataSource}
 */
export class UmbMediaRecycleBinTreeServerDataSource extends UmbTreeServerDataSourceBase {
    /**
     * Creates an instance of UmbMediaRecycleBinTreeServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbMediaRecycleBinTreeServerDataSource
     */
    constructor(host) {
        super(host, {
            getRootItems,
            getChildrenOf,
            getAncestorsOf,
            mapper,
        });
    }
}
const getRootItems = (args) => 
// eslint-disable-next-line local-rules/no-direct-api-import
MediaService.getRecycleBinMediaRoot({ query: { skip: args.skip, take: args.take } });
const getChildrenOf = (args) => {
    if (args.parent.unique === null) {
        return getRootItems(args);
    }
    else {
        // eslint-disable-next-line local-rules/no-direct-api-import
        return MediaService.getRecycleBinMediaChildren({
            query: { parentId: args.parent.unique, skip: args.skip, take: args.take },
        });
    }
};
const getAncestorsOf = (args) => 
// eslint-disable-next-line local-rules/no-direct-api-import
MediaService.getTreeMediaAncestors({
    query: { descendantId: args.treeItem.unique },
});
const mapper = (item) => {
    return {
        unique: item.id,
        parent: {
            unique: item.parent ? item.parent.id : null,
            entityType: item.parent ? UMB_MEDIA_ENTITY_TYPE : UMB_MEDIA_ROOT_ENTITY_TYPE,
        },
        entityType: UMB_MEDIA_ENTITY_TYPE,
        noAccess: false,
        isTrashed: true,
        hasChildren: item.hasChildren,
        mediaType: {
            unique: item.mediaType.id,
            icon: item.mediaType.icon,
            collection: item.mediaType.collection ? { unique: item.mediaType.collection.id } : null,
        },
        variants: item.variants.map((variant) => {
            return {
                name: variant.name,
                culture: variant.culture || null,
                segment: null, // TODO: add segment to the backend API?
            };
        }),
        name: item.variants[0]?.name, // TODO: this is not correct. We need to get it from the variants. This is a temp solution.
        isFolder: false,
        createDate: item.createDate,
    };
};
