import { tokenizeMathML } from "./mathml-tokenizer.js";
import { resolveMatchedHighlightTargetForBoundary as resolveMatchedTargetForBoundary, resolveHighlightTargetForBoundary as resolveTargetForBoundary, } from "./range-resolver.js";
import { alignSpeechToMath } from "./sequence-aligner.js";
import { resolveBoundaryToSpeechToken, resolveSpokenOffsetToSpeechToken, tokenizeSpeechSource, } from "./speech-tokenizer.js";
export const createMathAwareAlignment = (args) => {
    const speech = tokenizeSpeechSource({ speechText: args.speechText });
    const math = tokenizeMathML(args.mathElement);
    return {
        speech,
        math,
        result: alignSpeechToMath({ speech, math }),
    };
};
export const resolveHighlightTargetForBoundary = (alignment, args) => {
    const boundary = resolveBoundaryToSpeechToken({
        tokenization: alignment.speech,
        position: args.position,
        length: args.length,
        boundaryWord: args.boundaryWord,
    });
    return resolveTargetForBoundary({
        alignment: alignment.result,
        boundary,
    });
};
export const resolveMatchedHighlightTargetForBoundary = (alignment, args) => {
    const boundary = resolveBoundaryToSpeechToken({
        tokenization: alignment.speech,
        position: args.position,
        length: args.length,
        boundaryWord: args.boundaryWord,
    });
    return resolveMatchedTargetForBoundary({
        alignment: alignment.result,
        boundary,
    });
};
export const resolveMatchedHighlightTargetForSpokenBoundary = (alignment, args) => {
    const boundary = resolveSpokenOffsetToSpeechToken({
        tokenization: alignment.speech,
        position: args.position,
        length: args.length,
        boundaryWord: args.boundaryWord,
    });
    return resolveMatchedTargetForBoundary({
        alignment: alignment.result,
        boundary,
    });
};
export const resolveUniqueMathTargetForBoundaryWord = (alignment, args) => {
    if (!args.boundaryWord || /^<[^>]+>$/.test(args.boundaryWord.trim())) {
        return null;
    }
    const boundaryToken = tokenizeSpeechSource({
        speechText: args.boundaryWord,
    }).tokens[0];
    if (!boundaryToken)
        return null;
    const matches = alignment.math.tokens.filter((token) => {
        const normalized = boundaryToken.normalized.toLowerCase();
        return (token.normalized.toLowerCase() === normalized ||
            token.spokenAliases.some((alias) => alias.toLowerCase() === normalized));
    });
    return matches.length === 1 ? matches[0].target : null;
};
//# sourceMappingURL=index.js.map