(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/cdk/coercion'), require('resize-observer-polyfill'), require('rxjs')) :
    typeof define === 'function' && define.amd ? define('ng-zorro-antd/core/resize-observers', ['exports', '@angular/core', '@angular/cdk/coercion', 'resize-observer-polyfill', 'rxjs'], factory) :
    (global = global || self, factory((global['ng-zorro-antd'] = global['ng-zorro-antd'] || {}, global['ng-zorro-antd'].core = global['ng-zorro-antd'].core || {}, global['ng-zorro-antd'].core['resize-observers'] = {}), global.ng.core, global.ng.cdk.coercion, global.ResizeObserver, global.rxjs));
}(this, (function (exports, core, coercion, ResizeObserver, rxjs) { 'use strict';

    ResizeObserver = ResizeObserver && ResizeObserver.hasOwnProperty('default') ? ResizeObserver['default'] : ResizeObserver;

    /**
     * @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
     */
    /**
     * Factory that creates a new ResizeObserver and allows us to stub it out in unit tests.
     */
    var NzResizeObserverFactory = /** @class */ (function () {
        function NzResizeObserverFactory() {
        }
        NzResizeObserverFactory.prototype.create = function (callback) {
            return typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(callback);
        };
        /** @nocollapse */ NzResizeObserverFactory.ɵfac = function NzResizeObserverFactory_Factory(t) { return new (t || NzResizeObserverFactory)(); };
        /** @nocollapse */ NzResizeObserverFactory.ɵprov = core["ɵɵdefineInjectable"]({ token: NzResizeObserverFactory, factory: NzResizeObserverFactory.ɵfac, providedIn: 'root' });
        return NzResizeObserverFactory;
    }());
    /*@__PURE__*/ (function () { core["ɵsetClassMetadata"](NzResizeObserverFactory, [{
            type: core.Injectable,
            args: [{ providedIn: 'root' }]
        }], null, null); })();
    /** An injectable service that allows watching elements for changes to their content. */
    var NzResizeObserver = /** @class */ (function () {
        function NzResizeObserver(nzResizeObserverFactory) {
            this.nzResizeObserverFactory = nzResizeObserverFactory;
            /** Keeps track of the existing ResizeObservers so they can be reused. */
            this.observedElements = new Map();
        }
        NzResizeObserver.prototype.ngOnDestroy = function () {
            var _this = this;
            this.observedElements.forEach(function (_, element) { return _this.cleanupObserver(element); });
        };
        NzResizeObserver.prototype.observe = function (elementOrRef) {
            var _this = this;
            var element = coercion.coerceElement(elementOrRef);
            return new rxjs.Observable(function (observer) {
                var stream = _this.observeElement(element);
                var subscription = stream.subscribe(observer);
                return function () {
                    subscription.unsubscribe();
                    _this.unobserveElement(element);
                };
            });
        };
        /**
         * Observes the given element by using the existing ResizeObserver if available, or creating a
         * new one if not.
         */
        NzResizeObserver.prototype.observeElement = function (element) {
            if (!this.observedElements.has(element)) {
                var stream_1 = new rxjs.Subject();
                var observer = this.nzResizeObserverFactory.create(function (mutations) { return stream_1.next(mutations); });
                if (observer) {
                    observer.observe(element);
                }
                this.observedElements.set(element, { observer: observer, stream: stream_1, count: 1 });
            }
            else {
                this.observedElements.get(element).count++;
            }
            return this.observedElements.get(element).stream;
        };
        /**
         * Un-observes the given element and cleans up the underlying ResizeObserver if nobody else is
         * observing this element.
         */
        NzResizeObserver.prototype.unobserveElement = function (element) {
            if (this.observedElements.has(element)) {
                this.observedElements.get(element).count--;
                if (!this.observedElements.get(element).count) {
                    this.cleanupObserver(element);
                }
            }
        };
        /** Clean up the underlying ResizeObserver for the specified element. */
        NzResizeObserver.prototype.cleanupObserver = function (element) {
            if (this.observedElements.has(element)) {
                var _a = this.observedElements.get(element), observer = _a.observer, stream = _a.stream;
                if (observer) {
                    observer.disconnect();
                }
                stream.complete();
                this.observedElements.delete(element);
            }
        };
        /** @nocollapse */ NzResizeObserver.ɵfac = function NzResizeObserver_Factory(t) { return new (t || NzResizeObserver)(core["ɵɵinject"](NzResizeObserverFactory)); };
        /** @nocollapse */ NzResizeObserver.ɵprov = core["ɵɵdefineInjectable"]({ token: NzResizeObserver, factory: NzResizeObserver.ɵfac, providedIn: 'root' });
        return NzResizeObserver;
    }());
    /*@__PURE__*/ (function () { core["ɵsetClassMetadata"](NzResizeObserver, [{
            type: core.Injectable,
            args: [{ providedIn: 'root' }]
        }], function () { return [{ type: NzResizeObserverFactory }]; }, 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
     */
    var NzResizeObserversModule = /** @class */ (function () {
        function NzResizeObserversModule() {
        }
        /** @nocollapse */ NzResizeObserversModule.ɵmod = core["ɵɵdefineNgModule"]({ type: NzResizeObserversModule });
        /** @nocollapse */ NzResizeObserversModule.ɵinj = core["ɵɵdefineInjector"]({ factory: function NzResizeObserversModule_Factory(t) { return new (t || NzResizeObserversModule)(); }, providers: [NzResizeObserverFactory] });
        return NzResizeObserversModule;
    }());
    /*@__PURE__*/ (function () { core["ɵsetClassMetadata"](NzResizeObserversModule, [{
            type: core.NgModule,
            args: [{
                    providers: [NzResizeObserverFactory]
                }]
        }], null, null); })();

    exports.NzResizeObserver = NzResizeObserver;
    exports.NzResizeObserversModule = NzResizeObserversModule;

    Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=ng-zorro-antd-core-resize-observers.umd.js.map
