export function createSessionEmitPolicyState() {
    return {
        lastSessionEventSignatureByItemId: new Map(),
        lastMetadataSignatureByItemId: new Map(),
    };
}
export function resetSessionEmitPolicyState(state) {
    state.lastSessionEventSignatureByItemId.clear();
    state.lastMetadataSignatureByItemId.clear();
}
export function shouldEmitCanonicalSessionEvent(args) {
    const { state, itemId, payload } = args;
    if (isMetadataOnlyEvent(payload)) {
        const metadataSignature = getMetadataOnlySignature(payload);
        const previousMetadataSignature = state.lastMetadataSignatureByItemId.get(itemId);
        if (previousMetadataSignature === metadataSignature) {
            return false;
        }
        state.lastMetadataSignatureByItemId.set(itemId, metadataSignature);
    }
    const signature = getSessionEventSemanticSignature(payload);
    const previousSignature = state.lastSessionEventSignatureByItemId.get(itemId);
    if (previousSignature === signature) {
        return false;
    }
    state.lastSessionEventSignatureByItemId.set(itemId, signature);
    return true;
}
function getSessionEventSemanticSignature(payload) {
    const semanticPayload = { ...payload };
    delete semanticPayload.timestamp;
    delete semanticPayload.sourceRuntimeId;
    try {
        return JSON.stringify(semanticPayload);
    }
    catch {
        return String(semanticPayload.itemId || "");
    }
}
function isMetadataOnlyEvent(payload) {
    return payload.intent === "metadata-only";
}
function getMetadataOnlySignature(payload) {
    try {
        return JSON.stringify({
            itemId: payload.itemId,
            canonicalItemId: payload.canonicalItemId,
            intent: payload.intent,
            complete: payload.complete ?? null,
            component: payload.component ?? null,
        });
    }
    catch {
        return String(payload.itemId || "");
    }
}
//# sourceMappingURL=session-event-emitter-policy.js.map