import { mediaItemCache } from './media-item.server.cache.js';
import { UmbManagementApiItemDataCacheInvalidationManager, } from '../../../../management-api/index.js';
export class UmbManagementApiMediaItemDataCacheInvalidationManager extends UmbManagementApiItemDataCacheInvalidationManager {
    constructor(host) {
        super(host, {
            dataCache: mediaItemCache,
            /* The Media item model includes info about the Media Type.
            We need to invalidate the cache for both Media and MediaType events. */
            eventSources: ['Umbraco:CMS:Media', 'Umbraco:CMS:MediaType'],
            eventTypes: ['Updated', 'Deleted', 'Trashed'],
        });
    }
    _onServerEvent(event) {
        if (event.eventSource === 'Umbraco:CMS:MediaType') {
            this.#onMediaTypeChange(event);
        }
        else {
            this.#onMediaChange(event);
        }
    }
    #onMediaChange(event) {
        // Invalidate the specific media item
        const mediaId = event.key;
        this._dataCache.delete(mediaId);
    }
    #onMediaTypeChange(event) {
        // Invalidate all media items of the specified Media Type
        const mediaTypeId = event.key;
        const mediaIds = this._dataCache
            .getAll()
            .filter((cachedItem) => cachedItem.mediaType.id === mediaTypeId)
            .map((item) => item.id);
        mediaIds.forEach((id) => this._dataCache.delete(id));
    }
}
