import * as i2 from '@angular/cdk/scrolling';
import { CdkVirtualScrollViewport, ScrollingModule } from '@angular/cdk/scrolling';
import * as i0 from '@angular/core';
import { EventEmitter, Component, Input, Output, ViewChild, NgModule } from '@angular/core';
import { BehaviorSubject, Subscription, takeWhile, auditTime, switchMap, of, NEVER, distinctUntilChanged } from 'rxjs';
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 from '@aztrix/components/spinner';
import { SpinnerModule } from '@aztrix/components/spinner';

class InfiniteScrollComponent {
    constructor(_changeDetection) {
        this._changeDetection = _changeDetection;
        this.items = [];
        this.itemsChanged = new EventEmitter();
        this._scrollWindow = false;
        this.indexChanged$ = new BehaviorSubject(undefined);
        this.loading$ = new BehaviorSubject(true);
        this.done$ = new BehaviorSubject(false);
        this.subscription = new Subscription();
        this.subscriptionRenderChanges = new Subscription();
    }
    set scrollWindow(scrollWindow) {
        this._scrollWindow = typeof scrollWindow === 'boolean' ? scrollWindow : true;
    }
    ngOnInit() {
        this._subscribe();
    }
    ngOnDestroy() {
        this._unsubscribe();
        this.subscriptionRenderChanges.unsubscribe();
    }
    ngAfterViewInit() {
        this.subscriptionRenderChanges = this.viewport.renderedRangeStream.subscribe(() => {
            setTimeout(() => {
                this._checkViewport();
            }, 0);
        });
    }
    reset() {
        this.items = [];
        this._unsubscribe();
        this.loading$.next(true);
        this.done$.next(false);
        this._subscribe();
    }
    _checkViewport() {
        const dataSize = this.itemSize * this.items?.length;
        const viewportSize = this.viewport.getViewportSize();
        if (this.items?.length > 0 && dataSize < viewportSize) {
            this.indexChanged$.next();
        }
    }
    _subscribe() {
        this.subscription = this.indexChanged$
            .pipe(takeWhile(() => !this.done$.value), auditTime(250), switchMap(() => {
            const { end } = this.viewport.getRenderedRange();
            const total = this.viewport.getDataLength();
            if (end + 5 >= total) {
                return of(total);
            }
            return NEVER;
        }), distinctUntilChanged(), switchMap((index) => {
            this.loading$.next(true);
            return this.callback(index, this.batchSize);
        }))
            .subscribe((batch) => {
            if (batch.length < this.batchSize) {
                this.done$.next(true);
            }
            this.items = [...this.items, ...batch];
            this.itemsChanged.next(this.items);
            this.loading$.next(false);
            this._changeDetection.detectChanges();
        });
    }
    _unsubscribe() {
        this.subscription.unsubscribe();
    }
}
InfiniteScrollComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: InfiniteScrollComponent, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
InfiniteScrollComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.1.5", type: InfiniteScrollComponent, selector: "ax-infinite-scroll", inputs: { items: "items", itemSize: "itemSize", batchSize: "batchSize", itemTemplate: "itemTemplate", callback: "callback", scrollWindow: "scrollWindow" }, outputs: { itemsChanged: "itemsChanged" }, viewQueries: [{ propertyName: "viewport", first: true, predicate: CdkVirtualScrollViewport, descendants: true }], ngImport: i0, template: "<ng-container *ngIf=\"(done$ | async) === true && !items?.length\">\n  <ng-content></ng-content>\n</ng-container>\n\n<!-- TODO find a better option to switch between scrollWindow and regular scroll -->\n\n<cdk-virtual-scroll-viewport\n  class=\"viewport\"\n  *ngIf=\"!_scrollWindow\"\n  [itemSize]=\"itemSize\"\n  (scrolledIndexChange)=\"indexChanged$.next()\"\n>\n  <div *cdkVirtualFor=\"let item of items\" [ngStyle]=\"{'height.px': itemSize}\">\n    <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: item}\"></ng-container>\n  </div>\n  <ax-spinner *ngIf=\"(done$ | async) === false\"></ax-spinner>\n</cdk-virtual-scroll-viewport>\n\n<cdk-virtual-scroll-viewport\n  class=\"viewport\"\n  *ngIf=\"_scrollWindow\"\n  scrollWindow\n  [itemSize]=\"itemSize\"\n  (scrolledIndexChange)=\"indexChanged$.next()\"\n>\n  <div *cdkVirtualFor=\"let item of items\" [ngStyle]=\"{'height.px': itemSize}\">\n    <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: item}\"></ng-container>\n  </div>\n  <ax-spinner *ngIf=\"(done$ | async) === false\"></ax-spinner>\n</cdk-virtual-scroll-viewport>\n", styles: [":host{display:block}.viewport{height:100%}\n"], dependencies: [{ kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "directive", type: i2.CdkFixedSizeVirtualScroll, selector: "cdk-virtual-scroll-viewport[itemSize]", inputs: ["itemSize", "minBufferPx", "maxBufferPx"] }, { kind: "directive", type: i2.CdkVirtualForOf, selector: "[cdkVirtualFor][cdkVirtualForOf]", inputs: ["cdkVirtualForOf", "cdkVirtualForTrackBy", "cdkVirtualForTemplate", "cdkVirtualForTemplateCacheSize"] }, { kind: "component", type: i2.CdkVirtualScrollViewport, selector: "cdk-virtual-scroll-viewport", inputs: ["orientation", "appendOnly"], outputs: ["scrolledIndexChange"] }, { kind: "directive", type: i2.CdkVirtualScrollableWindow, selector: "cdk-virtual-scroll-viewport[scrollWindow]" }, { kind: "component", type: i3.SpinnerComponent, selector: "ax-spinner" }, { kind: "pipe", type: i1.AsyncPipe, name: "async" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: InfiniteScrollComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-infinite-scroll', template: "<ng-container *ngIf=\"(done$ | async) === true && !items?.length\">\n  <ng-content></ng-content>\n</ng-container>\n\n<!-- TODO find a better option to switch between scrollWindow and regular scroll -->\n\n<cdk-virtual-scroll-viewport\n  class=\"viewport\"\n  *ngIf=\"!_scrollWindow\"\n  [itemSize]=\"itemSize\"\n  (scrolledIndexChange)=\"indexChanged$.next()\"\n>\n  <div *cdkVirtualFor=\"let item of items\" [ngStyle]=\"{'height.px': itemSize}\">\n    <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: item}\"></ng-container>\n  </div>\n  <ax-spinner *ngIf=\"(done$ | async) === false\"></ax-spinner>\n</cdk-virtual-scroll-viewport>\n\n<cdk-virtual-scroll-viewport\n  class=\"viewport\"\n  *ngIf=\"_scrollWindow\"\n  scrollWindow\n  [itemSize]=\"itemSize\"\n  (scrolledIndexChange)=\"indexChanged$.next()\"\n>\n  <div *cdkVirtualFor=\"let item of items\" [ngStyle]=\"{'height.px': itemSize}\">\n    <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: item}\"></ng-container>\n  </div>\n  <ax-spinner *ngIf=\"(done$ | async) === false\"></ax-spinner>\n</cdk-virtual-scroll-viewport>\n", styles: [":host{display:block}.viewport{height:100%}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }]; }, propDecorators: { items: [{
                type: Input
            }], itemSize: [{
                type: Input
            }], batchSize: [{
                type: Input
            }], itemTemplate: [{
                type: Input
            }], callback: [{
                type: Input
            }], itemsChanged: [{
                type: Output
            }], viewport: [{
                type: ViewChild,
                args: [CdkVirtualScrollViewport]
            }], scrollWindow: [{
                type: Input
            }] } });

class InfiniteScrollModule {
}
InfiniteScrollModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: InfiniteScrollModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
InfiniteScrollModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.5", ngImport: i0, type: InfiniteScrollModule, declarations: [InfiniteScrollComponent], imports: [CommonModule, ScrollingModule, SpinnerModule], exports: [InfiniteScrollComponent] });
InfiniteScrollModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: InfiniteScrollModule, imports: [CommonModule, ScrollingModule, SpinnerModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: InfiniteScrollModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [InfiniteScrollComponent],
                    imports: [CommonModule, ScrollingModule, SpinnerModule],
                    exports: [InfiniteScrollComponent],
                }]
        }] });

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

export { InfiniteScrollComponent, InfiniteScrollModule };
//# sourceMappingURL=aztrix-components-infinite-scroll.mjs.map
//# sourceMappingURL=aztrix-components-infinite-scroll.mjs.map
