/**
 * Claude Plugin Enabler
 *
 * Enables installed plugins in .claude/settings.json
 * This is required after plugin installation for them to be active.
 *
 * Migration note: This module will be replaced by vskill settings module
 * (vskill/src/settings/settings.ts) once vskill is a direct dependency.
 * The vskill enablePlugin/disablePlugin API is a drop-in replacement.
 */
import * as fs from '../../../utils/fs-native.js';
import * as path from 'path';
import * as os from 'os';
/**
 * Enable installed SpecWeave plugins in Claude Code settings
 *
 * After installing plugins via `vskill install` (or legacy `claude plugin install`),
 * they must be explicitly enabled in ~/.claude/settings.json for them to be active.
 *
 * @param pluginNames - Array of plugin names to enable (e.g., ['sw', 'sw-github'])
 * @param marketplace - Marketplace name (default: 'specweave')
 * @param customSettingsPath - Optional custom path to settings.json (for testing)
 * @returns true if successful, false otherwise
 */
export function enablePluginsInSettings(pluginNames, marketplace = 'specweave', customSettingsPath) {
    try {
        const settingsPath = customSettingsPath || path.join(os.homedir(), '.claude', 'settings.json');
        // Read existing settings or create new
        let settings = {};
        if (fs.existsSync(settingsPath)) {
            const content = fs.readFileSync(settingsPath, 'utf-8');
            try {
                settings = JSON.parse(content);
            }
            catch {
                // Backup corrupt file before replacing — reuse already-read content
                if (content.trim().length > 0) {
                    try {
                        const bakPath = settingsPath + `.bak.${Date.now()}`;
                        fs.writeFileSync(bakPath, content, 'utf-8');
                        console.warn(`Warning: settings.json was corrupt, backed up to ${bakPath}`);
                    }
                    catch (err) {
                        console.warn(`Warning: settings.json corrupt and backup failed: ${err}`);
                    }
                }
                settings = {};
            }
        }
        // Ensure .claude directory exists
        const claudeDir = path.dirname(settingsPath);
        fs.mkdirSync(claudeDir, { recursive: true });
        // Get existing enabled plugins or create new object
        const enabledPlugins = (settings.enabledPlugins || {});
        // Enable each plugin
        for (const pluginName of pluginNames) {
            const pluginKey = `${pluginName}@${marketplace}`;
            enabledPlugins[pluginKey] = true;
        }
        settings.enabledPlugins = enabledPlugins;
        // Write back with formatting
        fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n');
        return true;
    }
    catch (error) {
        console.warn(`Failed to enable plugins in settings: ${error instanceof Error ? error.message : error}`);
        return false;
    }
}
/**
 * Enable a single plugin in Claude Code settings
 *
 * @param pluginName - Plugin name (e.g., 'sw')
 * @param marketplace - Marketplace name (default: 'specweave')
 * @param customSettingsPath - Optional custom path to settings.json (for testing)
 * @returns true if successful, false otherwise
 */
export function enablePlugin(pluginName, marketplace = 'specweave', customSettingsPath) {
    return enablePluginsInSettings([pluginName], marketplace, customSettingsPath);
}
//# sourceMappingURL=claude-plugin-enabler.js.map