export class EventPerformanceOptimizer {
    constructor() {
        this.debounceTimers = new Map, this.throttleTimers = new Map, this.lastCallTimes = new Map;
    }
    debounce(func, config) {
        const {delay: delay, immediate: immediate = !1, maxWait: maxWait} = config;
        let timeout, lastCallTime = 0, lastInvokeTime = 0;
        return (...args) => {
            const now = Date.now();
            if (lastCallTime = now, immediate && !timeout && now - lastInvokeTime > delay) return func(...args), 
            void (lastInvokeTime = now);
            if (!timeout || now - lastCallTime >= delay || maxWait && now - lastInvokeTime >= maxWait) return timeout && (clearTimeout(timeout), 
            timeout = void 0), func(...args), void (lastInvokeTime = now);
            timeout || (timeout = setTimeout((() => {
                func(...args), lastInvokeTime = Date.now(), timeout = void 0;
            }), delay));
        };
    }
    throttle(func, config) {
        const {interval: interval, leading: leading = !0, trailing: trailing = !0} = config;
        let timeout, lastArgs, lastInvokeTime = 0;
        return (...args) => {
            const now = Date.now();
            lastArgs = args, lastInvokeTime || leading || (lastInvokeTime = now);
            const remaining = interval - (now - lastInvokeTime);
            remaining <= 0 || remaining > interval ? (timeout && (clearTimeout(timeout), timeout = void 0), 
            lastInvokeTime = now, func(...args)) : !timeout && trailing && (timeout = setTimeout((() => {
                lastInvokeTime = leading ? Date.now() : 0, timeout = void 0, lastArgs && func(...lastArgs);
            }), remaining));
        };
    }
    clearAll() {
        for (const timer of this.debounceTimers.values()) clearTimeout(timer);
        for (const timer of this.throttleTimers.values()) clearTimeout(timer);
        this.debounceTimers.clear(), this.throttleTimers.clear(), this.lastCallTimes.clear();
    }
    static getRecommendedDebounceConfig(eventType) {
        switch (eventType) {
          case "resized":
          case "spreadsheet_resized":
            return {
                delay: 300,
                immediate: !0,
                maxWait: 1e3
            };

          case "range_data_changed":
            return {
                delay: 100,
                immediate: !1
            };

          case "formula_calculate_start":
          case "formula_calculate_end":
            return {
                delay: 50,
                immediate: !1
            };

          default:
            return null;
        }
    }
    static getRecommendedThrottleConfig(eventType) {
        switch (eventType) {
          case "mousemove":
          case "scroll":
            return {
                interval: 16,
                leading: !0,
                trailing: !0
            };

          case "resize":
            return {
                interval: 100,
                leading: !0,
                trailing: !0
            };

          default:
            return null;
        }
    }
}
//# sourceMappingURL=event-performance.js.map