import { loadManifest, restoreFromTrash, purgeTrashItem, purgeAllTrash, } from '../../storage/trash-store.js';
import { appendAuditEntry } from './audit-logger.js';
/**
 * List all items in trash for a project.
 */
export async function listTrash(projectId) {
    return loadManifest(projectId);
}
/**
 * Restore a specific item from trash.
 */
export async function restoreItem(projectId, itemId) {
    const restored = await restoreFromTrash(projectId, itemId);
    if (!restored) {
        return {
            success: false,
            action: 'preview',
            itemsAffected: 0,
            warnings: [`Item "${itemId}" not found in trash.`],
        };
    }
    const auditId = await appendAuditEntry(projectId, 'restore', restored.itemType, itemId, {
        originalPath: restored.originalPath,
    });
    return {
        success: true,
        action: 'soft_delete',
        itemsAffected: 1,
        warnings: [],
        auditId,
    };
}
/**
 * Purge items from trash (specific item or all).
 */
export async function purgeTrash(projectId, itemId) {
    if (itemId) {
        const manifest = await loadManifest(projectId);
        const item = manifest.find((i) => i.id === itemId);
        const purged = await purgeTrashItem(projectId, itemId);
        if (!purged) {
            return {
                success: false,
                action: 'hard_delete',
                itemsAffected: 0,
                warnings: [`Item "${itemId}" not found in trash.`],
            };
        }
        const auditId = await appendAuditEntry(projectId, 'purge', item?.itemType ?? 'spec', itemId, {});
        return {
            success: true,
            action: 'hard_delete',
            itemsAffected: 1,
            warnings: [],
            auditId,
        };
    }
    const count = await purgeAllTrash(projectId);
    const auditId = await appendAuditEntry(projectId, 'purge', 'project', 'all-trash', {
        itemsPurged: count,
    });
    return {
        success: true,
        action: 'hard_delete',
        itemsAffected: count,
        warnings: [],
        auditId,
    };
}
/**
 * Execute a trash management action.
 */
export async function executeTrashAction(projectId, action, itemId) {
    switch (action) {
        case 'list':
            return listTrash(projectId);
        case 'restore': {
            if (!itemId) {
                return {
                    success: false,
                    action: 'preview',
                    itemsAffected: 0,
                    warnings: ['itemId is required for restore action.'],
                };
            }
            return restoreItem(projectId, itemId);
        }
        case 'purge':
            return purgeTrash(projectId, itemId);
    }
}
//# sourceMappingURL=trash-manager.js.map