import { capableAgents } from '../../capabilities.js';
import { applyPermissionsToVersion as applyPermsToVersion, buildPermissionsFromGroups, } from '../../permissions.js';
import { lazyAgentMap } from './lazy-map.js';
function buildPermissionsWriter(agent) {
    return {
        kind: 'permissions',
        agent,
        write({ versionHome, selection, cwd }) {
            if (selection.length === 0)
                return { synced: [] };
            const built = buildPermissionsFromGroups(selection);
            const hasAllow = built.allow.length > 0;
            const hasDeny = (built.deny?.length ?? 0) > 0;
            if (!hasAllow && !hasDeny)
                return { synced: [] };
            const r = applyPermsToVersion(agent, built, versionHome, true, cwd);
            return { synced: r.success ? selection : [] };
        },
    };
}
export const permissionsWriters = lazyAgentMap(() => {
    const m = {};
    for (const agent of capableAgents('allowlist'))
        m[agent] = buildPermissionsWriter(agent);
    return m;
});
