import { __esDecorate, __runInitializers } from 'tslib';
import { Directionality } from '@angular/cdk/bidi';
import { normalizePassiveListenerOptions, Platform } from '@angular/cdk/platform';
import { NgTemplateOutlet } from '@angular/common';
import * as i0 from '@angular/core';
import { inject, NgZone, ChangeDetectorRef, DestroyRef, EventEmitter, DOCUMENT, numberAttribute, ViewChild, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, takeUntil } from 'rxjs/operators';
import { fadeMotion } from 'ng-zorro-antd/core/animation';
import { WithConfig, NzConfigService } from 'ng-zorro-antd/core/config';
import { NzScrollService } from 'ng-zorro-antd/core/services';
import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
import * as i1 from 'ng-zorro-antd/icon';
import { NzIconModule } from 'ng-zorro-antd/icon';

const NZ_CONFIG_MODULE_NAME = 'backTop';
const passiveEventListenerOptions = normalizePassiveListenerOptions({ passive: true });
/**
 * @deprecated Will be removed in v21. It is recommended to use `<nz-float-button-top>` instead.
 */
let NzBackTopComponent = (() => {
    let _nzVisibilityHeight_decorators;
    let _nzVisibilityHeight_initializers = [];
    let _nzVisibilityHeight_extraInitializers = [];
    return class NzBackTopComponent {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
            _nzVisibilityHeight_decorators = [WithConfig()];
            __esDecorate(null, null, _nzVisibilityHeight_decorators, { kind: "field", name: "nzVisibilityHeight", static: false, private: false, access: { has: obj => "nzVisibilityHeight" in obj, get: obj => obj.nzVisibilityHeight, set: (obj, value) => { obj.nzVisibilityHeight = value; } }, metadata: _metadata }, _nzVisibilityHeight_initializers, _nzVisibilityHeight_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        nzConfigService = inject(NzConfigService);
        scrollSrv = inject(NzScrollService);
        platform = inject(Platform);
        zone = inject(NgZone);
        cdr = inject(ChangeDetectorRef);
        directionality = inject(Directionality);
        destroyRef = inject(DestroyRef);
        _nzModuleName = NZ_CONFIG_MODULE_NAME;
        scrollListenerDestroy$ = new Subject();
        target = null;
        visible = false;
        dir = this.directionality.value || 'ltr';
        nzTemplate;
        nzVisibilityHeight = __runInitializers(this, _nzVisibilityHeight_initializers, 400);
        nzTarget = __runInitializers(this, _nzVisibilityHeight_extraInitializers);
        nzDuration = 450;
        nzClick = new EventEmitter();
        set backTop(backTop) {
            if (backTop) {
                this.backTopClickSubscription.unsubscribe();
                this.backTopClickSubscription = fromEventOutsideAngular(backTop.nativeElement, 'click')
                    .pipe(takeUntilDestroyed(this.destroyRef))
                    .subscribe(() => {
                    this.scrollSrv.scrollTo(this.getTarget(), 0, { duration: this.nzDuration });
                    if (this.nzClick.observers.length) {
                        this.zone.run(() => this.nzClick.emit(true));
                    }
                });
            }
        }
        backTopClickSubscription = Subscription.EMPTY;
        doc = inject(DOCUMENT);
        constructor() {
            this.destroyRef.onDestroy(() => {
                this.scrollListenerDestroy$.next(true);
                this.scrollListenerDestroy$.complete();
            });
        }
        ngOnInit() {
            this.registerScrollEvent();
            this.directionality.change?.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((direction) => {
                this.dir = direction;
                this.cdr.detectChanges();
            });
            this.dir = this.directionality.value;
        }
        getTarget() {
            return this.target || window;
        }
        handleScroll() {
            const newVisible = this.scrollSrv.getScroll(this.getTarget()) > this.nzVisibilityHeight;
            if (this.visible !== newVisible) {
                this.visible = newVisible;
                this.cdr.detectChanges();
            }
        }
        registerScrollEvent() {
            if (!this.platform.isBrowser) {
                return;
            }
            this.scrollListenerDestroy$.next(true);
            this.handleScroll();
            fromEventOutsideAngular(this.getTarget(), 'scroll', passiveEventListenerOptions)
                .pipe(debounceTime(50), takeUntil(this.scrollListenerDestroy$))
                .subscribe(() => this.handleScroll());
        }
        ngOnChanges(changes) {
            const { nzTarget } = changes;
            if (nzTarget) {
                this.target = typeof this.nzTarget === 'string' ? this.doc.querySelector(this.nzTarget) : this.nzTarget;
                this.registerScrollEvent();
            }
        }
        static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.0", ngImport: i0, type: NzBackTopComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
        static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.0", type: NzBackTopComponent, isStandalone: true, selector: "nz-back-top", inputs: { nzTemplate: "nzTemplate", nzVisibilityHeight: ["nzVisibilityHeight", "nzVisibilityHeight", numberAttribute], nzTarget: "nzTarget", nzDuration: ["nzDuration", "nzDuration", numberAttribute] }, outputs: { nzClick: "nzClick" }, viewQueries: [{ propertyName: "backTop", first: true, predicate: ["backTop"], descendants: true }], exportAs: ["nzBackTop"], usesOnChanges: true, ngImport: i0, template: `
    @if (visible) {
      <div #backTop class="ant-back-top" [class.ant-back-top-rtl]="dir === 'rtl'" @fadeMotion>
        <ng-template #defaultContent>
          <div class="ant-back-top-content">
            <div class="ant-back-top-icon">
              <nz-icon nzType="vertical-align-top" />
            </div>
          </div>
        </ng-template>
        <ng-template [ngTemplateOutlet]="nzTemplate || defaultContent"></ng-template>
      </div>
    }
  `, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i1.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }], animations: [fadeMotion], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
    };
})();
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.0", ngImport: i0, type: NzBackTopComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'nz-back-top',
                    exportAs: 'nzBackTop',
                    animations: [fadeMotion],
                    imports: [NgTemplateOutlet, NzIconModule],
                    template: `
    @if (visible) {
      <div #backTop class="ant-back-top" [class.ant-back-top-rtl]="dir === 'rtl'" @fadeMotion>
        <ng-template #defaultContent>
          <div class="ant-back-top-content">
            <div class="ant-back-top-icon">
              <nz-icon nzType="vertical-align-top" />
            </div>
          </div>
        </ng-template>
        <ng-template [ngTemplateOutlet]="nzTemplate || defaultContent"></ng-template>
      </div>
    }
  `,
                    changeDetection: ChangeDetectionStrategy.OnPush,
                    encapsulation: ViewEncapsulation.None
                }]
        }], ctorParameters: () => [], propDecorators: { nzTemplate: [{
                type: Input
            }], nzVisibilityHeight: [{
                type: Input,
                args: [{ transform: numberAttribute }]
            }], nzTarget: [{
                type: Input
            }], nzDuration: [{
                type: Input,
                args: [{ transform: numberAttribute }]
            }], nzClick: [{
                type: Output
            }], backTop: [{
                type: ViewChild,
                args: ['backTop', { static: false }]
            }] } });

/**
 * 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
 */
/**
 * @deprecated Will be removed in v21. It is recommended to use `<nz-float-button-top>` instead.
 */
class NzBackTopModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.0", ngImport: i0, type: NzBackTopModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.0", ngImport: i0, type: NzBackTopModule, imports: [NzBackTopComponent], exports: [NzBackTopComponent] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.0", ngImport: i0, type: NzBackTopModule, imports: [NzBackTopComponent] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.0", ngImport: i0, type: NzBackTopModule, decorators: [{
            type: NgModule,
            args: [{
                    exports: [NzBackTopComponent],
                    imports: [NzBackTopComponent]
                }]
        }] });

/**
 * 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.mjs.map
