import { Struct } from './generated/google/protobuf/struct.js';
import { normalizeText } from './helpers.js';
import { optionalString } from './runtime-agent-surface-parser-common.js';
export { parseAgentConsumeEvent } from './runtime-agent-agent-event-parsers.js';
export { parseAppConsumeEvent } from './runtime-agent-app-event-parsers.js';
export { parseSessionSnapshot } from './runtime-agent-snapshot-parsers.js';
export function fromProtoStruct(payload) {
    if (!payload) {
        return {};
    }
    return Struct.toJson(payload);
}
function eventHasConversationAnchor(event) {
    return typeof event.conversationAnchorId === 'string'
        && normalizeText(event.conversationAnchorId).length > 0;
}
export function matchesConsumeRequest(event, request) {
    if (event.localAgentRef !== request.localAgentRef) {
        return false;
    }
    const requestedAnchorId = optionalString(request.conversationAnchorId);
    if (!requestedAnchorId) {
        return true;
    }
    return eventHasConversationAnchor(event) && event.conversationAnchorId === requestedAnchorId;
}
export async function* mergeAsyncIterables(iterables) {
    const iterators = iterables.map((iterable) => iterable[Symbol.asyncIterator]());
    const idlePull = new Promise(() => { });
    const pulls = iterators.map((iterator, index) => iterator.next().then((result) => ({ index, result })));
    let active = pulls.length;
    try {
        while (active > 0) {
            const { index, result } = await Promise.race(pulls);
            if (result.done) {
                pulls[index] = idlePull;
                active -= 1;
                continue;
            }
            const iterator = iterators[index];
            if (!iterator) {
                throw new Error(`mergeAsyncIterables missing iterator for index ${index}`);
            }
            pulls[index] = iterator.next().then((nextResult) => ({ index, result: nextResult }));
            yield result.value;
        }
    }
    finally {
        await Promise.all(iterators.map(async (iterator) => {
            if (typeof iterator.return === 'function') {
                await iterator.return();
            }
        }));
    }
}
//# sourceMappingURL=runtime-agent-surface-parsers.js.map