import * as i0 from '@angular/core';
import { inject, Renderer2, ChangeDetectorRef, numberAttribute, Directive, Input, NgModule } from '@angular/core';
import { KbqTooltipTrigger } from '@koobiq/components/tooltip';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

const MIN_VISIBLE_LENGTH = 50;
class KbqEllipsisCenterDirective extends KbqTooltipTrigger {
    constructor() {
        super(...arguments);
        this.renderer = inject(Renderer2);
        this.cdr = inject(ChangeDetectorRef);
        this.minVisibleLength = MIN_VISIBLE_LENGTH;
        this.charWidth = 7;
        /**
         * Debounce time (ms) for resize events before recalculating ellipsis position.
         * @default 50
         */
        this.debounceInterval = 50;
        /** @docs-private */
        this.resizeStream = new Subject();
        this.resizeSubscription = Subscription.EMPTY;
    }
    set kbqEllipsisCenter(value) {
        this._kbqEllipsisCenter = value;
        this.refresh();
        // check the view to properly calculate text-start and text-end on text initialized
        this.cdr.detectChanges();
    }
    ngOnInit() {
        super.ngOnInit();
        this.content = this._kbqEllipsisCenter;
        this.refresh();
    }
    ngAfterViewInit() {
        this.resizeSubscription = this.resizeStream
            .pipe(debounceTime(this.debounceInterval))
            .subscribe(() => this.refresh());
    }
    ngOnDestroy() {
        super.ngOnDestroy();
        this.resizeSubscription.unsubscribe();
    }
    /**
     * Updates the displayed text with center ellipsis truncation based on container width.
     * Recreates start/end span elements, measures available space, and adjusts text accordingly.
     * @docs-private
     */
    refresh() {
        let start = '';
        let end = '';
        this.content = this._kbqEllipsisCenter;
        const [dataTextStart, dataTextEnd] = [
            'kbq-ellipsis-center_data-text-start',
            'kbq-ellipsis-center_data-text-end'
        ].map((querySelector) => {
            const element = this.elementRef.nativeElement.querySelector(`.${querySelector}`);
            if (element) {
                this.renderer.removeChild(this.elementRef.nativeElement, element);
            }
            const newElement = this.renderer.createElement('span');
            this.renderer.addClass(newElement, querySelector);
            return newElement;
        });
        this.renderer.appendChild(dataTextStart, this.renderer.createText(this._kbqEllipsisCenter));
        this.renderer.appendChild(dataTextEnd, this.renderer.createText(end));
        setTimeout(() => {
            this.disabled = this.elementRef.nativeElement.clientWidth > dataTextStart.scrollWidth;
            if (this.disabled) {
                start = '';
                end = this._kbqEllipsisCenter;
            }
            else {
                const averageCharWidth = this.charWidth;
                const lastCharsLength = Math.round(this.elementRef.nativeElement.clientWidth / 2 / averageCharWidth);
                const sliceIndex = Math.round(this._kbqEllipsisCenter.length - lastCharsLength);
                start = this._kbqEllipsisCenter.slice(0, sliceIndex);
                end = this._kbqEllipsisCenter.slice(sliceIndex);
            }
            dataTextStart.innerText = start;
            dataTextEnd.innerText = end;
            this.cdr.markForCheck();
        });
        this.renderer.appendChild(this.elementRef.nativeElement, dataTextStart);
        this.renderer.appendChild(this.elementRef.nativeElement, dataTextEnd);
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqEllipsisCenterDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); }
    /** @nocollapse */ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "16.1.0", version: "18.2.14", type: KbqEllipsisCenterDirective, selector: "[kbqEllipsisCenter]", inputs: { kbqEllipsisCenter: "kbqEllipsisCenter", minVisibleLength: "minVisibleLength", charWidth: "charWidth", debounceInterval: ["debounceInterval", "debounceInterval", numberAttribute] }, host: { listeners: { "window:resize": "resizeStream.next($event)" }, classAttribute: "kbq-ellipsis-center" }, usesInheritance: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqEllipsisCenterDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[kbqEllipsisCenter]',
                    host: {
                        class: 'kbq-ellipsis-center',
                        '(window:resize)': 'resizeStream.next($event)'
                    }
                }]
        }], propDecorators: { kbqEllipsisCenter: [{
                type: Input
            }], minVisibleLength: [{
                type: Input
            }], charWidth: [{
                type: Input
            }], debounceInterval: [{
                type: Input,
                args: [{ transform: numberAttribute }]
            }] } });
class KbqEllipsisCenterModule {
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqEllipsisCenterModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    /** @nocollapse */ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: KbqEllipsisCenterModule, declarations: [KbqEllipsisCenterDirective], exports: [KbqEllipsisCenterDirective] }); }
    /** @nocollapse */ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqEllipsisCenterModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqEllipsisCenterModule, decorators: [{
            type: NgModule,
            args: [{
                    exports: [KbqEllipsisCenterDirective],
                    declarations: [KbqEllipsisCenterDirective]
                }]
        }] });

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

export { KbqEllipsisCenterDirective, KbqEllipsisCenterModule };
//# sourceMappingURL=koobiq-components-ellipsis-center.mjs.map
