import { resolveReadableRegion } from "../../highlight-pipeline/index.js";
import { collectMathAwareTextAndMap } from "../../math-aware-text-processing.js";
import { assembleGeneratedSpeech, } from "../assemble-plan.js";
const sliceVisibleMap = (map, start, end) => {
    const sliced = new Map();
    for (let index = start; index < end; index++) {
        const mapping = map.get(index);
        if (mapping) {
            sliced.set(index - start, mapping);
        }
    }
    return sliced;
};
/**
 * DOM adapter: walk a live content root, assemble the (pure) speech plan, and
 * bind each segment to its live-DOM anchor. This is the only generated-speech
 * entry point that touches the DOM; assembly and serialization stay pure.
 */
export const buildGeneratedSpeechFromRoot = async (args) => {
    const extracted = collectMathAwareTextAndMap(args.contentRoot, args.textProcessingOptions);
    const assembled = await assembleGeneratedSpeech({
        chunks: extracted.chunks,
        visibleText: extracted.visibleText,
        language: args.language,
        mathSpeech: args.mathSpeech,
        produceSsml: args.produceSsml,
        resolveMathSpeech: args.resolveMathSpeech,
    });
    const items = assembled.segments.map(({ segment, sourceChunkIndex }) => {
        const sourceChunk = extracted.chunks[sourceChunkIndex];
        if (segment.kind === "prose") {
            const sourceElement = sourceChunk?.type === "text"
                ? (sourceChunk.sourceElement ?? args.contentRoot)
                : args.contentRoot;
            return {
                segment,
                anchor: {
                    sourceElement,
                    regionElement: resolveReadableRegion(sourceElement, args.contentRoot),
                    visibleMap: sliceVisibleMap(extracted.map, segment.visibleSpan.start, segment.visibleSpan.end),
                },
            };
        }
        const mathElement = sourceChunk?.type === "math"
            ? (sourceChunk.sourceElement ?? null)
            : null;
        return {
            segment,
            anchor: {
                sourceElement: mathElement,
                regionElement: mathElement,
            },
        };
    });
    return {
        plan: {
            items,
            locale: assembled.locale,
            visibleText: assembled.visibleText,
            plainSpeechText: assembled.plainSpeechText,
        },
        containsMathMarkup: extracted.containsMathMarkup,
        visibleText: extracted.visibleText,
    };
};
//# sourceMappingURL=build-dom-plan.js.map