var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import CompareCriteria from "./CompareCriteria";
import { CheckOn, FilterType, } from "../../../../Types/Monitor/CriteriaFilter";
import EvaluateOverTime from "./EvaluateOverTime";
import CaptureSpan from "../../Telemetry/CaptureSpan";
import logger from "../../Logger";
export default class SnmpMonitorCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        var _a, _b, _c, _d;
        let threshold = input.criteriaFilter.value;
        const dataToProcess = input.dataToProcess;
        const snmpResponse = dataToProcess.snmpResponse;
        let overTimeValue = undefined;
        if (input.criteriaFilter.evaluateOverTime &&
            input.criteriaFilter.evaluateOverTimeOptions) {
            try {
                overTimeValue = await EvaluateOverTime.getValueOverTime({
                    projectId: input.dataToProcess.projectId,
                    monitorId: input.dataToProcess.monitorId,
                    evaluateOverTimeOptions: input.criteriaFilter.evaluateOverTimeOptions,
                    metricType: input.criteriaFilter.checkOn,
                });
                if (Array.isArray(overTimeValue) && overTimeValue.length === 0) {
                    overTimeValue = undefined;
                }
            }
            catch (err) {
                logger.error(`Error in getting over time value for ${input.criteriaFilter.checkOn}`);
                logger.error(err);
                overTimeValue = undefined;
            }
        }
        // Check if SNMP device is online
        if (input.criteriaFilter.checkOn === CheckOn.SnmpIsOnline) {
            const currentIsOnline = overTimeValue ||
                input.dataToProcess.isOnline;
            return CompareCriteria.compareCriteriaBoolean({
                value: currentIsOnline,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check SNMP response time
        if (input.criteriaFilter.checkOn === CheckOn.SnmpResponseTime) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (threshold === null || threshold === undefined) {
                return null;
            }
            const currentResponseTime = overTimeValue ||
                (snmpResponse === null || snmpResponse === void 0 ? void 0 : snmpResponse.responseTimeInMs) ||
                input.dataToProcess.responseTimeInMs;
            if (currentResponseTime === null || currentResponseTime === undefined) {
                return null;
            }
            return CompareCriteria.compareCriteriaNumbers({
                value: currentResponseTime,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check if a specific OID exists (returns a value)
        if (input.criteriaFilter.checkOn === CheckOn.SnmpOidExists) {
            const oid = (_a = input.criteriaFilter.snmpMonitorOptions) === null || _a === void 0 ? void 0 : _a.oid;
            if (!oid) {
                return null;
            }
            const oidResponse = (_b = snmpResponse === null || snmpResponse === void 0 ? void 0 : snmpResponse.oidResponses) === null || _b === void 0 ? void 0 : _b.find((response) => {
                return response.oid === oid;
            });
            const exists = Boolean(oidResponse && oidResponse.value !== null);
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (exists && isTrue) {
                return `SNMP OID ${oid} exists and returned a value.`;
            }
            if (!exists && isFalse) {
                return `SNMP OID ${oid} does not exist or returned no value.`;
            }
            return null;
        }
        // Check the value of a specific OID
        if (input.criteriaFilter.checkOn === CheckOn.SnmpOidValue) {
            const oid = (_c = input.criteriaFilter.snmpMonitorOptions) === null || _c === void 0 ? void 0 : _c.oid;
            if (!oid) {
                return null;
            }
            const oidResponse = (_d = snmpResponse === null || snmpResponse === void 0 ? void 0 : snmpResponse.oidResponses) === null || _d === void 0 ? void 0 : _d.find((response) => {
                return response.oid === oid;
            });
            if (!oidResponse || oidResponse.value === null) {
                return null;
            }
            const oidValue = oidResponse.value;
            // Numeric comparison
            if (typeof oidValue === "number" || !isNaN(Number(oidValue))) {
                const numericValue = typeof oidValue === "number" ? oidValue : Number(oidValue);
                const numericThreshold = CompareCriteria.convertToNumber(threshold);
                if (numericThreshold !== null) {
                    const result = CompareCriteria.compareCriteriaNumbers({
                        value: numericValue,
                        threshold: numericThreshold,
                        criteriaFilter: input.criteriaFilter,
                    });
                    if (result) {
                        return `SNMP OID ${oid} (${oidResponse.name || oid}): ${result}`;
                    }
                }
            }
            // String comparison
            const result = CompareCriteria.compareCriteriaStrings({
                value: String(oidValue),
                threshold: String(threshold),
                criteriaFilter: input.criteriaFilter,
            });
            if (result) {
                return `SNMP OID ${oid} (${oidResponse.name || oid}): ${result}`;
            }
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SnmpMonitorCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=SnmpMonitorCriteria.js.map