export const MESSAGE_SOURCE_SECURITY_NOTE = "注意区分消息来源和人员权限范围";
function createAuthContextHeaders(metadata) {
    const headers = [];
    if (metadata?.authTokenName || metadata?.authTokenDescription || metadata?.authUser || metadata?.authPurpose) {
        headers.push({ label: "security note", value: MESSAGE_SOURCE_SECURITY_NOTE });
    }
    if (metadata?.authTokenName) {
        headers.push({ label: "message source auth token", value: metadata.authTokenName });
    }
    if (metadata?.authTokenDescription) {
        headers.push({ label: "message source auth token description", value: metadata.authTokenDescription });
    }
    if (metadata?.authUser) {
        headers.push({ label: "message source user", value: metadata.authUser });
    }
    if (metadata?.authPurpose) {
        headers.push({ label: "message source purpose", value: metadata.authPurpose });
    }
    return headers.length > 0 ? headers : undefined;
}
function getSentAt(metadata) {
    return metadata?.sentAt;
}
function withDeliveryContext(source, metadata) {
    const sentAt = getSentAt(metadata);
    const contextHeaders = createAuthContextHeaders(metadata);
    return {
        ...source,
        ...(sentAt ? { sentAt } : {}),
        ...(contextHeaders ? { contextHeaders } : {}),
    };
}
export function createPeerMessageSource(peerId, metadata) {
    return withDeliveryContext({ kind: "peer", name: peerId }, metadata);
}
export function createHostMessageSource(hostId = "host", metadata) {
    return withDeliveryContext({ kind: "host", name: hostId.trim() || "host" }, metadata);
}
export function createSourceMessageSource(name) {
    return withDeliveryContext({ kind: "source", name }, { sentAt: new Date().toISOString() });
}
export function createAgentMessageSource(agentId) {
    return withDeliveryContext({ kind: "agent", name: agentId }, { sentAt: new Date().toISOString() });
}
export function formatMessageSourceLabel(source) {
    return `${source.kind}/${source.name}`;
}
function formatLocalDateTime(isoString) {
    const date = new Date(isoString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    const hours = String(date.getHours()).padStart(2, "0");
    const minutes = String(date.getMinutes()).padStart(2, "0");
    const seconds = String(date.getSeconds()).padStart(2, "0");
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
export function messageSourceHeaderPrefix(source) {
    const lines = [`[message source: ${formatMessageSourceLabel(source)}]`];
    if (source.sentAt) {
        lines.push(`[message sent at: ${formatLocalDateTime(source.sentAt)}]`);
    }
    if (source.messageId) {
        lines.push(`[message id: ${source.messageId}]`);
    }
    if (source.responseIds && source.responseIds.length > 0) {
        lines.push(`[responding to: ${source.responseIds.join(", ")}]`);
    }
    for (const header of source.contextHeaders ?? []) {
        if (header.label.trim() && header.value.trim()) {
            lines.push(`[${header.label}: ${header.value}]`);
        }
    }
    return `${lines.join("\n")}\n`;
}
//# sourceMappingURL=types.js.map