import * as i0 from '@angular/core';
import { input, output, inject, ElementRef, DestroyRef, Directive, NgModule } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { fromEvent } from 'rxjs';
import { debounceTime, filter } from 'rxjs/operators';

/**
 * Tool directive used to achieve the infinite scroll mechanism.
 */
class InfiniteScrollDirective {
    constructor() {
        /** Scroll percentage at which the onScrollAction event is fired. */
        this.scrollPercent = input(75, ...(ngDevMode ? [{ debugName: "scrollPercent" }] : []));
        /** Scroll PX at which the onScrollAction event is fired. */
        this.scrollOffset = input(null, ...(ngDevMode ? [{ debugName: "scrollOffset" }] : []));
        /** Event emitted when the scrollPercent threshold is met. */
        // eslint-disable-next-line @angular-eslint/no-output-on-prefix
        this.onScrollAction = output();
        /** @hidden */
        this._element = inject(ElementRef);
        /** @hidden */
        this._destroyRef = inject(DestroyRef);
        this._listenOnScroll();
    }
    /** @hidden */
    shouldTriggerAction() {
        const element = this._element.nativeElement;
        const offset = element.scrollTop + element.offsetHeight;
        const scrollOffsetValue = this.scrollOffset();
        if (scrollOffsetValue) {
            return offset > element.scrollHeight - scrollOffsetValue;
        }
        else {
            return offset / element.scrollHeight > this.scrollPercent() / 100;
        }
    }
    /** @hidden */
    _listenOnScroll() {
        fromEvent(this._element.nativeElement, 'scroll')
            .pipe(debounceTime(50), filter(() => this.shouldTriggerAction()), takeUntilDestroyed(this._destroyRef))
            .subscribe(() => this.onScrollAction.emit());
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.0", ngImport: i0, type: InfiniteScrollDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.1.0", type: InfiniteScrollDirective, isStandalone: true, selector: "[fdInfiniteScroll]", inputs: { scrollPercent: { classPropertyName: "scrollPercent", publicName: "scrollPercent", isSignal: true, isRequired: false, transformFunction: null }, scrollOffset: { classPropertyName: "scrollOffset", publicName: "scrollOffset", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { onScrollAction: "onScrollAction" }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.0", ngImport: i0, type: InfiniteScrollDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[fdInfiniteScroll]'
                }]
        }], ctorParameters: () => [], propDecorators: { scrollPercent: [{ type: i0.Input, args: [{ isSignal: true, alias: "scrollPercent", required: false }] }], scrollOffset: [{ type: i0.Input, args: [{ isSignal: true, alias: "scrollOffset", required: false }] }], onScrollAction: [{ type: i0.Output, args: ["onScrollAction"] }] } });

/**
 * @deprecated
 * Use direct imports of components and directives.
 */
class InfiniteScrollModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.0", ngImport: i0, type: InfiniteScrollModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "21.1.0", ngImport: i0, type: InfiniteScrollModule, imports: [InfiniteScrollDirective], exports: [InfiniteScrollDirective] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "21.1.0", ngImport: i0, type: InfiniteScrollModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.0", ngImport: i0, type: InfiniteScrollModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [InfiniteScrollDirective],
                    exports: [InfiniteScrollDirective]
                }]
        }] });

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

export { InfiniteScrollDirective, InfiniteScrollModule };
//# sourceMappingURL=fundamental-ngx-core-infinite-scroll.mjs.map
