import { k as debounce, r as rIC } from './helpers-BNj87EbG.js';
import { L as ListenerAbstract, a as SingleSubject } from './listener-CClDIhvK.js';
import { b as balBrowser } from './browser-B1u-Mq75.js';

class BalResizeListener extends ListenerAbstract {
    constructor() {
        super(...arguments);
        this.debouncedNotify = debounce((info) => this.notify(info), 42);
    }
    connect(el) {
        var _a;
        super.connect(el);
        if (typeof ResizeObserver === 'undefined') {
            return;
        }
        if (this.resizeObserver !== undefined) {
            (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
            this.resizeObserver = undefined;
        }
        this.resizeObserver = new ResizeObserver(entries => {
            if (balBrowser.hasWindow) {
                window.requestAnimationFrame(() => {
                    if (!Array.isArray(entries) || !entries.length) {
                        return;
                    }
                    const entry = entries[0];
                    if (this.lastWidth === undefined && this.lastHeight === undefined) {
                        this.lastWidth = entry.contentRect.width;
                        this.lastHeight = entry.contentRect.height;
                    }
                    else {
                        const widthChanged = this.lastWidth !== entry.contentRect.width;
                        const heightChanged = this.lastHeight !== entry.contentRect.height;
                        if (widthChanged || heightChanged) {
                            this.debouncedNotify({
                                width: widthChanged,
                                height: heightChanged,
                            });
                            this.lastWidth = entry.contentRect.width;
                            this.lastHeight = entry.contentRect.height;
                        }
                    }
                });
            }
        });
        this.resizeObserver.observe(el);
    }
    disconnect() {
        var _a;
        super.disconnect();
        (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
        this.resizeObserver = undefined;
    }
}

class BalResizeSubject extends SingleSubject {
    constructor() {
        super((observer, data) => {
            rIC(() => observer.resizeListener(data));
        });
        this.listener = new BalResizeListener();
    }
    attach(observer) {
        super.attach(observer);
        this.listener.connect(observer.el);
        this.listener.add(info => super.notify(info));
    }
    detach() {
        super.detach();
        this.listener.disconnect();
    }
}

function ListenToResize() {
    return function (target, _propertyKey, _descriptor) {
        const { connectedCallback, disconnectedCallback } = target;
        target.connectedCallback = function () {
            if (!this._balResizeSubject) {
                this._balResizeSubject = new BalResizeSubject();
                this._balResizeSubject.attach(this);
            }
            return connectedCallback && connectedCallback.call(this);
        };
        target.disconnectedCallback = function () {
            if (this._balResizeSubject) {
                this._balResizeSubject.detach();
                this._balResizeSubject = undefined;
            }
            return disconnectedCallback && disconnectedCallback.call(this);
        };
    };
}

export { ListenToResize as L };
