import React from 'react';
function sanitizeHtml(html) {
    const escaped = html
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#039;');
    return escaped
        .replace(/&lt;mark&gt;/g, '<mark>')
        .replace(/&lt;\/mark&gt;/g, '</mark>')
        .replace(/&lt;ais-highlight&gt;/g, '<mark>')
        .replace(/&lt;\/ais-highlight&gt;/g, '</mark>')
        .replace(/&lt;em&gt;/g, '<mark>')
        .replace(/&lt;\/em&gt;/g, '</mark>');
}
export function Highlight({ value, highlightedValue }) {
    if (!highlightedValue)
        return React.createElement("span", null, value);
    return (React.createElement("span", { className: "seekora-docsearch-highlight", dangerouslySetInnerHTML: { __html: sanitizeHtml(highlightedValue) } }));
}
export function truncateAroundMatch(content, maxLength = 150) {
    const markIndex = content.indexOf('<mark>');
    if (markIndex === -1 || content.length <= maxLength) {
        if (content.length <= maxLength)
            return content;
        return content.slice(0, maxLength) + '...';
    }
    const halfLength = Math.floor(maxLength / 2);
    let start = Math.max(0, markIndex - halfLength);
    let end = Math.min(content.length, markIndex + halfLength);
    if (start > 0) {
        const spaceIndex = content.indexOf(' ', start);
        if (spaceIndex !== -1 && spaceIndex < markIndex)
            start = spaceIndex + 1;
    }
    if (end < content.length) {
        const spaceIndex = content.lastIndexOf(' ', end);
        if (spaceIndex !== -1 && spaceIndex > markIndex)
            end = spaceIndex;
    }
    let result = content.slice(start, end);
    if (start > 0)
        result = '...' + result;
    if (end < content.length)
        result = result + '...';
    return result;
}
