"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrations = void 0;
exports.applyMigrations = applyMigrations;
exports.getCurrentMigrationVersion = getCurrentMigrationVersion;
const DEFAULT_PLUGINS = [
    "embeddings",
    "language",
    "git",
    "vim",
    "linter",
    "github",
    "asana",
    "jira",
    "linear",
    "notion",
    "download",
    "figma",
    "url",
    "tmux",
    "agents-md",
];
const migration1 = {
    version: 1,
    description: "Convert plugins from string[] to { enabled, disabled } format and add missing plugins",
    migrate: (config, context) => {
        const allPluginKeys = context?.allPluginKeys || DEFAULT_PLUGINS;
        let modified = false;
        if (!config.plugins) {
            config.plugins = {
                enabled: [...allPluginKeys],
                disabled: [],
            };
            return { modified: true, config };
        }
        if (Array.isArray(config.plugins)) {
            config.plugins = {
                enabled: config.plugins,
                disabled: [],
            };
            modified = true;
        }
        if (typeof config.plugins === "object" && !Array.isArray(config.plugins)) {
            if (!config.plugins.enabled) {
                config.plugins.enabled = [...allPluginKeys];
                modified = true;
            }
            if (!config.plugins.disabled) {
                config.plugins.disabled = [];
                modified = true;
            }
            const missingPlugins = allPluginKeys.filter((key) => !config.plugins.enabled.includes(key) &&
                !config.plugins.disabled.includes(key));
            if (missingPlugins.length > 0) {
                config.plugins.enabled.push(...missingPlugins);
                modified = true;
            }
        }
        return { modified, config };
    },
};
exports.migrations = [migration1];
function applyMigrations(config, context) {
    let modified = false;
    let currentConfig = { ...config };
    for (const migration of exports.migrations) {
        try {
            const result = migration.migrate(currentConfig, context);
            if (result.modified) {
                console.log(`Applied migration ${migration.version}: ${migration.description}`);
                modified = true;
                currentConfig = result.config;
            }
        }
        catch (error) {
            console.error(`Failed to apply migration ${migration.version}: ${migration.description}`, error);
        }
    }
    return { modified, config: currentConfig };
}
function getCurrentMigrationVersion() {
    return exports.migrations.length > 0
        ? Math.max(...exports.migrations.map((m) => m.version))
        : 0;
}
//# sourceMappingURL=migrations.js.map