import { UmbCollectionActionBase } from '../../../collection/action/collection-action-base.js';
import { createExtensionApiByAlias } from '../../../extension-registry/index.js';
import { umbConfirmModal } from '../../../modal/index.js';
import { UmbRequestReloadChildrenOfEntityEvent } from '../../../entity-action/index.js';
import { UMB_ACTION_EVENT_CONTEXT } from '../../../action/index.js';
import { UMB_COLLECTION_CONTEXT } from '../../../collection/index.js';
import { UMB_ENTITY_CONTEXT } from '../../../entity/index.js';
/**
 * Collection action for emptying the recycle bin.
 * @class UmbEmptyRecycleBinCollectionAction
 * @augments {UmbCollectionActionBase}
 */
export class UmbEmptyRecycleBinCollectionAction extends UmbCollectionActionBase {
    #manifest;
    constructor(host, manifest) {
        super(host);
        this.#manifest = manifest;
    }
    /**
     * Executes the action.
     * @memberof UmbEmptyRecycleBinCollectionAction
     */
    async execute() {
        if (!this.#manifest.meta.recycleBinRepositoryAlias) {
            throw new Error('Recycle Bin Repository Alias is not defined in the manifest meta');
        }
        await umbConfirmModal(this._host, {
            headline: `#actions_emptyrecyclebin`,
            content: `#defaultdialogs_recycleBinWarning`,
            color: 'danger',
            confirmLabel: `#actions_emptyrecyclebin`,
        });
        const recycleBinRepository = await createExtensionApiByAlias(this, this.#manifest.meta.recycleBinRepositoryAlias);
        const { error } = await recycleBinRepository.requestEmpty();
        if (error) {
            throw error;
        }
        // Refresh the collection
        const collectionContext = await this.getContext(UMB_COLLECTION_CONTEXT);
        collectionContext?.loadCollection();
        // Refresh the tree (if any)
        await this.#reloadChildrenOfEntity();
    }
    /**
     * Requests a reload of the children of the current entity in the tree.
     * Silently returns if the required contexts are not available.
     */
    async #reloadChildrenOfEntity() {
        const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
        if (!eventContext)
            return;
        const entityContext = await this.getContext(UMB_ENTITY_CONTEXT);
        if (!entityContext)
            return;
        const entityType = entityContext.getEntityType();
        if (!entityType)
            return;
        const unique = entityContext.getUnique();
        const event = new UmbRequestReloadChildrenOfEntityEvent({ entityType, unique });
        eventContext.dispatchEvent(event);
    }
}
export { UmbEmptyRecycleBinCollectionAction as api };
