export const PEER_PROTOCOL_VERSION = 1;
export const DEFAULT_PEER_SCOPES = [
    "inspect",
    "sessions.read",
    "sessions.write",
    "prompt.send",
    "prompt.abort",
    "queue.read",
    "queue.write",
    "queue.plan.read",
    "queue.plan.write",
    "queue.plan.approve",
    "files.read",
    "files.write",
    "diagnostics.read",
    "logs.read",
];
export function publicPeer(record) {
    const trust = peerTrust(record);
    return {
        id: record.id,
        name: record.name,
        group: record.group,
        url: record.url,
        nodeId: record.nodeId,
        fingerprint: record.fingerprint,
        tlsFingerprint: record.tlsFingerprint,
        enabled: record.enabled,
        direction: record.direction,
        scopes: [...record.scopes],
        allowedAgents: [...record.allowedAgents],
        allowedWorkspaceRoots: [...record.allowedWorkspaceRoots],
        workspaceAliases: { ...record.workspaceAliases },
        createdAt: record.createdAt,
        updatedAt: record.updatedAt,
        lastSeenAt: record.lastSeenAt,
        lastCheckedAt: record.lastCheckedAt,
        lastLatencyMs: record.lastLatencyMs,
        remoteVersion: record.remoteVersion,
        remoteStatus: record.remoteStatus,
        lastError: record.lastError,
        healthHistory: record.healthHistory?.map((sample) => ({ ...sample })),
        trustStatus: trust.status,
        trustWarnings: trust.warnings,
        effectiveAccess: {
            scopes: [...record.scopes],
            allowedAgents: [...record.allowedAgents],
            allowedWorkspaceRoots: [...record.allowedWorkspaceRoots],
            workspaceAliases: { ...record.workspaceAliases },
        },
    };
}
function peerTrust(record) {
    const warnings = [];
    if (!record.enabled) {
        warnings.push("Peer is disabled.");
        return { status: "disabled", warnings };
    }
    if (record.lastError) {
        warnings.push(record.lastError);
        return { status: "error", warnings };
    }
    if (record.url?.startsWith("https://") && !record.tlsFingerprint) {
        warnings.push("TLS fingerprint is not pinned yet. Probe or re-pin this peer before using it over untrusted networks.");
        return { status: "tls-unpinned", warnings };
    }
    return { status: "trusted", warnings };
}
export function publicInvitation(record) {
    return {
        id: record.id,
        name: record.name,
        group: record.group,
        expiresAt: record.expiresAt,
        createdAt: record.createdAt,
        scopes: [...record.scopes],
        allowedAgents: [...record.allowedAgents],
        allowedWorkspaceRoots: [...record.allowedWorkspaceRoots],
        workspaceAliases: { ...record.workspaceAliases },
        usedAt: record.usedAt,
        usedByNodeId: record.usedByNodeId,
    };
}
