import { UMB_MEDIA_PLACEHOLDER_ENTITY_TYPE } from '../entity.js';
import { UMB_EDIT_MEDIA_WORKSPACE_PATH_PATTERN } from '../paths.js';
import { UMB_MEDIA_GRID_COLLECTION_VIEW_ALIAS } from './views/constants.js';
import { UmbDefaultCollectionContext } from '../../../core/collection/index.js';
import { UmbArrayState } from '../../../../libs/observable-api/index.js';
import { UmbDeprecation } from '../../../core/utils/index.js';
export class UmbMediaCollectionContext extends UmbDefaultCollectionContext {
    #placeholders;
    constructor(host) {
        super(host, UMB_MEDIA_GRID_COLLECTION_VIEW_ALIAS);
        this.#placeholders = new UmbArrayState([], (x) => x.unique);
        this.placeholders = this.#placeholders.asObservable();
    }
    setPlaceholders(partial) {
        const items = this._items.getValue();
        // We do not want to set a placeholder which unique already exists in the collection.
        const date = new Date();
        const placeholders = partial
            .filter((placeholder) => !items.find((item) => item.unique === placeholder.unique))
            .map((placeholder) => ({
            updateDate: date,
            createDate: date,
            entityType: UMB_MEDIA_PLACEHOLDER_ENTITY_TYPE,
            flags: [],
            ...placeholder,
        }))
            .reverse();
        this.#placeholders.setValue(placeholders);
        this._items.setValue([...placeholders, ...items]);
        this._totalItems.setValue(placeholders.length + items.length);
        this.pagination.setTotalItems(placeholders.length + items.length);
    }
    updatePlaceholderStatus(unique, status) {
        this._items.updateOne(unique, { status });
        this.#placeholders.updateOne(unique, { status });
    }
    updatePlaceholderProgress(unique, progress) {
        this._items.updateOne(unique, { progress });
        this.#placeholders.updateOne(unique, { progress });
    }
    /**
     * Requests the collection from the repository.
     * @returns {Promise<void>}
     * @deprecated Deprecated since v.17.0.0. Use `loadCollection` instead.
     * @memberof UmbMediaCollectionContext
     */
    async requestCollection() {
        new UmbDeprecation({
            removeInVersion: '19.0.0',
            deprecated: 'requestCollection',
            solution: 'Use .loadCollection method instead',
        }).warn();
        return this._requestCollection();
    }
    async _requestCollection() {
        await this._init;
        if (!this._configured)
            this._configure();
        if (!this._repository)
            throw new Error(`Missing repository for ${this._manifest}`);
        this._loading.setValue(true);
        const filter = this._filter.getValue();
        const { data } = await this._repository.requestCollection(filter);
        if (data) {
            this.#cleanupPlaceholdersFromCollection(data.items);
            const placeholders = this.#placeholders.getValue();
            this._items.setValue([...placeholders, ...data.items]);
            this._totalItems.setValue(placeholders.length + data.total);
            this.pagination.setTotalItems(placeholders.length + data.total);
        }
        this._loading.setValue(false);
    }
    #cleanupPlaceholdersFromCollection(collection) {
        const placeholderItems = this.#placeholders.getValue();
        const dataSet = new Set(collection.map((item) => item.unique));
        const completedPlaceholders = placeholderItems.filter((item) => dataSet.has(item.unique));
        completedPlaceholders.forEach((placeholder) => {
            this.#placeholders.removeOne(placeholder.unique);
        });
    }
    /**
     * Returns the href for a specific media collection item.
     * @param {UmbMediaCollectionItemModel} item - The media item to get the href for.
     * @returns {Promise<string | undefined>} - The edit workspace href for the media.
     */
    async requestItemHref(item) {
        return `${UMB_EDIT_MEDIA_WORKSPACE_PATH_PATTERN.generateAbsolute({ unique: item.unique })}`;
    }
}
export { UmbMediaCollectionContext as api };
