"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MouseTracker = void 0;
const electron_1 = require("electron");
/**
 * MouseTracker
 *
 * Centralized mouse position tracker for Eden.
 * Provides a single efficient polling mechanism that multiple
 * operations (drag, resize, etc.) can subscribe to.
 */
class MouseTracker {
    subscribers = new Map();
    interval = null;
    lastUpdateTime = 0;
    lastPosition = { x: 0, y: 0 };
    updateInterval;
    /**
     * @param updateInterval - Update interval in milliseconds (default: 8ms for ~120fps)
     */
    constructor(updateInterval = 8) {
        this.updateInterval = updateInterval;
    }
    /**
     * Subscribe to mouse position updates
     * @param id - Unique identifier for this subscriber
     * @param callback - Function to call with mouse updates
     */
    subscribe(id, callback) {
        // Add subscriber
        this.subscribers.set(id, callback);
        // Start tracking if this is the first subscriber
        if (this.subscribers.size === 1) {
            this.start();
        }
    }
    /**
     * Unsubscribe from mouse position updates
     * @param id - Unique identifier of the subscriber to remove
     */
    unsubscribe(id) {
        this.subscribers.delete(id);
        // Stop tracking if no more subscribers
        if (this.subscribers.size === 0) {
            this.stop();
        }
    }
    /**
     * Get the current mouse position
     */
    getCurrentPosition() {
        return electron_1.screen.getCursorScreenPoint();
    }
    /**
     * Check if currently tracking
     */
    isTracking() {
        return this.interval !== null;
    }
    /**
     * Get number of active subscribers
     */
    getSubscriberCount() {
        return this.subscribers.size;
    }
    /**
     * Start the mouse tracking interval
     */
    start() {
        if (this.interval)
            return;
        this.lastUpdateTime = Date.now();
        this.lastPosition = this.getCurrentPosition();
        this.interval = setInterval(() => {
            const now = Date.now();
            const deltaTime = now - this.lastUpdateTime;
            this.lastUpdateTime = now;
            const currentPosition = this.getCurrentPosition();
            // Only emit if cursor has actually moved (skip unnecessary callbacks)
            if (Math.abs(currentPosition.x - this.lastPosition.x) < 1 &&
                Math.abs(currentPosition.y - this.lastPosition.y) < 1) {
                return;
            }
            this.lastPosition = currentPosition;
            // Notify all subscribers of the position change
            for (const callback of this.subscribers.values()) {
                try {
                    callback({ ...currentPosition, deltaTime });
                }
                catch (error) {
                    console.error("Error in MouseTracker callback:", error);
                }
            }
        }, this.updateInterval);
    }
    /**
     * Stop the mouse tracking interval
     */
    stop() {
        if (this.interval) {
            clearInterval(this.interval);
            this.interval = null;
        }
    }
    /**
     * Cleanup and stop tracking
     */
    dispose() {
        this.subscribers.clear();
        this.stop();
    }
}
exports.MouseTracker = MouseTracker;
//# sourceMappingURL=MouseTracker.js.map