import * as i1 from '@angular/common';
import { DOCUMENT, CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, Component, ChangeDetectionStrategy, Inject, Input, Output, NgModule } from '@angular/core';
import { Subscription, fromEvent } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

class BackTopComponent {
    constructor(cdr, el, doc) {
        this.cdr = cdr;
        this.el = el;
        this.doc = doc;
        this.visibleHeight = 300;
        this.bottom = '50px';
        this.right = '30px';
        this.backTopEvent = new EventEmitter();
        this.currScrollTop = 0;
        this.isVisible = false;
        this.SCROLL_REFRESH_INTERVAL = 100;
        this.subs = new Subscription();
        this.document = this.doc;
    }
    ngOnInit() {
        this.addScrollEvent();
        this.showButton();
    }
    ngOnChanges(changes) {
        if (changes['scrollTarget']) {
            if (this.subs) {
                this.subs.unsubscribe();
            }
            this.subs = new Subscription();
            this.addScrollEvent();
        }
    }
    addScrollEvent() {
        this.subs.add(fromEvent(this.getScrollTarget(), 'scroll')
            .pipe(debounceTime(this.SCROLL_REFRESH_INTERVAL))
            .subscribe(() => {
            this.showButton();
            this.cdr.detectChanges();
        }));
    }
    getScrollTarget() {
        if (this.scrollTarget) {
            this.el.nativeElement.querySelector('.devui-backtop').style.position = 'absolute';
            this.scrollTarget.parentElement.style.position = 'relative';
        }
        this.target = this.scrollTarget || window;
        return this.target;
    }
    showButton() {
        this.currScrollTop = this.target === window ?
            (window.pageYOffset || this.document.documentElement.scrollTop || this.document.body.scrollTop) : this.scrollTarget.scrollTop;
        if (this.isVisible !== (this.currScrollTop >= this.visibleHeight)) {
            this.isVisible = !this.isVisible;
        }
    }
    goTop() {
        if (this.target === window) {
            this.document.documentElement.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' });
            this.document.body.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' });
        }
        else {
            this.scrollTarget.style.scrollBehavior = 'smooth';
            this.scrollTarget.scrollTop = 0;
        }
        this.backTopEvent.emit(true);
    }
    ngOnDestroy() {
        if (this.subs) {
            this.subs.unsubscribe();
        }
    }
}
BackTopComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: BackTopComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Component });
BackTopComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.2.0", type: BackTopComponent, selector: "d-back-top", inputs: { customTemplate: "customTemplate", visibleHeight: "visibleHeight", bottom: "bottom", right: "right", scrollTarget: "scrollTarget" }, outputs: { backTopEvent: "backTopEvent" }, usesOnChanges: true, ngImport: i0, template: "<div (click)=\"goTop()\" class=\"devui-backtop\" [ngStyle]=\"{ display: isVisible ? 'block' : 'none', bottom: bottom, right: right }\">\n  <ng-template [ngTemplateOutlet]=\"customTemplate ? customTemplate : default\"></ng-template>\n  <ng-template #default>\n    <div class=\"devui-backtop-content\">\n      <svg\n        width=\"16px\"\n        height=\"16px\"\n        viewBox=\"0 0 16 16\"\n        version=\"1.1\"\n        xmlns=\"http://www.w3.org/2000/svg\"\n        xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n      >\n        <g stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\n          <path\n            d=\"M8.71335931,15.2865297 C8.61179683,16.2090609 7.32293758,16.1267953 7.27304695,15.2865297 C7.27175008,14.6475142 7.27175008,5.26479636 7.27175008,5.26479636 L2.83675052,9.54548344 C2.14185995,10.1440615 1.3143288,9.18731159 1.83135998,8.55773353 C3.79557855,6.65310872 7.3202657,3.24515592 7.40179694,3.16632781 C7.72696878,2.81306222 8.23887498,2.79476534 8.58495308,3.16632781 C9.23193739,3.7919215 14.0334057,8.42146792 14.1791557,8.58804603 C14.66614,9.19338972 13.8787807,10.0892021 13.2066089,9.58451469 C13.0329683,9.43717095 8.71468744,5.26462448 8.71468744,5.26462448 L8.71335931,15.2865297 Z M1.81868811,-8.54871729e-14 L14.1075619,-8.54871729e-14 L14.1075619,1.39509361 L1.81868811,1.39509361 L1.81868811,-8.54871729e-14 Z\"\n            fill=\"#FFFFFF\"\n            fill-rule=\"nonzero\"\n          ></path>\n        </g>\n      </svg>\n    </div>\n  </ng-template>\n</div>\n", styles: [".devui-backtop{width:40px;height:40px;position:fixed;cursor:pointer;z-index:1}.devui-backtop-content{width:40px;height:40px;background-color:#575d6c;background-color:var(--devui-text-weak, #575d6c);opacity:.4;border-radius:50%;display:flex;align-items:center;justify-content:center}.devui-backtop-content:hover{opacity:1}\n"], dependencies: [{ kind: "directive", type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: BackTopComponent, decorators: [{
            type: Component,
            args: [{ selector: 'd-back-top', changeDetection: ChangeDetectionStrategy.OnPush, preserveWhitespaces: false, template: "<div (click)=\"goTop()\" class=\"devui-backtop\" [ngStyle]=\"{ display: isVisible ? 'block' : 'none', bottom: bottom, right: right }\">\n  <ng-template [ngTemplateOutlet]=\"customTemplate ? customTemplate : default\"></ng-template>\n  <ng-template #default>\n    <div class=\"devui-backtop-content\">\n      <svg\n        width=\"16px\"\n        height=\"16px\"\n        viewBox=\"0 0 16 16\"\n        version=\"1.1\"\n        xmlns=\"http://www.w3.org/2000/svg\"\n        xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n      >\n        <g stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\n          <path\n            d=\"M8.71335931,15.2865297 C8.61179683,16.2090609 7.32293758,16.1267953 7.27304695,15.2865297 C7.27175008,14.6475142 7.27175008,5.26479636 7.27175008,5.26479636 L2.83675052,9.54548344 C2.14185995,10.1440615 1.3143288,9.18731159 1.83135998,8.55773353 C3.79557855,6.65310872 7.3202657,3.24515592 7.40179694,3.16632781 C7.72696878,2.81306222 8.23887498,2.79476534 8.58495308,3.16632781 C9.23193739,3.7919215 14.0334057,8.42146792 14.1791557,8.58804603 C14.66614,9.19338972 13.8787807,10.0892021 13.2066089,9.58451469 C13.0329683,9.43717095 8.71468744,5.26462448 8.71468744,5.26462448 L8.71335931,15.2865297 Z M1.81868811,-8.54871729e-14 L14.1075619,-8.54871729e-14 L14.1075619,1.39509361 L1.81868811,1.39509361 L1.81868811,-8.54871729e-14 Z\"\n            fill=\"#FFFFFF\"\n            fill-rule=\"nonzero\"\n          ></path>\n        </g>\n      </svg>\n    </div>\n  </ng-template>\n</div>\n", styles: [".devui-backtop{width:40px;height:40px;position:fixed;cursor:pointer;z-index:1}.devui-backtop-content{width:40px;height:40px;background-color:#575d6c;background-color:var(--devui-text-weak, #575d6c);opacity:.4;border-radius:50%;display:flex;align-items:center;justify-content:center}.devui-backtop-content:hover{opacity:1}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [DOCUMENT]
                }] }]; }, propDecorators: { customTemplate: [{
                type: Input
            }], visibleHeight: [{
                type: Input
            }], bottom: [{
                type: Input
            }], right: [{
                type: Input
            }], scrollTarget: [{
                type: Input
            }], backTopEvent: [{
                type: Output
            }] } });

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

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

export { BackTopComponent, BackTopModule };
//# sourceMappingURL=ng-devui-back-top.mjs.map
