import { capableAgents } from '../../capabilities.js';
import { discoverPlugins, syncPluginToVersion, pluginSupportsAgent, cleanOrphanedPluginSkills } from '../../plugins/plugins.js';
import { lazyAgentMap } from './lazy-map.js';
function buildPluginsWriter(agent) {
    return {
        kind: 'plugins',
        agent,
        write({ version, versionHome, selection }) {
            const all = discoverPlugins();
            const map = new Map(all.map(p => [p.name, p]));
            // Clean orphan plugin-skills from plugins that no longer exist. Pass the
            // discovered plugins (not just names) so a stale install under one
            // marketplace is trashed even when another marketplace still ships that
            // name — the PHNX-2618 shadow `code` plugin.
            cleanOrphanedPluginSkills(agent, versionHome, all);
            const synced = [];
            for (const name of selection) {
                const plugin = map.get(name);
                if (!plugin || !pluginSupportsAgent(plugin, agent))
                    continue;
                const r = syncPluginToVersion(plugin, agent, versionHome, { version });
                if (r.success)
                    synced.push(name);
            }
            return { synced };
        },
    };
}
export const pluginsWriters = lazyAgentMap(() => {
    const m = {};
    for (const agent of capableAgents('plugins'))
        m[agent] = buildPluginsWriter(agent);
    return m;
});
