export class ResizeObserverEntry {
    target;
    contentRect;
    constructor(target, contentRect) {
        this.target = target;
        this.contentRect = contentRect;
    }
}
export default class ResizeObserver {
    callback;
    observedElements = new Set();
    constructor(callback) {
        this.callback = callback;
    }
    observe(element) {
        if (this.observedElements.has(element)) {
            return;
        }
        this.observedElements.add(element);
        element.resizeObservers ||= new Set();
        element.resizeObservers.add(this);
        let lastMeasuredSize = element.internal_lastMeasuredSize;
        if (lastMeasuredSize === undefined && element.yogaNode) {
            const width = element.yogaNode.getComputedWidth();
            const height = element.yogaNode.getComputedHeight();
            if (!Number.isNaN(width) && !Number.isNaN(height)) {
                lastMeasuredSize = { width, height };
                element.internal_lastMeasuredSize = lastMeasuredSize;
            }
        }
        if (lastMeasuredSize) {
            const entry = new ResizeObserverEntry(element, lastMeasuredSize);
            try {
                this.callback([entry], this);
            }
            catch (error) {
                console.error(error);
            }
        }
    }
    unobserve(element) {
        this.observedElements.delete(element);
        element.resizeObservers?.delete(this);
    }
    disconnect() {
        for (const element of this.observedElements) {
            element.resizeObservers?.delete(this);
        }
        this.observedElements.clear();
    }
    // Internal method called by Ink during layout
    internalTrigger(entries) {
        try {
            this.callback(entries, this);
        }
        catch (error) {
            console.error(error);
        }
    }
}
export function measureAndExtractObservers(node, observerEntries, forceCache = false) {
    const hasObservers = node.resizeObservers && node.resizeObservers.size > 0;
    if ((hasObservers || forceCache) && node.yogaNode) {
        const width = node.yogaNode.getComputedWidth();
        const height = node.yogaNode.getComputedHeight();
        if (!Number.isNaN(width) && !Number.isNaN(height)) {
            const lastSize = node.internal_lastMeasuredSize;
            if (!lastSize || lastSize.width !== width || lastSize.height !== height) {
                node.internal_lastMeasuredSize = { width, height };
                if (hasObservers) {
                    const entry = new ResizeObserverEntry(node, { width, height });
                    for (const observer of node.resizeObservers) {
                        if (!observerEntries.has(observer)) {
                            observerEntries.set(observer, []);
                        }
                        observerEntries.get(observer).push(entry);
                    }
                }
            }
        }
    }
}
export function triggerResizeObservers(node, forceCache = false) {
    const observerEntries = new Map();
    function traverse(n) {
        measureAndExtractObservers(n, observerEntries, forceCache);
        if (n.internal_static ||
            (n.nodeName === 'ink-static-render' && n !== node)) {
            return;
        }
        for (const child of n.childNodes) {
            if (child.nodeName !== '#text') {
                traverse(child);
            }
        }
    }
    traverse(node);
    for (const [observer, entries] of observerEntries) {
        observer.internalTrigger(entries);
    }
}
//# sourceMappingURL=resize-observer.js.map