import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Component, ViewEncapsulation, Input, NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import * as i1 from '@deja-js/component/core';
import { RegExpUtils } from '@deja-js/component/core';

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class DejaBoldQueryComponent {
    constructor(diacriticService) {
        this.diacriticService = diacriticService;
        this._regexpOptions = 'i';
        this._firstOccurenceOnly = false;
        this._firstOccurencePerWordOnly = false;
        this._atTheBeginningOfWordOnly = false;
        this._highlightClassName = 'highlight | yellowBackgroundColorHighlight';
    }
    get content() {
        return this._content;
    }
    set query(value) {
        value = this.diacriticService.remove(value);
        if (this._query !== value) {
            this._query = value;
            this.refresh();
        }
    }
    set value(value) {
        this._value = value;
        this.refresh();
    }
    set regexpOption(value) {
        this._regexpOptions = value;
        this.refresh();
    }
    set firstOccurenceOnly(value) {
        this._firstOccurenceOnly = value;
        this.refresh();
    }
    set firstOccurencePerWordOnly(value) {
        this._firstOccurencePerWordOnly = value;
        this.refresh();
    }
    set atTheBeginningOfWordOnly(value) {
        this._atTheBeginningOfWordOnly = value;
        this.refresh();
    }
    get highlightClassName() {
        return this._highlightClassName;
    }
    set highlightClassName(value) {
        this._highlightClassName = value;
        if (!this._highlightClassName) {
            this._highlightClassName = 'highlight';
        }
        this.refresh();
    }
    refresh() {
        if (this._value && this._query && this._query.length > 0) {
            const regexpPattern = this._atTheBeginningOfWordOnly ? (`\\b${RegExpUtils.escapeRegExp(this._query)}`) : RegExpUtils.escapeRegExp(this._query);
            const sc = new RegExp(regexpPattern, this._regexpOptions);
            const value = this._value.toString();
            const search = this.diacriticService.remove(value);
            const splitted = search.split(sc);
            let position = 0;
            const queryLength = this._query.length;
            const contents = [];
            let firstOccurence = true;
            let nbOccurence = 0;
            splitted.forEach(text => {
                if (text) {
                    contents.push(value.slice(position, position + text.length));
                    position += text.length;
                }
                if (position + queryLength <= value.length) {
                    nbOccurence += 1;
                    let skipHighlight = false;
                    if (this._firstOccurencePerWordOnly && nbOccurence > 1) {
                        const words = text.split(/[^a-zA-Z\d]/g);
                        if (words.length === 1) {
                            skipHighlight = true;
                        }
                    }
                    if (!skipHighlight && (!this._firstOccurenceOnly || firstOccurence)) {
                        contents.push(`<span class="${this._highlightClassName}">`);
                    }
                    contents.push(value.slice(position, position + queryLength));
                    if (!skipHighlight && (!this._firstOccurenceOnly || firstOccurence)) {
                        contents.push('</span>');
                    }
                    position += queryLength;
                }
                firstOccurence = false;
            });
            this._content = contents.join('');
        }
        else {
            this._content = this._value;
        }
    }
}
DejaBoldQueryComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaBoldQueryComponent, deps: [{ token: i1.DiacriticService }], target: i0.ɵɵFactoryTarget.Component });
DejaBoldQueryComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.1", type: DejaBoldQueryComponent, selector: "deja-bold-query", inputs: { query: "query", value: "value", regexpOption: "regexpOption", firstOccurenceOnly: "firstOccurenceOnly", firstOccurencePerWordOnly: "firstOccurencePerWordOnly", atTheBeginningOfWordOnly: "atTheBeginningOfWordOnly", highlightClassName: "highlightClassName" }, ngImport: i0, template: '<div [innerHTML]="content"></div>', isInline: true, styles: ["deja-bold-query .highlight{font-weight:700}deja-bold-query .yellowBackgroundColorHighlight{font-weight:inherit;background-color:#ff0;color:#111}\n"], encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaBoldQueryComponent, decorators: [{
            type: Component,
            args: [{ encapsulation: ViewEncapsulation.None, selector: 'deja-bold-query', template: '<div [innerHTML]="content"></div>', styles: ["deja-bold-query .highlight{font-weight:700}deja-bold-query .yellowBackgroundColorHighlight{font-weight:inherit;background-color:#ff0;color:#111}\n"] }]
        }], ctorParameters: function () { return [{ type: i1.DiacriticService }]; }, propDecorators: { query: [{
                type: Input
            }], value: [{
                type: Input
            }], regexpOption: [{
                type: Input
            }], firstOccurenceOnly: [{
                type: Input
            }], firstOccurencePerWordOnly: [{
                type: Input
            }], atTheBeginningOfWordOnly: [{
                type: Input
            }], highlightClassName: [{
                type: Input
            }] } });

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class DejaBoldQueryModule {
}
DejaBoldQueryModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaBoldQueryModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaBoldQueryModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaBoldQueryModule, declarations: [DejaBoldQueryComponent], imports: [CommonModule,
        FormsModule], exports: [DejaBoldQueryComponent] });
DejaBoldQueryModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaBoldQueryModule, imports: [[
            CommonModule,
            FormsModule
        ]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaBoldQueryModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        DejaBoldQueryComponent
                    ],
                    exports: [DejaBoldQueryComponent],
                    imports: [
                        CommonModule,
                        FormsModule
                    ]
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { DejaBoldQueryComponent, DejaBoldQueryModule };
//# sourceMappingURL=deja-js-component-templates.mjs.map
