/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ConfigurableUi
 */
import { InternalConfigurableUiManager } from "./InternalConfigurableUiManager";
/** Time Tracker utility class
 * @internal
 */
export class TimeTracker {
    constructor() {
        this._firstActiveTimestamp = 0;
        this._idleStartTimestamp = 0;
        this._lastActiveTimestamp = 0;
        this._totalIdleTime = 0;
        this._idleCount = 0;
        this._idleTimeout = 5000;
        this._totalTime = 0;
        this._idleTime = 0;
        this._idleTimeCounter = (args) => {
            const idleTimeout = args.idleTimeout ?? this._idleTimeout;
            if (this._lastActiveTimestamp > 0 &&
                this._idleStartTimestamp === 0 &&
                Date.now() - this._lastActiveTimestamp >= idleTimeout) {
                this._idleStartTimestamp = Date.now();
                this._idleCount++;
            }
        };
        this._trackActivity = (_args) => {
            this._lastActiveTimestamp = Date.now();
            if (this._idleStartTimestamp > 0) {
                const lastIdleTime = this._lastActiveTimestamp - this._idleStartTimestamp;
                this._totalIdleTime += lastIdleTime;
                this._idleStartTimestamp = 0;
            }
        };
    }
    /** Starts time tracking
     */
    startTiming() {
        this._totalIdleTime = 0;
        this._idleStartTimestamp = 0;
        this._idleCount = 0;
        this._totalTime = 0;
        this._idleTime = 0;
        this._firstActiveTimestamp = Date.now();
        this._lastActiveTimestamp = this._firstActiveTimestamp;
        InternalConfigurableUiManager.onUiIntervalEvent.addListener(this._idleTimeCounter);
        InternalConfigurableUiManager.onUiActivityEvent.addListener(this._trackActivity);
    }
    /** Stops time tracking
     */
    stopTiming() {
        InternalConfigurableUiManager.onUiIntervalEvent.removeListener(this._idleTimeCounter);
        InternalConfigurableUiManager.onUiActivityEvent.removeListener(this._trackActivity);
        this._totalTime = Date.now() - this._firstActiveTimestamp;
        this._idleTime = this._totalIdleTime + this._idleCount * this._idleTimeout;
    }
    /** Gets engagement time in seconds
     */
    getEngagementTimeSeconds() {
        return (this._totalTime - this._idleTime) / 1000;
    }
    /** Gets total time in seconds
     */
    getTotalTimeSeconds() {
        return this._totalTime / 1000;
    }
    /** Gets idle time in seconds
     */
    getIdleTimeSeconds() {
        return this._idleTime / 1000;
    }
}
//# sourceMappingURL=TimeTracker.js.map