import { hashProjectPath } from '../storage/base-store.js';
import { previewDeletePattern, executeDeletePattern } from '../engine/deleter/core.js';
import { compactResult, formatKeyValue } from './output-formatter.js';
/**
 * Handle delete_pattern tool invocation.
 * Supports single or bulk pattern deletion.
 */
export async function handleDeletePattern(params) {
    try {
        const projectId = hashProjectPath(params.projectRoot);
        const patternIds = Array.isArray(params.patternId) ? params.patternId : [params.patternId];
        const confirm = params.confirm ?? false;
        const force = params.force ?? false;
        if (!confirm) {
            const preview = await previewDeletePattern(projectId, patternIds);
            return compactResult(formatKeyValue({
                itemsAffected: preview.itemsAffected,
                message: `Preview: ${String(preview.itemsAffected)} pattern(s) would be deleted. Set confirm=true to proceed.`,
            }));
        }
        const result = await executeDeletePattern(projectId, patternIds, force);
        if (!result.success) {
            return {
                content: [
                    compactResult(formatKeyValue({
                        success: result.success,
                        itemsAffected: result.itemsAffected,
                        error: 'Delete operation failed',
                    })).content[0] ?? { type: 'text', text: 'Delete operation failed' },
                ],
                isError: true,
            };
        }
        return compactResult(formatKeyValue({
            success: result.success,
            itemsAffected: result.itemsAffected,
            message: `Successfully deleted ${String(result.itemsAffected)} pattern(s).`,
        }));
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Delete pattern failed: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=delete-pattern.js.map