import { UMB_MEDIA_ENTITY_TYPE } from '../../entity.js';
import { MediaService } from '../../../../core/backend-api/index.js';
import { UmbManagementApiDataMapper } from '../../../../core/repository/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
import { UmbControllerBase } from '../../../../../libs/class-api/index.js';
/**
 * @class UmbMediaReferenceServerDataSource
 * @implements {UmbEntityReferenceDataSource}
 */
export class UmbMediaReferenceServerDataSource extends UmbControllerBase {
    #dataMapper = new UmbManagementApiDataMapper(this);
    /**
     * Fetches the item for the given unique from the server
     * @param {string} unique - The unique identifier of the item to fetch
     * @param {number} skip - The number of items to skip
     * @param {number} take - The number of items to take
     * @returns {Promise<UmbDataSourceResponse<UmbPagedModel<UmbReferenceItemModel>>>} - Items that are referenced by the given unique
     * @memberof UmbMediaReferenceServerDataSource
     */
    async getReferencedBy(unique, skip = 0, take = 20) {
        const { data, error } = await tryExecute(this, MediaService.getMediaByIdReferencedBy({ path: { id: unique }, query: { skip, take } }));
        if (data) {
            const promises = data.items.map(async (item) => {
                return this.#dataMapper.map({
                    forDataModel: item.$type,
                    data: item,
                    fallback: async () => {
                        return {
                            ...item,
                            unique: item.id,
                            entityType: 'unknown',
                        };
                    },
                });
            });
            const items = await Promise.all(promises);
            return { data: { items, total: data.total } };
        }
        return { data, error };
    }
    /**
     * Checks if the items are referenced by other items
     * @param {Array<string>} uniques - The unique identifiers of the items to fetch
     * @param {number} skip - The number of items to skip
     * @param {number} take - The number of items to take
     * @returns {Promise<UmbDataSourceResponse<UmbPagedModel<UmbEntityModel>>>} - Items that are referenced by other items
     * @memberof UmbMediaReferenceServerDataSource
     */
    async getAreReferenced(uniques, skip = 0, take = 20) {
        const { data, error } = await tryExecute(this, MediaService.getMediaAreReferenced({ query: { id: uniques, skip, take } }));
        if (data) {
            const items = data.items.map((item) => {
                return {
                    unique: item.id,
                    entityType: UMB_MEDIA_ENTITY_TYPE,
                };
            });
            return { data: { items, total: data.total } };
        }
        return { data, error };
    }
    /**
     * Returns any descendants of the given unique that is referenced by other items
     * @param {string} unique - The unique identifier of the item to fetch descendants for
     * @param {number} skip - The number of items to skip
     * @param {number} take - The number of items to take
     * @returns {Promise<UmbDataSourceResponse<UmbPagedModel<UmbEntityModel>>>} - Any descendants of the given unique that is referenced by other items
     * @memberof UmbMediaReferenceServerDataSource
     */
    async getReferencedDescendants(unique, skip = 0, take = 20) {
        const { data, error } = await tryExecute(this, MediaService.getMediaByIdReferencedDescendants({ path: { id: unique }, query: { skip, take } }));
        if (data) {
            const items = data.items.map((item) => {
                return {
                    unique: item.id,
                    entityType: UMB_MEDIA_ENTITY_TYPE,
                };
            });
            return { data: { items, total: data.total } };
        }
        return { data, error };
    }
}
