import { __decorate, __metadata } from 'tslib';
import { Platform, PlatformModule } from '@angular/cdk/platform';
import { DOCUMENT, NgIf, NgTemplateOutlet, CommonModule } from '@angular/common';
import { ɵɵelementStart, ɵɵelement, ɵɵelementEnd, ɵɵgetCurrentView, ɵɵlistener, ɵɵrestoreView, ɵɵnextContext, ɵɵtemplate, ɵɵtemplateRefExtractor, ɵɵreference, ɵɵproperty, ɵɵadvance, EventEmitter, ɵɵdirectiveInject, ChangeDetectorRef, NgZone, ɵɵdefineComponent, ɵɵNgOnChangesFeature, ɵsetClassMetadata, Component, ChangeDetectionStrategy, ViewEncapsulation, Inject, Input, Output, ɵɵdefineNgModule, ɵɵdefineInjector, ɵɵsetNgModuleScope, NgModule } from '@angular/core';
import { NzConfigService, NzScrollService, fadeMotion, WithConfig, InputNumber, SCROLL_SERVICE_PROVIDER } from 'ng-zorro-antd/core';
import { Subject, fromEvent } from 'rxjs';
import { throttleTime, takeUntil } from 'rxjs/operators';

function NzBackTopComponent_div_0_ng_template_1_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementStart(0, "div", 4);
    ɵɵelement(1, "div", 5);
    ɵɵelementEnd();
} }
function NzBackTopComponent_div_0_ng_template_3_Template(rf, ctx) { }
function NzBackTopComponent_div_0_Template(rf, ctx) { if (rf & 1) {
    var _r49 = ɵɵgetCurrentView();
    ɵɵelementStart(0, "div", 1);
    ɵɵlistener("click", function NzBackTopComponent_div_0_Template_div_click_0_listener() { ɵɵrestoreView(_r49); var ctx_r48 = ɵɵnextContext(); return ctx_r48.clickBackTop(); });
    ɵɵtemplate(1, NzBackTopComponent_div_0_ng_template_1_Template, 2, 0, "ng-template", null, 2, ɵɵtemplateRefExtractor);
    ɵɵtemplate(3, NzBackTopComponent_div_0_ng_template_3_Template, 0, 0, "ng-template", 3);
    ɵɵelementEnd();
} if (rf & 2) {
    var _r45 = ɵɵreference(2);
    var ctx_r44 = ɵɵnextContext();
    ɵɵproperty("@fadeMotion", undefined);
    ɵɵadvance(3);
    ɵɵproperty("ngTemplateOutlet", ctx_r44.nzTemplate || _r45);
} }
var NZ_CONFIG_COMPONENT_NAME = 'backTop';
var NzBackTopComponent = /** @class */ (function () {
    function NzBackTopComponent(
    // tslint:disable-next-line:no-any
    doc, nzConfigService, scrollSrv, platform, cd, zone) {
        this.doc = doc;
        this.nzConfigService = nzConfigService;
        this.scrollSrv = scrollSrv;
        this.platform = platform;
        this.cd = cd;
        this.zone = zone;
        this.scrollListenerDestroy$ = new Subject();
        this.target = null;
        this.visible = false;
        this.nzClick = new EventEmitter();
    }
    NzBackTopComponent.prototype.ngOnInit = function () {
        this.registerScrollEvent();
    };
    NzBackTopComponent.prototype.clickBackTop = function () {
        this.scrollSrv.scrollTo(this.getTarget(), 0);
        this.nzClick.emit(true);
    };
    NzBackTopComponent.prototype.getTarget = function () {
        return this.target || window;
    };
    NzBackTopComponent.prototype.handleScroll = function () {
        if (this.visible === this.scrollSrv.getScroll(this.getTarget()) > this.nzVisibilityHeight) {
            return;
        }
        this.visible = !this.visible;
        this.cd.detectChanges();
    };
    NzBackTopComponent.prototype.registerScrollEvent = function () {
        var _this = this;
        if (!this.platform.isBrowser) {
            return;
        }
        this.scrollListenerDestroy$.next();
        this.handleScroll();
        this.zone.runOutsideAngular(function () {
            fromEvent(_this.getTarget(), 'scroll')
                .pipe(throttleTime(50), takeUntil(_this.scrollListenerDestroy$))
                .subscribe(function () { return _this.handleScroll(); });
        });
    };
    NzBackTopComponent.prototype.ngOnDestroy = function () {
        this.scrollListenerDestroy$.next();
        this.scrollListenerDestroy$.complete();
    };
    NzBackTopComponent.prototype.ngOnChanges = function (changes) {
        var nzTarget = changes.nzTarget;
        if (nzTarget) {
            this.target = typeof this.nzTarget === 'string' ? this.doc.querySelector(this.nzTarget) : this.nzTarget;
            this.registerScrollEvent();
        }
    };
    /** @nocollapse */ NzBackTopComponent.ɵfac = function NzBackTopComponent_Factory(t) { return new (t || NzBackTopComponent)(ɵɵdirectiveInject(DOCUMENT), ɵɵdirectiveInject(NzConfigService), ɵɵdirectiveInject(NzScrollService), ɵɵdirectiveInject(Platform), ɵɵdirectiveInject(ChangeDetectorRef), ɵɵdirectiveInject(NgZone)); };
    /** @nocollapse */ NzBackTopComponent.ɵcmp = ɵɵdefineComponent({ type: NzBackTopComponent, selectors: [["nz-back-top"]], inputs: { nzTemplate: "nzTemplate", nzVisibilityHeight: "nzVisibilityHeight", nzTarget: "nzTarget" }, outputs: { nzClick: "nzClick" }, exportAs: ["nzBackTop"], features: [ɵɵNgOnChangesFeature], decls: 1, vars: 1, consts: [["class", "ant-back-top", 3, "click", 4, "ngIf"], [1, "ant-back-top", 3, "click"], ["defaultContent", ""], [3, "ngTemplateOutlet"], [1, "ant-back-top-content"], [1, "ant-back-top-icon"]], template: function NzBackTopComponent_Template(rf, ctx) { if (rf & 1) {
            ɵɵtemplate(0, NzBackTopComponent_div_0_Template, 4, 2, "div", 0);
        } if (rf & 2) {
            ɵɵproperty("ngIf", ctx.visible);
        } }, directives: [NgIf, NgTemplateOutlet], encapsulation: 2, data: { animation: [fadeMotion] }, changeDetection: 0 });
    __decorate([
        WithConfig(NZ_CONFIG_COMPONENT_NAME, 400), InputNumber(),
        __metadata("design:type", Number)
    ], NzBackTopComponent.prototype, "nzVisibilityHeight", void 0);
    return NzBackTopComponent;
}());
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzBackTopComponent, [{
        type: Component,
        args: [{
                selector: 'nz-back-top',
                exportAs: 'nzBackTop',
                animations: [fadeMotion],
                template: "\n    <div class=\"ant-back-top\" (click)=\"clickBackTop()\" @fadeMotion *ngIf=\"visible\">\n      <ng-template #defaultContent>\n        <div class=\"ant-back-top-content\">\n          <div class=\"ant-back-top-icon\"></div>\n        </div>\n      </ng-template>\n      <ng-template [ngTemplateOutlet]=\"nzTemplate || defaultContent\"></ng-template>\n    </div>\n  ",
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                preserveWhitespaces: false
            }]
    }], function () { return [{ type: undefined, decorators: [{
                type: Inject,
                args: [DOCUMENT]
            }] }, { type: NzConfigService }, { type: NzScrollService }, { type: Platform }, { type: ChangeDetectorRef }, { type: NgZone }]; }, { nzTemplate: [{
            type: Input
        }], nzVisibilityHeight: [{
            type: Input
        }], nzTarget: [{
            type: Input
        }], nzClick: [{
            type: Output
        }] }); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
var NzBackTopModule = /** @class */ (function () {
    function NzBackTopModule() {
    }
    /** @nocollapse */ NzBackTopModule.ɵmod = ɵɵdefineNgModule({ type: NzBackTopModule });
    /** @nocollapse */ NzBackTopModule.ɵinj = ɵɵdefineInjector({ factory: function NzBackTopModule_Factory(t) { return new (t || NzBackTopModule)(); }, providers: [SCROLL_SERVICE_PROVIDER], imports: [[CommonModule, PlatformModule]] });
    return NzBackTopModule;
}());
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵɵsetNgModuleScope(NzBackTopModule, { declarations: [NzBackTopComponent], imports: [CommonModule, PlatformModule], exports: [NzBackTopComponent] }); })();
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzBackTopModule, [{
        type: NgModule,
        args: [{
                declarations: [NzBackTopComponent],
                exports: [NzBackTopComponent],
                imports: [CommonModule, PlatformModule],
                providers: [SCROLL_SERVICE_PROVIDER]
            }]
    }], null, null); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

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

export { NzBackTopComponent, NzBackTopModule };
//# sourceMappingURL=ng-zorro-antd-back-top.js.map
