import * as plugins from '../plugins.js';
/**
 * Base class for all cached documents with TTL support
 *
 * Extends smartdata's SmartDataDbDoc to add:
 * - Automatic timestamps (createdAt, lastAccessedAt)
 * - TTL/expiration support (expiresAt)
 * - Helper methods for TTL management
 *
 * NOTE: Subclasses MUST add @svDb() decorators to createdAt, expiresAt, and lastAccessedAt
 * since decorators on abstract classes don't propagate correctly.
 */
export class CachedDocument extends plugins.smartdata.SmartDataDbDoc {
    /**
     * Timestamp when the document was created
     * NOTE: Subclasses must add @svDb() decorator
     */
    createdAt = new Date();
    /**
     * Timestamp when the document expires and should be cleaned up
     * NOTE: Subclasses must add @svDb() decorator
     */
    expiresAt;
    /**
     * Timestamp of last access (for LRU-style eviction if needed)
     * NOTE: Subclasses must add @svDb() decorator
     */
    lastAccessedAt = new Date();
    /**
     * Set the TTL (time to live) for this document
     * @param ttlMs Time to live in milliseconds
     */
    setTTL(ttlMs) {
        this.expiresAt = new Date(Date.now() + ttlMs);
    }
    /**
     * Set TTL using days
     * @param days Number of days until expiration
     */
    setTTLDays(days) {
        this.setTTL(days * 24 * 60 * 60 * 1000);
    }
    /**
     * Set TTL using hours
     * @param hours Number of hours until expiration
     */
    setTTLHours(hours) {
        this.setTTL(hours * 60 * 60 * 1000);
    }
    /**
     * Check if this document has expired
     */
    isExpired() {
        if (!this.expiresAt) {
            return false; // No expiration set
        }
        return new Date() > this.expiresAt;
    }
    /**
     * Update the lastAccessedAt timestamp
     */
    touch() {
        this.lastAccessedAt = new Date();
    }
    /**
     * Get remaining TTL in milliseconds
     * Returns 0 if expired, -1 if no expiration set
     */
    getRemainingTTL() {
        if (!this.expiresAt) {
            return -1;
        }
        const remaining = this.expiresAt.getTime() - Date.now();
        return remaining > 0 ? remaining : 0;
    }
    /**
     * Extend the TTL by the specified milliseconds from now
     * @param ttlMs Additional time to live in milliseconds
     */
    extendTTL(ttlMs) {
        this.expiresAt = new Date(Date.now() + ttlMs);
    }
    /**
     * Set the document to never expire (100 years in the future)
     */
    setNeverExpires() {
        this.expiresAt = new Date(Date.now() + 100 * 365 * 24 * 60 * 60 * 1000);
    }
}
/**
 * TTL constants in milliseconds
 */
export const TTL = {
    HOURS_1: 1 * 60 * 60 * 1000,
    HOURS_24: 24 * 60 * 60 * 1000,
    DAYS_7: 7 * 24 * 60 * 60 * 1000,
    DAYS_30: 30 * 24 * 60 * 60 * 1000,
    DAYS_90: 90 * 24 * 60 * 60 * 1000,
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xhc3Nlcy5jYWNoZWQuZG9jdW1lbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi90cy9kYi9jbGFzc2VzLmNhY2hlZC5kb2N1bWVudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLGVBQWUsQ0FBQztBQUV6Qzs7Ozs7Ozs7OztHQVVHO0FBQ0gsTUFBTSxPQUFnQixjQUE0QyxTQUFRLE9BQU8sQ0FBQyxTQUFTLENBQUMsY0FBb0I7SUFDOUc7OztPQUdHO0lBQ0ksU0FBUyxHQUFTLElBQUksSUFBSSxFQUFFLENBQUM7SUFFcEM7OztPQUdHO0lBQ0ksU0FBUyxDQUFRO0lBRXhCOzs7T0FHRztJQUNJLGNBQWMsR0FBUyxJQUFJLElBQUksRUFBRSxDQUFDO0lBRXpDOzs7T0FHRztJQUNJLE1BQU0sQ0FBQyxLQUFhO1FBQ3pCLElBQUksQ0FBQyxTQUFTLEdBQUcsSUFBSSxJQUFJLENBQUMsSUFBSSxDQUFDLEdBQUcsRUFBRSxHQUFHLEtBQUssQ0FBQyxDQUFDO0lBQ2hELENBQUM7SUFFRDs7O09BR0c7SUFDSSxVQUFVLENBQUMsSUFBWTtRQUM1QixJQUFJLENBQUMsTUFBTSxDQUFDLElBQUksR0FBRyxFQUFFLEdBQUcsRUFBRSxHQUFHLEVBQUUsR0FBRyxJQUFJL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