// engine/hooks/hook-templates.ts — Claude Code notification & permission hook templates (SPEC-258)
// ---------------------------------------------------------------------------
// Template builders
// ---------------------------------------------------------------------------
function buildNotificationEntry(command, matcher) {
    return {
        matcher: matcher ?? '',
        hooks: [{ type: 'command', command }],
    };
}
function buildMacosTemplate(matcher) {
    return {
        hooks: {
            Notification: [
                buildNotificationEntry('osascript -e \'display notification "Claude needs your attention" with title "Planu"\'', matcher),
            ],
        },
        mergeInstructions: 'Add the "Notification" key to the "hooks" object in .claude/settings.json.',
        description: 'macOS desktop notification via osascript when Claude requests attention.',
    };
}
function buildLinuxTemplate(matcher) {
    return {
        hooks: {
            Notification: [
                buildNotificationEntry("notify-send 'Planu' 'Claude needs your attention'", matcher),
            ],
        },
        mergeInstructions: 'Add the "Notification" key to the "hooks" object in .claude/settings.json.',
        description: 'Linux desktop notification via notify-send when Claude requests attention. Requires libnotify-bin.',
    };
}
function buildWindowsTemplate(matcher) {
    const command = "powershell.exe -Command \"[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); " +
        "[System.Windows.Forms.MessageBox]::Show('Claude needs your attention', 'Planu')\"";
    return {
        hooks: {
            Notification: [buildNotificationEntry(command, matcher)],
        },
        mergeInstructions: 'Add the "Notification" key to the "hooks" object in .claude/settings.json.',
        description: 'Windows desktop notification via PowerShell MessageBox when Claude needs input.',
    };
}
function buildDesktopTemplate(matcher) {
    const platform = process.platform;
    if (platform === 'darwin') {
        return buildMacosTemplate(matcher);
    }
    if (platform === 'win32') {
        return buildWindowsTemplate(matcher);
    }
    // Default to Linux for all other platforms (linux, freebsd, etc.)
    return buildLinuxTemplate(matcher);
}
function buildInstructionsLoadedDebugTemplate() {
    return {
        hooks: {
            InstructionsLoaded: [
                {
                    matcher: '',
                    hooks: [
                        {
                            type: 'command',
                            command: 'echo "[CLAUDE $(date +%H:%M:%S)] Instructions loaded: $CLAUDE_INSTRUCTIONS_FILE" >> ~/.claude/instructions-debug.log',
                        },
                    ],
                },
            ],
        },
        mergeInstructions: 'Add the "InstructionsLoaded" key to the "hooks" object in .claude/settings.json.',
        description: 'Debug hook that appends a timestamped line to ~/.claude/instructions-debug.log every time Claude loads instructions.',
    };
}
function buildPermissionAutoApproveReadonlyTemplate() {
    return {
        hooks: {
            PermissionRequest: [
                {
                    matcher: 'Read|Glob|Grep',
                    hooks: [
                        {
                            type: 'command',
                            command: 'echo \'{"decision": "allow", "reason": "Read-only operation auto-approved by Planu"}\'',
                        },
                    ],
                },
            ],
        },
        mergeInstructions: 'Add the "PermissionRequest" key to the "hooks" object in .claude/settings.json.',
        description: 'Automatically approves read-only permission requests (Read, Glob, Grep) without user confirmation.',
    };
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Generates a Claude Code hook configuration for the given template.
 * `notification-desktop` auto-detects the current platform via process.platform.
 */
export function generateHookTemplate(opts) {
    const { template, notificationMatcher } = opts;
    switch (template) {
        case 'notification-macos':
            return buildMacosTemplate(notificationMatcher);
        case 'notification-linux':
            return buildLinuxTemplate(notificationMatcher);
        case 'notification-windows':
            return buildWindowsTemplate(notificationMatcher);
        case 'notification-desktop':
            return buildDesktopTemplate(notificationMatcher);
        case 'instructions-loaded-debug':
            return buildInstructionsLoadedDebugTemplate();
        case 'permission-auto-approve-readonly':
            return buildPermissionAutoApproveReadonlyTemplate();
        /* v8 ignore next 3 */
        default: {
            const _exhaustive = template;
            throw new Error(`Unknown hook template: ${String(_exhaustive)}`);
        }
    }
}
/**
 * Merges a new hook config into an existing settings object without overwriting.
 * Existing hooks for the same event type are preserved (array merge).
 */
export function mergeHookConfig(existing, newHook) {
    const existingHooks = (existing.hooks ?? {});
    const merged = { ...existingHooks };
    for (const [eventType, entries] of Object.entries(newHook.hooks)) {
        const current = merged[eventType] ?? [];
        merged[eventType] = [...current, ...entries];
    }
    return { ...existing, hooks: merged };
}
/**
 * Returns all valid HookTemplateName values (for use in Zod schemas and descriptions).
 */
export function listHookTemplateNames() {
    return [
        'notification-macos',
        'notification-linux',
        'notification-windows',
        'notification-desktop',
        'instructions-loaded-debug',
        'permission-auto-approve-readonly',
    ];
}
//# sourceMappingURL=hook-templates.js.map