import { hashProjectPath } from '../storage/base-store.js';
import { previewDeleteDecision, executeDeleteDecision } from '../engine/deleter/core.js';
import { notifyStoreChange } from '../engine/doc-generator/portal/regen-hook.js';
import { compactResult, formatKeyValue } from './output-formatter.js';
/**
 * Handle delete_decision tool invocation.
 * Supports single or bulk decision deletion.
 */
export async function handleDeleteDecision(params) {
    try {
        const projectId = hashProjectPath(params.projectRoot);
        const decisionIds = Array.isArray(params.decisionId) ? params.decisionId : [params.decisionId];
        const confirm = params.confirm ?? false;
        const force = params.force ?? false;
        if (!confirm) {
            const preview = await previewDeleteDecision(projectId, decisionIds);
            return compactResult(formatKeyValue({
                action: preview.action,
                itemsAffected: preview.itemsAffected,
                message: `Preview: ${String(preview.itemsAffected)} decision(s) would be deleted. Set confirm=true to proceed.`,
            }, 'Delete Preview'));
        }
        const result = await executeDeleteDecision(projectId, decisionIds, force);
        if (!result.success) {
            return {
                content: [
                    {
                        type: 'text',
                        text: formatKeyValue({
                            action: result.action,
                            itemsAffected: result.itemsAffected,
                            warnings: result.warnings.join('; '),
                        }, 'Delete Failed'),
                    },
                ],
                isError: true,
            };
        }
        notifyStoreChange(params.projectRoot, 'decisions');
        return compactResult(formatKeyValue({
            action: result.action,
            itemsAffected: result.itemsAffected,
            message: `Successfully deleted ${String(result.itemsAffected)} decision(s).`,
        }, 'Delete Result'));
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Delete decision failed: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=delete-decision.js.map