import { capableAgents } from '../../capabilities.js';
import { listInstalledWorkflows, syncWorkflowToVersion } from '../../workflows.js';
import { lazyAgentMap } from './lazy-map.js';
function buildWorkflowsWriter(agent) {
    return {
        kind: 'workflows',
        agent,
        write({ versionHome, selection }) {
            const all = listInstalledWorkflows();
            const synced = [];
            for (const name of selection) {
                const wf = all.get(name);
                if (!wf)
                    continue;
                try {
                    const r = syncWorkflowToVersion(wf.path, name, agent, versionHome);
                    if (r.success)
                        synced.push(name);
                }
                catch { /* per-item failure: skip */ }
            }
            return { synced };
        },
    };
}
export const workflowsWriters = lazyAgentMap(() => {
    const m = {};
    for (const agent of capableAgents('workflows'))
        m[agent] = buildWorkflowsWriter(agent);
    return m;
});
