// Shared helpers for the parser adapters (Epic #189, Issue #266). NOT exported from the
// package barrel — kept internal so the adapter surface stays minimal.
export function emptyResult(capability, documentId, options, diagnostics = [], units = []) {
    return {
        documentId,
        parser: { parserId: capability.parserId, parserVersion: capability.parserVersion },
        pages: [],
        sections: [],
        units,
        diagnostics,
        extractedAt: options.now(),
    };
}
export function diagnostic(code, message, documentId, severity = "info") {
    return { severity, code, message, documentId };
}
export function shouldStop(startedAt, options, emittedUnits) {
    if (options.signal?.aborted === true) {
        return { stop: true, code: "PARSER_CANCELLED", message: "caller aborted parser" };
    }
    if (options.now() - startedAt > options.timeoutMs) {
        return {
            stop: true,
            code: "PARSER_TIMEOUT",
            message: `exceeded ${String(options.timeoutMs)}ms deadline`,
        };
    }
    if (emittedUnits >= options.maxUnitsPerDocument) {
        return {
            stop: true,
            code: "UNIT_LIMIT_REACHED",
            message: `reached maxUnitsPerDocument=${String(options.maxUnitsPerDocument)}`,
        };
    }
    return { stop: false };
}
export function oversizeDiagnostic(documentId, byteLength, maxBytes) {
    return diagnostic("OVERSIZED_FILE", `input size ${String(byteLength)} exceeds maxBytes=${String(maxBytes)}`, documentId, "info");
}
export function decodeUtf8(bytes) {
    const decoder = new TextDecoder("utf-8", { fatal: false });
    const raw = decoder.decode(bytes);
    if (raw.length > 0 && raw.charCodeAt(0) === 0xfeff) {
        return { text: raw.slice(1), bomBytes: 3 };
    }
    return { text: raw, bomBytes: 0 };
}
