function extractTextToHighlight(text) {
    text = text.trim();
    const sentenceRegex = /(?<=^|\p{P}\s)\p{P}?\p{Lu}.*?[.!?]+(?=\s+(?:\p{Lu}|\d)|$)/gu;
    const fallbackWordCount = 5;
    const sentences = text.match(sentenceRegex);
    if (sentences?.length) return sentences[0].trim();
    return text.split(/\s+/).slice(0, fallbackWordCount).join(' ');
}
function generateTextFragmentUrl(uri, text, filetype) {
    if ('html' !== filetype || !text) return uri;
    const highlight = extractTextToHighlight(text);
    const encodedTextFragment = encodeURIComponent(highlight).replace(/-/g, '%2D');
    return `${uri}#:~:text=${encodedTextFragment}`;
}
function generatePdfPageUrl(uri, pageNumber) {
    if (!pageNumber || pageNumber <= 0) return uri;
    return `${uri}#page=${pageNumber}`;
}
export { extractTextToHighlight, generatePdfPageUrl, generateTextFragmentUrl };
