import { Pipe } from '@angular/core';
import { EscapeHTML } from '@memberjunction/global';
import * as i0 from "@angular/core";
import * as i1 from "@angular/platform-browser";
/**
 * Pipe to highlight search terms within text.
 * Usage: {{ text | highlightSearch:searchTerm }}
 *
 * The matching portions will be wrapped in a <mark class="search-highlight"> tag.
 */
export class HighlightSearchPipe {
    sanitizer;
    constructor(sanitizer) {
        this.sanitizer = sanitizer;
    }
    transform(value, searchTerm) {
        if (!value) {
            return '';
        }
        if (!searchTerm || searchTerm.trim() === '') {
            return EscapeHTML(value);
        }
        // Escape special regex characters in the search term
        const escapedSearchTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
        // Create a case-insensitive regex
        const regex = new RegExp(`(${escapedSearchTerm})`, 'gi');
        // Split text by the search term and escape each part,
        // wrapping matching terms in the <mark> tags.
        const parts = value.split(regex);
        const highlighted = parts.map((part, index) => {
            // The split regex includes the capturing group, so matching parts
            // will be at odd indices (1, 3, 5, etc.).
            if (index % 2 === 1) {
                return `<mark class="search-highlight">${EscapeHTML(part)}</mark>`;
            }
            return EscapeHTML(part);
        }).join('');
        // Return as SafeHtml to allow innerHTML binding securely
        return this.sanitizer.bypassSecurityTrustHtml(highlighted);
    }
    static ɵfac = function HighlightSearchPipe_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || HighlightSearchPipe)(i0.ɵɵdirectiveInject(i1.DomSanitizer, 16)); };
    static ɵpipe = /*@__PURE__*/ i0.ɵɵdefinePipe({ name: "highlightSearch", type: HighlightSearchPipe, pure: true, standalone: false });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(HighlightSearchPipe, [{
        type: Pipe,
        args: [{
                standalone: false,
                name: 'highlightSearch'
            }]
    }], () => [{ type: i1.DomSanitizer }], null); })();
//# sourceMappingURL=highlight-search.pipe.js.map