/**
 * Batched style scheduler that collects all `getComputedStyle` calls within a single frame and
 * executes them together using `requestAnimationFrame`. This reduces layout thrashing by batching
 * all style reads into a single reflow.
 *
 * @example
 *
 * ```ts
 * // Instead of calling window.getComputedStyle directly:
 * const style = window.getComputedStyle(element)
 * const value = style.getPropertyValue('--my-prop')
 *
 * // Use the scheduler to batch with other reads:
 * const style = await styleScheduler.getComputedStyle(element)
 * const value = style.getPropertyValue('--my-prop')
 *
 * // Or schedule a custom read operation:
 * styleScheduler.scheduleRead(() => {
 *   const style = window.getComputedStyle(element)
 *   doSomethingWith(style)
 * })
 * ```
 */
class StyleScheduler {
    rafId = null;
    pendingReads = [];
    /**
     * Schedule a style read operation to be executed in the next animation frame. All reads scheduled
     * within the same frame will be batched together and executed synchronously in a single batch.
     */
    scheduleRead(callback) {
        this.pendingReads.push(callback);
        if (this.rafId === null) {
            this.rafId = requestAnimationFrame(() => {
                const reads = this.pendingReads;
                this.pendingReads = [];
                this.rafId = null;
                // Execute all reads in one synchronous batch
                // This causes only a single reflow for all the getComputedStyle calls
                reads.forEach((read) => read());
            });
        }
    }
    /**
     * Get computed style for an element in a batched manner. Returns a promise that resolves with the
     * computed style once all batched reads are executed.
     */
    getComputedStyle(element) {
        return new Promise((resolve) => {
            this.scheduleRead(() => {
                resolve(window.getComputedStyle(element));
            });
        });
    }
}
// Singleton instance shared across all components
export const styleScheduler = new StyleScheduler();
//# sourceMappingURL=styleScheduler.js.map