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 DnsMonitorCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        let threshold = input.criteriaFilter.value;
        const dataToProcess = input.dataToProcess;
        const dnsResponse = dataToProcess.dnsResponse;
        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 DNS is online
        if (input.criteriaFilter.checkOn === CheckOn.DnsIsOnline) {
            const currentIsOnline = overTimeValue ||
                input.dataToProcess.isOnline;
            return CompareCriteria.compareCriteriaBoolean({
                value: currentIsOnline,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check DNS response time
        if (input.criteriaFilter.checkOn === CheckOn.DnsResponseTime) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (threshold === null || threshold === undefined) {
                return null;
            }
            const currentResponseTime = overTimeValue ||
                (dnsResponse === null || dnsResponse === void 0 ? void 0 : dnsResponse.responseTimeInMs) ||
                input.dataToProcess.responseTimeInMs;
            if (currentResponseTime === null || currentResponseTime === undefined) {
                return null;
            }
            return CompareCriteria.compareCriteriaNumbers({
                value: currentResponseTime,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check if DNS record exists
        if (input.criteriaFilter.checkOn === CheckOn.DnsRecordExists) {
            const exists = Boolean((dnsResponse === null || dnsResponse === void 0 ? void 0 : dnsResponse.records) && dnsResponse.records.length > 0);
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (exists && isTrue) {
                return `DNS records exist for the query.`;
            }
            if (!exists && isFalse) {
                return `No DNS records found for the query.`;
            }
            return null;
        }
        // Check DNSSEC validity
        if (input.criteriaFilter.checkOn === CheckOn.DnssecIsValid) {
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if ((dnsResponse === null || dnsResponse === void 0 ? void 0 : dnsResponse.isDnssecValid) === undefined) {
                return null;
            }
            if (dnsResponse.isDnssecValid && isTrue) {
                return `DNSSEC is valid.`;
            }
            if (!dnsResponse.isDnssecValid && isFalse) {
                return `DNSSEC is not valid.`;
            }
            return null;
        }
        // Check DNS record value
        if (input.criteriaFilter.checkOn === CheckOn.DnsRecordValue) {
            if (!(dnsResponse === null || dnsResponse === void 0 ? void 0 : dnsResponse.records) || dnsResponse.records.length === 0) {
                return null;
            }
            // Check if any record value matches the criteria
            for (const record of dnsResponse.records) {
                const recordValue = record.value;
                // Try numeric comparison first
                if (typeof threshold === "number" ||
                    (typeof threshold === "string" && !isNaN(Number(threshold)))) {
                    const numericThreshold = CompareCriteria.convertToNumber(threshold);
                    if (numericThreshold !== null && !isNaN(Number(recordValue))) {
                        const result = CompareCriteria.compareCriteriaNumbers({
                            value: Number(recordValue),
                            threshold: numericThreshold,
                            criteriaFilter: input.criteriaFilter,
                        });
                        if (result) {
                            return `DNS record (${record.type}): ${result}`;
                        }
                    }
                }
                // String comparison
                const result = CompareCriteria.compareCriteriaStrings({
                    value: recordValue,
                    threshold: String(threshold),
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return `DNS record (${record.type}): ${result}`;
                }
            }
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DnsMonitorCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=DnsMonitorCriteria.js.map