import { getCurrentAgentIdentity } from "../services/request_context.js";
import { substrateEventBus } from "./substrate_event_bus.js";
export function shallowFieldsChanged(before, after) {
    const keys = new Set([...Object.keys(before), ...Object.keys(after)]);
    const changed = [];
    for (const k of keys) {
        const a = before[k];
        const b = after[k];
        if (JSON.stringify(a) !== JSON.stringify(b)) {
            changed.push(k);
        }
    }
    return changed.sort();
}
function thumbprint() {
    return getCurrentAgentIdentity()?.thumbprint ?? undefined;
}
export function emitObservationCreated(params) {
    const base = {
        event_type: "observation.created",
        timestamp: params.timestamp,
        user_id: params.user_id,
        entity_id: params.entity_id,
        entity_type: params.entity_type,
        observation_id: params.observation_id,
        action: "created",
        source_id: params.source_id,
        idempotency_key: params.idempotency_key,
        agent_thumbprint: thumbprint(),
        observation_source: params.observation_source,
        source_peer_id: params.source_peer_id,
    };
    substrateEventBus.emitSubstrateEvent(base);
}
export function emitEntitySnapshotChange(params) {
    substrateEventBus.emitSubstrateEvent({
        event_type: params.event_type,
        timestamp: params.timestamp,
        user_id: params.user_id,
        entity_id: params.entity_id,
        entity_type: params.entity_type,
        observation_id: params.observation_id,
        action: params.event_type === "entity.created" ? "created" : "updated",
        fields_changed: params.fields_changed,
        source_id: params.source_id,
        idempotency_key: params.idempotency_key,
        agent_thumbprint: thumbprint(),
        observation_source: params.observation_source,
        source_peer_id: params.source_peer_id,
    });
}
export function emitRelationshipLifecycle(params) {
    const action = params.event_type === "relationship.created"
        ? "created"
        : params.event_type === "relationship.deleted"
            ? "deleted"
            : "restored";
    substrateEventBus.emitSubstrateEvent({
        event_type: params.event_type,
        timestamp: params.timestamp,
        user_id: params.user_id,
        entity_id: params.relationship_key,
        entity_type: "__relationship__",
        relationship_type: params.relationship_type,
        source_entity_id: params.source_entity_id,
        target_entity_id: params.target_entity_id,
        observation_id: params.observation_id,
        action,
        source_id: params.source_id,
        idempotency_key: params.idempotency_key,
        agent_thumbprint: thumbprint(),
        source_peer_id: params.source_peer_id,
    });
}
export function emitEntityLifecycle(params) {
    const action = params.event_type === "entity.deleted"
        ? "deleted"
        : params.event_type === "entity.restored"
            ? "restored"
            : params.event_type === "entity.merged"
                ? "merged"
                : "split";
    substrateEventBus.emitSubstrateEvent({
        event_type: params.event_type,
        timestamp: params.timestamp,
        user_id: params.user_id,
        entity_id: params.entity_id,
        entity_type: params.entity_type,
        observation_id: params.observation_id,
        action,
        source_id: params.source_id,
        idempotency_key: params.idempotency_key,
        agent_thumbprint: thumbprint(),
        source_peer_id: params.source_peer_id,
    });
}
//# sourceMappingURL=substrate_store_emit.js.map