/**
 * Aggregated cross-deployment roster (HUB side): the hub's own local bots
 * ([[team-roster]]) merged with every member deployment's advertised bots
 * ([[federation-store]]), each tagged with the deployment it belongs to so the
 * UI can group by deployment (local first, then remote by name).
 *
 * Pure read from `{dataDir}` files — testable, no Lark API.
 */
import { buildTeamRoster } from './team-roster.js';
import { listFederatedDeployments } from './federation-store.js';
import { getDeploymentIdentity } from './deployment-identity.js';
import { getTeam, getDefaultTeam, DEFAULT_TEAM_ID } from './team-store.js';
/** A federated deployment is considered stale (likely offline) if it hasn't
 *  synced within this window — its bots are flagged so the UI can de-emphasize them. */
export const FEDERATION_STALE_MS = 5 * 60 * 1000;
/** Hub's local bots + all member deployments' bots, tagged + grouped by deployment. */
export function buildFederatedRoster(dataDir, teamId = DEFAULT_TEAM_ID, configOrder, now = Date.now(), liveBots) {
    const team = getTeam(dataDir, teamId) ?? getDefaultTeam(dataDir);
    const localId = getDeploymentIdentity(dataDir);
    const local = buildTeamRoster(dataDir, teamId, configOrder, liveBots);
    const deployments = [
        { id: localId.deploymentId, name: localId.name, local: true, botCount: local.bots.length, stale: false },
    ];
    const bots = local.bots.map(b => ({
        larkAppId: b.larkAppId,
        name: b.name,
        cliId: b.cliId,
        capability: b.capability,
        hasTeamRole: b.hasTeamRole,
        owner: b.owner ? { unionId: b.owner.unionId, name: b.owner.name } : undefined,
        deployment: { id: localId.deploymentId, name: localId.name, local: true, stale: false },
    }));
    for (const dep of listFederatedDeployments(dataDir, teamId)) {
        const stale = now - dep.lastSeenAt > FEDERATION_STALE_MS;
        deployments.push({ id: dep.deploymentId, name: dep.name, local: false, botCount: dep.bots.length, lastSeenAt: dep.lastSeenAt, stale });
        for (const b of dep.bots) {
            bots.push({
                larkAppId: b.larkAppId,
                name: b.botName,
                cliId: b.cliId,
                capability: b.capability ?? null,
                hasTeamRole: !!b.hasTeamRole,
                botUnionId: b.botUnionId,
                owner: (b.ownerUnionId || b.ownerName) ? { unionId: b.ownerUnionId, name: b.ownerName } : undefined,
                deployment: { id: dep.deploymentId, name: dep.name, local: false, stale },
            });
        }
    }
    return { team: { id: team.id, name: team.name, memberCount: team.members.length }, deployments, bots };
}
//# sourceMappingURL=federation-roster.js.map