import { truncate, isNonEmptyString } from "./utils.js";
export async function* adaptNative(lines) {
    for await (const rawLine of lines) {
        const line = rawLine.trim();
        if (!line)
            continue;
        let event;
        try {
            event = JSON.parse(line);
        }
        catch (error) {
            const stack = error instanceof Error ? error.stack : undefined;
            yield {
                event: "error",
                message: `[adaptNative] Malformed JSON line: ${truncate(line, 200)}`,
                stack
            };
            continue;
        }
        const maybeEventType = event?.event;
        if (!isNonEmptyString(maybeEventType)) {
            yield {
                event: "error",
                message: `[adaptNative] Line missing string "event" field: ${truncate(line, 200)}`
            };
            continue;
        }
        yield event;
    }
}
