"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WindowActivityTimer = void 0;
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const MAX_INACTIVITY_COUNT = 30;
const INACTIVITY_CHECK_INTERVAL = 1000;
class WindowActivityTimer extends ide_core_browser_1.Disposable {
    // Window instance where the timer is active
    constructor(window, inactivityLimit = 30, checkInterval = 1000) {
        super();
        this.window = window;
        this.activityCount = 0;
        this.maxInactivityCount = MAX_INACTIVITY_COUNT;
        this.inactivityCheckInterval = INACTIVITY_CHECK_INTERVAL;
        this.isActive = true;
        // Event emitter for active state changes
        this.activeStateChangedEmitter = new ide_core_browser_1.Emitter();
        // Reset the inactivity timer when activity is detected
        this.resetInactivityTimer = () => {
            this.activityCount = 0;
            this.setActiveState(true);
            if (!this.inactivityCheckTimer) {
                this.startInactivityCheck();
            }
        };
        // Check if the user is inactive and update the state
        this.checkInactivity = () => {
            this.activityCount++;
            if (this.activityCount >= this.maxInactivityCount) {
                this.setActiveState(false);
                this.stopInactivityCheck();
            }
        };
        this.maxInactivityCount = inactivityLimit;
        this.inactivityCheckInterval = checkInterval;
        // Set up event listeners for user activity
        this.setupActivityListeners();
    }
    // Getter for event listener
    get onDidChangeActiveState() {
        return this.activeStateChangedEmitter.event;
    }
    // Set the current activity state
    setActiveState(newState) {
        if (this.isActive !== newState) {
            this.isActive = newState;
            this.activeStateChangedEmitter.fire(newState);
        }
    }
    // Set up event listeners for detecting user activity
    setupActivityListeners() {
        this.window.addEventListener('mousedown', this.resetInactivityTimer, {
            passive: true,
            capture: true,
        });
        this.window.addEventListener('keydown', this.resetInactivityTimer, {
            passive: true,
            capture: true,
        });
        this.window.addEventListener('touchstart', this.resetInactivityTimer, {
            passive: true,
            capture: true,
        });
    }
    // Start the inactivity timer
    startInactivityCheck() {
        this.stopInactivityCheck();
        this.inactivityCheckTimer = setInterval(() => {
            this.checkInactivity();
        }, this.inactivityCheckInterval);
    }
    // Stop the inactivity timer
    stopInactivityCheck() {
        if (this.inactivityCheckTimer) {
            clearInterval(this.inactivityCheckTimer);
            this.inactivityCheckTimer = undefined;
        }
    }
    // Clean up event listeners and stop the timer when disposed
    dispose() {
        this.stopInactivityCheck();
        this.activeStateChangedEmitter.dispose();
        super.dispose();
        this.window.removeEventListener('mousedown', this.resetInactivityTimer, {
            passive: true,
            capture: true,
        });
        this.window.removeEventListener('keydown', this.resetInactivityTimer, {
            passive: true,
            capture: true,
        });
        this.window.removeEventListener('touchstart', this.resetInactivityTimer, {
            passive: true,
            capture: true,
        });
    }
}
exports.WindowActivityTimer = WindowActivityTimer;
//# sourceMappingURL=window-activity-timer.js.map