"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UxpResizeObserver = void 0;
/*
Copyright 2019 Adobe
All Rights Reserved.

NOTICE: Adobe permits you to use, modify, and distribute this file in
accordance with the terms of the Adobe license agreement accompanying
it. If you have received this file from a source other than Adobe,
then your use, modification, or distribution of it requires the prior
written permission of Adobe.
*/
const __1 = require("..");
class UxpResizeObserver {
    constructor(callback) {
        const resizeObserver = this;
        this.elements = new Set();
        this.pendingEntries = [];
        this.externalObserver = callback;
        this.internalObserver = function () {
            let element = this;
            let width = element.offsetWidth;
            let height = element.offsetHeight;
            resizeObserver.queueCallback({ target: element, contentRect: new DOMRect(0, 0, width, height) });
        };
        this.queuedCallback = () => {
            this.queuedCallbackId = null;
            if (this.pendingEntries.length > 0) {
                let entries = this.pendingEntries;
                this.pendingEntries = [];
                callback(entries, this);
            }
        };
    }
    queueCallback(entry) {
        this.pendingEntries.push(entry);
        if (this.queuedCallbackId == null) {
            this.queuedCallbackId = setTimeout(this.queuedCallback, 0);
        }
    }
    observe(element, options) {
        if (options != null && options.box !== "border-box") {
            console.warn(`ResizeObserver shim only supports 'border-box', not ${options.box}`);
        }
        element.addEventListener("resize", this.internalObserver);
        this.elements.add(element);
    }
    unobserve(element) {
        if (this.elements.has(element)) {
            element.removeEventListener("resize", this.internalObserver);
            this.elements.delete(element);
        }
    }
    disconnect() {
        for (let element of this.elements.values()) {
            this.unobserve(element);
        }
    }
}
exports.UxpResizeObserver = UxpResizeObserver;
let ResizeObserver = UxpResizeObserver;
if (typeof window !== "undefined") {
    if (window.ResizeObserver == null) {
        if (__1.isUXP) {
            window.ResizeObserver = ResizeObserver;
        }
        else {
            console.error("This browser requires a ResizeObserver shim");
        }
    }
    else {
        ResizeObserver = window.ResizeObserver;
    }
}
exports.default = ResizeObserver;
