import * as i0 from '@angular/core';
import { Directive, Input, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { tap, map } from 'rxjs/operators';

class LoadMoreDirective {
    set fusionLoadMoreOf(value) {
        if (!isNullOrUndefined(value)) {
            this.options$.next(value);
            this.loadMore$.next(1);
        }
    }
    constructor(templateRef, vcr) {
        this.templateRef = templateRef;
        this.vcr = vcr;
        this.fusionLoadMoreChunkSize = 10;
        this.options$ = new BehaviorSubject([]);
        this.loadMore$ = new BehaviorSubject(1);
        this.hasMore$ = new BehaviorSubject(false);
    }
    ngOnInit() {
        this.vcr.createEmbeddedView(this.templateRef, {
            $implicit: this.buildImplicitObservable(),
            hasMore$: this.hasMore$.asObservable(),
            loadMore: this.loadMore.bind(this)
        });
    }
    buildImplicitObservable() {
        return combineLatest([this.options$.asObservable(), this.loadMore$.asObservable()]).pipe(tap(([options, index]) => this.hasMore$.next(options.length > this.fusionLoadMoreChunkSize * index)), map(([options, index]) => options.slice(0, this.fusionLoadMoreChunkSize * index)));
    }
    loadMore() {
        this.loadMore$.next(this.loadMore$.getValue() + 1);
    }
    static { this.ɵfac = function LoadMoreDirective_Factory(t) { return new (t || LoadMoreDirective)(i0.ɵɵdirectiveInject(i0.TemplateRef), i0.ɵɵdirectiveInject(i0.ViewContainerRef)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: LoadMoreDirective, selectors: [["", "fusionLoadMore", ""]], inputs: { fusionLoadMoreChunkSize: "fusionLoadMoreChunkSize", fusionLoadMoreOf: "fusionLoadMoreOf" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(LoadMoreDirective, [{
        type: Directive,
        args: [{
                selector: '[fusionLoadMore]'
            }]
    }], () => [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }], { fusionLoadMoreChunkSize: [{
            type: Input
        }], fusionLoadMoreOf: [{
            type: Input
        }] }); })();

class LoadMoreModule {
    static { this.ɵfac = function LoadMoreModule_Factory(t) { return new (t || LoadMoreModule)(); }; }
    static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: LoadMoreModule }); }
    static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [CommonModule] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(LoadMoreModule, [{
        type: NgModule,
        args: [{
                declarations: [LoadMoreDirective],
                imports: [CommonModule],
                exports: [LoadMoreDirective]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(LoadMoreModule, { declarations: [LoadMoreDirective], imports: [CommonModule], exports: [LoadMoreDirective] }); })();

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

export { LoadMoreDirective, LoadMoreModule };
//# sourceMappingURL=ironsource-fusion-ui-directives-load-more.mjs.map
