import { ContentObserver } from '@angular/cdk/observers';
import * as i0 from '@angular/core';
import { inject, Directive, Host, Optional, Inject, ContentChild, NgModule } from '@angular/core';
import { PopUpTriggers, KBQ_TITLE_TEXT_REF } from '@koobiq/components/core';
import { KbqTooltipTrigger, KbqToolTipModule } from '@koobiq/components/tooltip';
import { Subject, Subscription, throttleTime } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

class KbqTitleDirective extends KbqTooltipTrigger {
    // todo need rename kbqTrigger in popover, tooltip and title. Here workaround for kbq-title and popover on one button
    set trigger(value) {
        super.trigger = value;
    }
    get trigger() {
        return PopUpTriggers.Hover;
    }
    get isOverflown() {
        /** For special cases where the difference is a fraction of a pixel */
        if (this.child.scrollWidth === 0 || this.parent.offsetWidth === this.child.scrollWidth) {
            return this.parent.getBoundingClientRect().width < this.child.getBoundingClientRect().width;
        }
        return this.parent.offsetWidth < this.child.scrollWidth;
    }
    get viewValue() {
        return (this.parent?.textContent || '').trim();
    }
    get parent() {
        return this.parentContainer?.nativeElement || this.parentContainer;
    }
    get child() {
        return this.childContainer.nativeElement || this.childContainer;
    }
    constructor(componentInstance) {
        super();
        this.componentInstance = componentInstance;
        this.contentObserver = inject(ContentObserver);
        this.resizeStream = new Subject();
        this.debounceInterval = 100;
        this.resizeSubscription = Subscription.EMPTY;
        this.contentObserverSubscription = Subscription.EMPTY;
        this.focusMonitorSubscription = Subscription.EMPTY;
    }
    ngAfterViewInit() {
        this.parentContainer = this.parentContainer || this.componentInstance?.parentTextElement || this.elementRef;
        this.childContainer = this.childContainer || this.componentInstance?.textElement || this.elementRef;
        this.content = this.viewValue;
        this.resizeSubscription = this.resizeStream
            .pipe(debounceTime(this.debounceInterval))
            .subscribe(() => (this.disabled = !this.isOverflown));
        this.contentObserverSubscription = this.contentObserver
            .observe(this.parent)
            .pipe(throttleTime(this.debounceInterval))
            .subscribe(() => {
            this.disabled = !this.isOverflown;
            this.content = this.viewValue;
        });
        this.focusMonitorSubscription = this.focusMonitor
            .monitor(this.elementRef)
            .subscribe((origin) => (origin === 'keyboard' ? this.handleElementEnter() : this.hideTooltip()));
    }
    ngOnDestroy() {
        super.ngOnDestroy();
        this.resizeSubscription.unsubscribe();
        this.contentObserverSubscription.unsubscribe();
        this.focusMonitorSubscription.unsubscribe();
        this.focusMonitor.stopMonitoring(this.elementRef);
    }
    handleElementEnter() {
        this.disabled = !this.isOverflown;
    }
    hideTooltip() {
        this.disabled = true;
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqTitleDirective, deps: [{ token: KBQ_TITLE_TEXT_REF, host: true, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); }
    /** @nocollapse */ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.14", type: KbqTitleDirective, selector: "[kbq-title]", host: { listeners: { "mouseenter": "handleElementEnter()", "mouseleave": "hideTooltip()", "window:resize": "resizeStream.next($event)" } }, queries: [{ propertyName: "childContainer", first: true, predicate: ["kbqTitleText"], descendants: true, static: true }, { propertyName: "parentContainer", first: true, predicate: ["kbqTitleContainer"], descendants: true }], exportAs: ["kbqTitle"], usesInheritance: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqTitleDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[kbq-title]',
                    exportAs: 'kbqTitle',
                    host: {
                        '(mouseenter)': 'handleElementEnter()',
                        '(mouseleave)': 'hideTooltip()',
                        '(window:resize)': 'resizeStream.next($event)'
                    }
                }]
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Host
                }, {
                    type: Optional
                }, {
                    type: Inject,
                    args: [KBQ_TITLE_TEXT_REF]
                }] }], propDecorators: { childContainer: [{
                type: ContentChild,
                args: ['kbqTitleText', { descendants: true, static: true }]
            }], parentContainer: [{
                type: ContentChild,
                args: ['kbqTitleContainer']
            }] } });

class KbqTitleModule {
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqTitleModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    /** @nocollapse */ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: KbqTitleModule, declarations: [KbqTitleDirective], imports: [KbqToolTipModule], exports: [KbqTitleDirective] }); }
    /** @nocollapse */ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqTitleModule, imports: [KbqToolTipModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqTitleModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [KbqToolTipModule],
                    declarations: [KbqTitleDirective],
                    exports: [KbqTitleDirective]
                }]
        }] });

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

export { KbqTitleDirective, KbqTitleModule };
//# sourceMappingURL=koobiq-components-title.mjs.map
