var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for (var i = decorators.length - 1; i >= 0; i--) {
        var context = {};
        for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
        for (var p in contextIn.access) context.access[p] = contextIn.access[p];
        context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
        var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        }
        else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for (var i = 0; i < initializers.length; i++) {
        value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    }
    return useValue ? value : void 0;
};
import * as plugins from '../../plugins.js';
import { CachedDocument, TTL } from '../classes.cached.document.js';
import { DcRouterDb } from '../classes.dcrouter-db.js';
/**
 * Helper to get the smartdata database instance
 */
const getDb = () => DcRouterDb.getInstance().getDb();
/**
 * CachedIPReputation - Stores IP reputation lookup results
 *
 * Caches the results of IP reputation checks to avoid repeated
 * external API calls. Default TTL is 24 hours.
 */
let CachedIPReputation = (() => {
    let _classDecorators = [plugins.smartdata.Collection(() => getDb())];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = CachedDocument;
    let _createdAt_decorators;
    let _createdAt_initializers = [];
    let _createdAt_extraInitializers = [];
    let _expiresAt_decorators;
    let _expiresAt_initializers = [];
    let _expiresAt_extraInitializers = [];
    let _lastAccessedAt_decorators;
    let _lastAccessedAt_initializers = [];
    let _lastAccessedAt_extraInitializers = [];
    let _ipAddress_decorators;
    let _ipAddress_initializers = [];
    let _ipAddress_extraInitializers = [];
    let _score_decorators;
    let _score_initializers = [];
    let _score_extraInitializers = [];
    let _isSpam_decorators;
    let _isSpam_initializers = [];
    let _isSpam_extraInitializers = [];
    let _isProxy_decorators;
    let _isProxy_initializers = [];
    let _isProxy_extraInitializers = [];
    let _isTor_decorators;
    let _isTor_initializers = [];
    let _isTor_extraInitializers = [];
    let _isVPN_decorators;
    let _isVPN_initializers = [];
    let _isVPN_extraInitializers = [];
    let _country_decorators;
    let _country_initializers = [];
    let _country_extraInitializers = [];
    let _asn_decorators;
    let _asn_initializers = [];
    let _asn_extraInitializers = [];
    let _org_decorators;
    let _org_initializers = [];
    let _org_extraInitializers = [];
    let _blacklists_decorators;
    let _blacklists_initializers = [];
    let _blacklists_extraInitializers = [];
    let _checkCount_decorators;
    let _checkCount_initializers = [];
    let _checkCount_extraInitializers = [];
    let _connectionCount_decorators;
    let _connectionCount_initializers = [];
    let _connectionCount_extraInitializers = [];
    let _emailCount_decorators;
    let _emailCount_initializers = [];
    let _emailCount_extraInitializers = [];
    let _spamCount_decorators;
    let _spamCount_initializers = [];
    let _spamCount_extraInitializers = [];
    var CachedIPReputation = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _createdAt_decorators = [plugins.smartdata.svDb()];
            _expiresAt_decorators = [plugins.smartdata.svDb()];
            _lastAccessedAt_decorators = [plugins.smartdata.svDb()];
            _ipAddress_decorators = [plugins.smartdata.unI(), plugins.smartdata.svDb()];
            _score_decorators = [plugins.smartdata.svDb()];
            _isSpam_decorators = [plugins.smartdata.svDb()];
            _isProxy_decorators = [plugins.smartdata.svDb()];
            _isTor_decorators = [plugins.smartdata.svDb()];
            _isVPN_decorators = [plugins.smartdata.svDb()];
            _country_decorators = [plugins.smartdata.svDb()];
            _asn_decorators = [plugins.smartdata.svDb()];
            _org_decorators = [plugins.smartdata.svDb()];
            _blacklists_decorators = [plugins.smartdata.svDb()];
            _checkCount_decorators = [plugins.smartdata.svDb()];
            _connectionCount_decorators = [plugins.smartdata.svDb()];
            _emailCount_decorators = [plugins.smartdata.svDb()];
            _spamCount_decorators = [plugins.smartdata.svDb()];
            __esDecorate(null, null, _createdAt_decorators, { kind: "field", name: "createdAt", static: false, private: false, access: { has: obj => "createdAt" in obj, get: obj => obj.createdAt, set: (obj, value) => { obj.createdAt = value; } }, metadata: _metadata }, _createdAt_initializers, _createdAt_extraInitializers);
            __esDecorate(null, null, _expiresAt_decorators, { kind: "field", name: "expiresAt", static: false, private: false, access: { has: obj => "expiresAt" in obj, get: obj => obj.expiresAt, set: (obj, value) => { obj.expiresAt = value; } }, metadata: _metadata }, _expiresAt_initializers, _expiresAt_extraInitializers);
            __esDecorate(null, null, _lastAccessedAt_decorators, { kind: "field", name: "lastAccessedAt", static: false, private: false, access: { has: obj => "lastAccessedAt" in obj, get: obj => obj.lastAccessedAt, set: (obj, value) => { obj.lastAccessedAt = value; } }, metadata: _metadata }, _lastAccessedAt_initializers, _lastAccessedAt_extraInitializers);
            __esDecorate(null, null, _ipAddress_decorators, { kind: "field", name: "ipAddress", static: false, private: false, access: { has: obj => "ipAddress" in obj, get: obj => obj.ipAddress, set: (obj, value) => { obj.ipAddress = value; } }, metadata: _metadata }, _ipAddress_initializers, _ipAddress_extraInitializers);
            __esDecorate(null, null, _score_decorators, { kind: "field", name: "score", static: false, private: false, access: { has: obj => "score" in obj, get: obj => obj.score, set: (obj, value) => { obj.score = value; } }, metadata: _metadata }, _score_initializers, _score_extraInitializers);
            __esDecorate(null, null, _isSpam_decorators, { kind: "field", name: "isSpam", static: false, private: false, access: { has: obj => "isSpam" in obj, get: obj => obj.isSpam, set: (obj, value) => { obj.isSpam = value; } }, metadata: _metadata }, _isSpam_initializers, _isSpam_extraInitializers);
            __esDecorate(null, null, _isProxy_decorators, { kind: "field", name: "isProxy", static: false, private: false, access: { has: obj => "isProxy" in obj, get: obj => obj.isProxy, set: (obj, value) => { obj.isProxy = value; } }, metadata: _metadata }, _isProxy_initializers, _isProxy_extraInitializers);
            __esDecorate(null, null, _isTor_decorators, { kind: "field", name: "isTor", static: false, private: false, access: { has: obj => "isTor" in obj, get: obj => obj.isTor, set: (obj, value) => { obj.isTor = value; } }, metadata: _metadata }, _isTor_initializers, _isTor_extraInitializers);
            __esDecorate(null, null, _isVPN_decorators, { kind: "field", name: "isVPN", static: false, private: false, access: { has: obj => "isVPN" in obj, get: obj => obj.isVPN, set: (obj, value) => { obj.isVPN = value; } }, metadata: _metadata }, _isVPN_initializers, _isVPN_extraInitializers);
            __esDecorate(null, null, _country_decorators, { kind: "field", name: "country", static: false, private: false, access: { has: obj => "country" in obj, get: obj => obj.country, set: (obj, value) => { obj.country = value; } }, metadata: _metadata }, _country_initializers, _country_extraInitializers);
            __esDecorate(null, null, _asn_decorators, { kind: "field", name: "asn", static: false, private: false, access: { has: obj => "asn" in obj, get: obj => obj.asn, set: (obj, value) => { obj.asn = value; } }, metadata: _metadata }, _asn_initializers, _asn_extraInitializers);
            __esDecorate(null, null, _org_decorators, { kind: "field", name: "org", static: false, private: false, access: { has: obj => "org" in obj, get: obj => obj.org, set: (obj, value) => { obj.org = value; } }, metadata: _metadata }, _org_initializers, _org_extraInitializers);
            __esDecorate(null, null, _blacklists_decorators, { kind: "field", name: "blacklists", static: false, private: false, access: { has: obj => "blacklists" in obj, get: obj => obj.blacklists, set: (obj, value) => { obj.blacklists = value; } }, metadata: _metadata }, _blacklists_initializers, _blacklists_extraInitializers);
            __esDecorate(null, null, _checkCount_decorators, { kind: "field", name: "checkCount", static: false, private: false, access: { has: obj => "checkCount" in obj, get: obj => obj.checkCount, set: (obj, value) => { obj.checkCount = value; } }, metadata: _metadata }, _checkCount_initializers, _checkCount_extraInitializers);
            __esDecorate(null, null, _connectionCount_decorators, { kind: "field", name: "connectionCount", static: false, private: false, access: { has: obj => "connectionCount" in obj, get: obj => obj.connectionCount, set: (obj, value) => { obj.connectionCount = value; } }, metadata: _metadata }, _connectionCount_initializers, _connectionCount_extraInitializers);
            __esDecorate(null, null, _emailCount_decorators, { kind: "field", name: "emailCount", static: false, private: false, access: { has: obj => "emailCount" in obj, get: obj => obj.emailCount, set: (obj, value) => { obj.emailCount = value; } }, metadata: _metadata }, _emailCount_initializers, _emailCount_extraInitializers);
            __esDecorate(null, null, _spamCount_decorators, { kind: "field", name: "spamCount", static: false, private: false, access: { has: obj => "spamCount" in obj, get: obj => obj.spamCount, set: (obj, value) => { obj.spamCount = value; } }, metadata: _metadata }, _spamCount_initializers, _spamCount_extraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            CachedIPReputation = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
            __runInitializers(_classThis, _classExtraInitializers);
        }
        // TTL fields from base class (decorators required on concrete class)
        createdAt = __runInitializers(this, _createdAt_initializers, new Date());
        expiresAt = (__runInitializers(this, _createdAt_extraInitializers), __runInitializers(this, _expiresAt_initializers, new Date(Date.now() + TTL.HOURS_24)));
        lastAccessedAt = (__runInitializers(this, _expiresAt_extraInitializers), __runInitializers(this, _lastAccessedAt_initializers, new Date()));
        /**
         * IP address (unique identifier)
         */
        ipAddress = (__runInitializers(this, _lastAccessedAt_extraInitializers), __runInitializers(this, _ipAddress_initializers, void 0));
        /**
         * Reputation score (0-100, higher = better)
         */
        score = (__runInitializers(this, _ipAddress_extraInitializers), __runInitializers(this, _score_initializers, void 0));
        /**
         * Whether the IP is flagged as spam source
         */
        isSpam = (__runInitializers(this, _score_extraInitializers), __runInitializers(this, _isSpam_initializers, void 0));
        /**
         * Whether the IP is a known proxy
         */
        isProxy = (__runInitializers(this, _isSpam_extraInitializers), __runInitializers(this, _isProxy_initializers, void 0));
        /**
         * Whether the IP is a Tor exit node
         */
        isTor = (__runInitializers(this, _isProxy_extraInitializers), __runInitializers(this, _isTor_initializers, void 0));
        /**
         * Whether the IP is a VPN endpoint
         */
        isVPN = (__runInitializers(this, _isTor_extraInitializers), __runInitializers(this, _isVPN_initializers, void 0));
        /**
         * Country code (ISO 3166-1 alpha-2)
         */
        country = (__runInitializers(this, _isVPN_extraInitializers), __runInitializers(this, _country_initializers, void 0));
        /**
         * Autonomous System Number
         */
        asn = (__runInitializers(this, _country_extraInitializers), __runInitializers(this, _asn_initializers, void 0));
        /**
         * Organization name
         */
        org = (__runInitializers(this, _asn_extraInitializers), __runInitializers(this, _org_initializers, void 0));
        /**
         * List of blacklists the IP appears on
         */
        blacklists = (__runInitializers(this, _org_extraInitializers), __runInitializers(this, _blacklists_initializers, void 0));
        /**
         * Number of times this IP has been checked
         */
        checkCount = (__runInitializers(this, _blacklists_extraInitializers), __runInitializers(this, _checkCount_initializers, 0));
        /**
         * Number of connections from this IP
         */
        connectionCount = (__runInitializers(this, _checkCount_extraInitializers), __runInitializers(this, _connectionCount_initializers, 0));
        /**
         * Number of emails received from this IP
         */
        emailCount = (__runInitializers(this, _connectionCount_extraInitializers), __runInitializers(this, _emailCount_initializers, 0));
        /**
         * Number of spam emails from this IP
         */
        spamCount = (__runInitializers(this, _emailCount_extraInitializers), __runInitializers(this, _spamCount_initializers, 0));
        constructor() {
            super();
            __runInitializers(this, _spamCount_extraInitializers);
            this.setTTL(TTL.HOURS_24); // Default 24-hour TTL
            this.blacklists = [];
            this.score = 50; // Default neutral score
            this.isSpam = false;
            this.isProxy = false;
            this.isTor = false;
            this.isVPN = false;
        }
        /**
         * Create from reputation data
         */
        static fromReputationData(ipAddress, data) {
            const cached = new CachedIPReputation();
            cached.ipAddress = ipAddress;
            cached.score = data.score;
            cached.isSpam = data.isSpam;
            cached.isProxy = data.isProxy;
            cached.isTor = data.isTor;
            cached.isVPN = data.isVPN;
            cached.country = data.country || '';
            cached.asn = data.asn || '';
            cached.org = data.org || '';
            cached.blacklists = data.blacklists || [];
            cached.checkCount = 1;
            return cached;
        }
        /**
         * Convert to reputation data object
         */
        toReputationData() {
            this.touch();
            return {
                score: this.score,
                isSpam: this.isSpam,
                isProxy: this.isProxy,
                isTor: this.isTor,
                isVPN: this.isVPN,
                country: this.country,
                asn: this.asn,
                org: this.org,
                blacklists: this.blacklists,
            };
        }
        /**
         * Find by IP address
         */
        static async findByIP(ipAddress) {
            return await CachedIPReputation.getInstance({
                ipAddress,
            });
        }
        /**
         * Find all IPs flagged as spam
         */
        static async findSpamIPs() {
            return await CachedIPReputation.getInstances({
                isSpam: true,
            });
        }
        /**
         * Find IPs with score below threshold
         */
        static async findLowScoreIPs(threshold) {
            return await CachedIPReputation.getInstances({
                score: { $lt: threshold },
            });
        }
        /**
         * Record a connection from this IP
         */
        recordConnection() {
            this.connectionCount++;
            this.touch();
        }
        /**
         * Record an email from this IP
         */
        recordEmail(isSpam = false) {
            this.emailCount++;
            if (isSpam) {
                this.spamCount++;
            }
            this.touch();
        }
        /**
         * Update the reputation data
         */
        updateReputation(data) {
            this.score = data.score;
            this.isSpam = data.isSpam;
            this.isProxy = data.isProxy;
            this.isTor = data.isTor;
            this.isVPN = data.isVPN;
            this.country = data.country || this.country;
            this.asn = data.asn || this.asn;
            this.org = data.org || this.org;
            this.blacklists = data.blacklists || this.blacklists;
            this.checkCount++;
            this.touch();
            // Refresh TTL on update
            this.setTTL(TTL.HOURS_24);
        }
        /**
         * Check if this IP should be blocked
         */
        shouldBlock() {
            return this.isSpam || this.score < 20 || this.blacklists.length > 2;
        }
    };
    return CachedIPReputation = _classThis;
})();
export { CachedIPReputation };
//# sourceMappingURL=data:application/json;base64, 