// tools/checkpoint/configure-policy-handler.ts — configure_checkpoint_policy handler (SPEC-411)
import { randomUUID } from 'node:crypto';
import { POLICY_PRESETS, loadCheckpointConfig, saveCheckpointConfig, } from '../../engine/checkpoint/policy-engine.js';
export async function handleConfigureCheckpointPolicy(args) {
    const { projectPath, preset, policies } = args;
    if (preset === undefined && (policies === undefined || policies.length === 0)) {
        return {
            content: [
                {
                    type: 'text',
                    text: 'Error: provide either a preset (strict/balanced/relaxed) or a custom policies array.',
                },
            ],
        };
    }
    let activePolicies;
    let activePreset;
    if (preset !== undefined) {
        const presetDef = POLICY_PRESETS[preset];
        if (presetDef === undefined) {
            return {
                content: [{ type: 'text', text: `Error: unknown preset "${preset}".` }],
            };
        }
        activePolicies = presetDef.policies.map((p) => ({ id: randomUUID(), ...p }));
        activePreset = preset;
    }
    else {
        activePolicies = (policies ?? []).map((p) => ({
            id: randomUUID(),
            transition: p.transition,
            requiredRole: p.requiredRole,
            description: p.description,
            autoApproveAfterHours: p.autoApproveAfterHours,
        }));
        activePreset = 'custom';
    }
    const config = await loadCheckpointConfig(projectPath);
    config.policies = activePolicies;
    config.activePreset = activePreset;
    await saveCheckpointConfig(projectPath, config);
    const policyLines = activePolicies
        .map((p) => `  • ${p.transition} — requires ${p.requiredRole}: ${p.description}`)
        .join('\n');
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Checkpoint policy configured (preset: ${activePreset}).`,
                    '',
                    'Active policies:',
                    policyLines,
                ].join('\n'),
            },
        ],
        structuredContent: { activePreset, policies: activePolicies },
    };
}
//# sourceMappingURL=configure-policy-handler.js.map