import { UMB_ACTION_EVENT_CONTEXT } from '../../../../action/index.js';
import { UmbEntityActionBase, UmbRequestReloadStructureForEntityEvent } from '../../../../entity-action/index.js';
import { createExtensionApiByAlias } from '../../../../extension-registry/index.js';
import { umbConfirmModal } from '../../../../modal/index.js';
export class UmbDeleteFolderEntityAction extends UmbEntityActionBase {
    async execute() {
        if (!this.args.unique)
            throw new Error('Unique is not available');
        const folderRepository = await createExtensionApiByAlias(this, this.args.meta.folderRepositoryAlias);
        const { data: folder } = await folderRepository.requestByUnique(this.args.unique);
        if (folder) {
            // TODO: maybe we can show something about how many items are part of the folder?
            await umbConfirmModal(this, {
                headline: `Delete ${folder.name}`,
                content: 'Are you sure you want to delete this folder?',
                color: 'danger',
                confirmLabel: 'Delete',
            });
            const { error } = await folderRepository.delete(this.args.unique);
            if (error) {
                throw error;
            }
            const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
            if (!actionEventContext)
                throw new Error('Action event context is missing');
            const event = new UmbRequestReloadStructureForEntityEvent({
                unique: this.args.unique,
                entityType: this.args.entityType,
            });
            actionEventContext.dispatchEvent(event);
        }
    }
}
