"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnreferencedStateTracker = exports.UnreferencedStateTrackerMap = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const gcDefinitions_js_1 = require("./gcDefinitions.js");
/** A wrapper around common-utils Timer that requires the timeout when calling start/restart */
class TimerWithNoDefaultTimeout extends internal_1.Timer {
    callback;
    constructor(callback) {
        // The default timeout/handlers will never be used since start/restart pass overrides below
        super(0, () => {
            throw new Error("DefaultHandler should not be used");
        });
        this.callback = callback;
    }
    start(timeoutMs) {
        super.start(timeoutMs, this.callback);
    }
    restart(timeoutMs) {
        super.restart(timeoutMs, this.callback);
    }
}
/** The collection of UnreferencedStateTrackers for all unreferenced nodes. Ensures stopTracking is called when deleting */
class UnreferencedStateTrackerMap extends Map {
    /** Delete the given key, and stop tracking if that node was actually unreferenced */
    delete(key) {
        // Stop tracking so as to clear out any running timers.
        this.get(key)?.stopTracking();
        // Delete the node as we don't need to track it any more.
        return super.delete(key);
    }
}
exports.UnreferencedStateTrackerMap = UnreferencedStateTrackerMap;
/**
 * Helper class that tracks the state of an unreferenced node such as the time it was unreferenced and if it can
 * be tombstoned or deleted by the sweep phase.
 */
class UnreferencedStateTracker {
    unreferencedTimestampMs;
    inactiveTimeoutMs;
    tombstoneTimeoutMs;
    sweepGracePeriodMs;
    _state = gcDefinitions_js_1.UnreferencedState.Active;
    get state() {
        return this._state;
    }
    /** Timer to indicate when an unreferenced object is considered Inactive */
    inactiveTimer;
    /** Timer to indicate when an unreferenced object is Tombstone-Ready */
    tombstoneTimer;
    /** Timer to indicate when an unreferenced object is Sweep-Ready */
    sweepTimer;
    constructor(unreferencedTimestampMs, 
    /** The time after which node transitions to Inactive state. */
    inactiveTimeoutMs, 
    /** The current reference timestamp used to track how long this node has been unreferenced for. */
    currentReferenceTimestampMs, 
    /** The time after which node transitions to TombstoneReady state; undefined if session expiry is disabled. */
    tombstoneTimeoutMs, 
    /** The delay from TombstoneReady to SweepReady (only applies if tombstoneTimeoutMs is defined) */
    sweepGracePeriodMs) {
        this.unreferencedTimestampMs = unreferencedTimestampMs;
        this.inactiveTimeoutMs = inactiveTimeoutMs;
        this.tombstoneTimeoutMs = tombstoneTimeoutMs;
        this.sweepGracePeriodMs = sweepGracePeriodMs;
        (0, internal_2.validatePrecondition)(this.tombstoneTimeoutMs === undefined ||
            this.tombstoneTimeoutMs >= this.inactiveTimeoutMs, "inactiveTimeoutMs must not be greater than the tombstoneTimeoutMs");
        this.sweepTimer = new TimerWithNoDefaultTimeout(() => {
            this._state = gcDefinitions_js_1.UnreferencedState.SweepReady;
            (0, internal_1.assert)(!this.inactiveTimer.hasTimer && !this.tombstoneTimer.hasTimer, 0x863 /* inactiveTimer or tombstoneTimer still running after sweepTimer fired! */);
        });
        this.tombstoneTimer = new TimerWithNoDefaultTimeout(() => {
            this._state = gcDefinitions_js_1.UnreferencedState.TombstoneReady;
            (0, internal_1.assert)(!this.inactiveTimer.hasTimer, 0x864 /* inactiveTimer still running after tombstoneTimer fired! */); // aka 0x3b1
            if (this.sweepGracePeriodMs > 0) {
                // After the node becomes tombstone ready, start the sweep timer after which the node will be ready for sweep.
                this.sweepTimer.restart(this.sweepGracePeriodMs);
            }
            else {
                this._state = gcDefinitions_js_1.UnreferencedState.SweepReady;
            }
        });
        this.inactiveTimer = new TimerWithNoDefaultTimeout(() => {
            this._state = gcDefinitions_js_1.UnreferencedState.Inactive;
            // After the node becomes inactive, start the tombstone timer after which the node will be ready for tombstone.
            if (this.tombstoneTimeoutMs !== undefined) {
                this.tombstoneTimer.restart(this.tombstoneTimeoutMs - this.inactiveTimeoutMs);
            }
        });
        this.updateTracking(currentReferenceTimestampMs);
    }
    /* Updates the unreferenced state based on the provided timestamp. */
    updateTracking(currentReferenceTimestampMs) {
        const unreferencedDurationMs = currentReferenceTimestampMs - this.unreferencedTimestampMs;
        // Below we will set the appropriate timer (or none). Any running timers are superceded by the new currentReferenceTimestampMs
        this.clearTimers();
        // If the node has been unreferenced long enough, update the state to SweepReady.
        if (this.tombstoneTimeoutMs !== undefined &&
            unreferencedDurationMs >= this.tombstoneTimeoutMs + this.sweepGracePeriodMs) {
            this._state = gcDefinitions_js_1.UnreferencedState.SweepReady;
            return;
        }
        // If the node has been unreferenced long enough, update the state to TombstoneReady.
        // Also, start a timer for the remainder of the sweep delay.
        if (this.tombstoneTimeoutMs !== undefined &&
            unreferencedDurationMs >= this.tombstoneTimeoutMs) {
            this._state = gcDefinitions_js_1.UnreferencedState.TombstoneReady;
            this.sweepTimer.restart(this.tombstoneTimeoutMs + this.sweepGracePeriodMs - unreferencedDurationMs);
            return;
        }
        // If the node has been unreferenced for long enough, update the state to inactive.
        // Also, start a timer for the remainder of the tombstone timeout.
        if (unreferencedDurationMs >= this.inactiveTimeoutMs) {
            this._state = gcDefinitions_js_1.UnreferencedState.Inactive;
            if (this.tombstoneTimeoutMs !== undefined) {
                this.tombstoneTimer.restart(this.tombstoneTimeoutMs - unreferencedDurationMs);
            }
            return;
        }
        // The node is still active. Ensure the inactive timer is running with the proper remaining duration.
        this.inactiveTimer.restart(this.inactiveTimeoutMs - unreferencedDurationMs);
    }
    clearTimers() {
        this.inactiveTimer.clear();
        this.tombstoneTimer.clear();
        this.sweepTimer.clear();
    }
    /** Stop tracking this node. Reset the unreferenced timers and state, if any. */
    stopTracking() {
        this.clearTimers();
        this._state = gcDefinitions_js_1.UnreferencedState.Active;
    }
}
exports.UnreferencedStateTracker = UnreferencedStateTracker;
//# sourceMappingURL=gcUnreferencedStateTracker.js.map