import * as i2 from '@angular/common';
import { DOCUMENT, CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Directive, Injectable, Inject, Component, ContentChildren, ViewChildren, HostListener, NgModule } from '@angular/core';
import { Subject, BehaviorSubject, combineLatest, filter, take, takeUntil } from 'rxjs';
import { ROUTER_CONFIGURATION } from '@angular/router';

class BsScrollspyDirective {
    constructor(element) {
        this.element = element;
    }
}
BsScrollspyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollspyDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
BsScrollspyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.7", type: BsScrollspyDirective, selector: "[bsScrollspy]", ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollspyDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsScrollspy]'
                }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }]; } });

class BsScrollOffsetService {
    constructor(routerConfig) {
        this.routerConfig = routerConfig;
    }
    getScrollOffset() {
        if (!this.routerConfig.scrollOffset) {
            return [0, 0];
        }
        else if (Array.isArray(this.routerConfig.scrollOffset)) {
            return this.routerConfig.scrollOffset;
        }
        else {
            return this.routerConfig.scrollOffset();
        }
    }
}
BsScrollOffsetService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollOffsetService, deps: [{ token: ROUTER_CONFIGURATION }], target: i0.ɵɵFactoryTarget.Injectable });
BsScrollOffsetService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollOffsetService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollOffsetService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }], ctorParameters: function () {
        return [{ type: undefined, decorators: [{
                        type: Inject,
                        args: [ROUTER_CONFIGURATION]
                    }] }];
    } });

class BsScrollspyComponent {
    constructor(scrollOffsetService, document, zone) {
        this.scrollOffsetService = scrollOffsetService;
        this.zone = zone;
        this.destroyed$ = new Subject();
        this.viewInit$ = new BehaviorSubject(false);
        this.contentInit$ = new BehaviorSubject(false);
        this.activeDirective = null;
        this.doc = document;
        combineLatest([this.viewInit$, this.contentInit$])
            .pipe(filter(([viewInit, contentInit]) => viewInit && contentInit), take(1))
            .pipe(takeUntil(this.destroyed$))
            .subscribe(() => {
            // this.setActiveDirective();
            this.scrollToCurrentInSpy();
        });
    }
    ngAfterViewInit() {
        this.viewInit$.next(true);
    }
    ngAfterContentInit() {
        this.contentInit$.next(true);
    }
    ngOnDestroy() {
        this.destroyed$.next(true);
    }
    onWindowScroll() {
        this.setActiveDirective();
        this.scrollToCurrentInSpy();
    }
    setActiveDirective() {
        var _a;
        const offsetY = this.scrollOffsetService.getScrollOffset()[1];
        const dirs = this.directives.filter((d) => d.element.nativeElement.getBoundingClientRect().y < offsetY);
        if (this.directives.length === 0) {
            this.activeDirective = null;
        }
        else if (dirs.length === 0) {
            this.activeDirective = (_a = this.directives.get(0)) !== null && _a !== void 0 ? _a : null;
        }
        else {
            this.activeDirective = dirs[dirs.length - 1];
        }
    }
    scrollToCurrentInSpy() {
        if ((typeof window !== 'undefined') && (window.innerWidth >= 768)) {
            if (this.activeDirective) {
                const index = this.directives.toArray().findIndex((v, i) => v === this.activeDirective);
                const anchor = this.anchors.get(index);
                if (anchor && anchor.nativeElement.parentElement) {
                    anchor.nativeElement.scrollIntoView({ block: 'nearest', inline: 'nearest' });
                }
            }
        }
    }
    scrollToHeader(directive) {
        if (typeof window !== 'undefined') {
            const header = directive.element.nativeElement;
            const offsetY = this.scrollOffsetService.getScrollOffset()[1];
            const y = header.getBoundingClientRect().top + window.scrollY - offsetY + 1;
            window.scrollTo({ top: y, behavior: 'smooth' });
        }
    }
}
BsScrollspyComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollspyComponent, deps: [{ token: BsScrollOffsetService }, { token: DOCUMENT }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component });
BsScrollspyComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.2.7", type: BsScrollspyComponent, selector: "bs-scrollspy", host: { listeners: { "window:scroll": "onWindowScroll($event)" } }, queries: [{ propertyName: "directives", predicate: BsScrollspyDirective, descendants: true }], viewQueries: [{ propertyName: "anchors", predicate: ["anchor"], descendants: true }], ngImport: i0, template: "<div class=\"spy\">\n    <ul class=\"text-muted\">\n        <li *ngFor=\"let dir of directives; let i = index\">\n            <span [attr.data-index]=\"i\" class=\"cursor-pointer\" [ngClass]=\"'nav' + dir.element.nativeElement.tagName\" [class.fw-bold]=\"activeDirective?.element === dir.element\" (click)=\"scrollToHeader(dir)\" [title]=\"dir.element.nativeElement.textContent\" #anchor>\n                {{ dir.element.nativeElement.textContent }}\n            </span>\n        </li>\n    </ul>\n</div>\n<div class=\"content\">\n    <ng-content></ng-content>\n</div>", styles: [":host{display:block}.spy>ul{list-style-type:none;padding-left:0}.spy>ul>li:hover{color:rgba(var(--bs-dark-rgb),var(--bs-text-opacity))!important}.spy>ul>li{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}@media (min-width: 768px){:host{display:grid;grid-template-areas:\"content toc\";grid-template-columns:auto 200px;grid-template-rows:auto;gap:inherit;grid-area:main}.spy{position:sticky;top:5rem;right:0;z-index:2;height:calc(100vh - 7rem);overflow-y:auto;grid-area:toc}.spy>ul{padding-left:2rem}}.navH2{margin-left:20px}.navH3{margin-left:40px}.navH4{margin-left:60px}\n"], dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsScrollspyComponent, decorators: [{
            type: Component,
            args: [{ selector: 'bs-scrollspy', template: "<div class=\"spy\">\n    <ul class=\"text-muted\">\n        <li *ngFor=\"let dir of directives; let i = index\">\n            <span [attr.data-index]=\"i\" class=\"cursor-pointer\" [ngClass]=\"'nav' + dir.element.nativeElement.tagName\" [class.fw-bold]=\"activeDirective?.element === dir.element\" (click)=\"scrollToHeader(dir)\" [title]=\"dir.element.nativeElement.textContent\" #anchor>\n                {{ dir.element.nativeElement.textContent }}\n            </span>\n        </li>\n    </ul>\n</div>\n<div class=\"content\">\n    <ng-content></ng-content>\n</div>", styles: [":host{display:block}.spy>ul{list-style-type:none;padding-left:0}.spy>ul>li:hover{color:rgba(var(--bs-dark-rgb),var(--bs-text-opacity))!important}.spy>ul>li{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}@media (min-width: 768px){:host{display:grid;grid-template-areas:\"content toc\";grid-template-columns:auto 200px;grid-template-rows:auto;gap:inherit;grid-area:main}.spy{position:sticky;top:5rem;right:0;z-index:2;height:calc(100vh - 7rem);overflow-y:auto;grid-area:toc}.spy>ul{padding-left:2rem}}.navH2{margin-left:20px}.navH3{margin-left:40px}.navH4{margin-left:60px}\n"] }]
        }], ctorParameters: function () {
        return [{ type: BsScrollOffsetService }, { type: undefined, decorators: [{
                        type: Inject,
                        args: [DOCUMENT]
                    }] }, { type: i0.NgZone }];
    }, propDecorators: { directives: [{
                type: ContentChildren,
                args: [BsScrollspyDirective, { descendants: true }]
            }], anchors: [{
                type: ViewChildren,
                args: ['anchor']
            }], onWindowScroll: [{
                type: HostListener,
                args: ['window:scroll', ['$event']]
            }] } });

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

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

export { BsScrollOffsetService, BsScrollspyComponent, BsScrollspyDirective, BsScrollspyModule };
//# sourceMappingURL=mintplayer-ng-bootstrap-scrollspy.mjs.map
