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 CaptureSpan from "../../Telemetry/CaptureSpan";
export default class DomainMonitorCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        let threshold = input.criteriaFilter.value;
        const dataToProcess = input.dataToProcess;
        const domainResponse = dataToProcess.domainResponse;
        // Check domain expires in days
        if (input.criteriaFilter.checkOn === CheckOn.DomainExpiresDaysIn) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (threshold === null || threshold === undefined) {
                return null;
            }
            if (!(domainResponse === null || domainResponse === void 0 ? void 0 : domainResponse.expiresDate)) {
                return null;
            }
            const expiresDate = new Date(domainResponse.expiresDate);
            const now = new Date();
            const diffMs = expiresDate.getTime() - now.getTime();
            const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
            return CompareCriteria.compareCriteriaNumbers({
                value: diffDays,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check domain registrar
        if (input.criteriaFilter.checkOn === CheckOn.DomainRegistrar) {
            if (!(domainResponse === null || domainResponse === void 0 ? void 0 : domainResponse.registrar)) {
                return null;
            }
            return CompareCriteria.compareCriteriaStrings({
                value: domainResponse.registrar,
                threshold: String(threshold),
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check domain name server
        if (input.criteriaFilter.checkOn === CheckOn.DomainNameServer) {
            if (!(domainResponse === null || domainResponse === void 0 ? void 0 : domainResponse.nameServers) ||
                domainResponse.nameServers.length === 0) {
                return null;
            }
            // Check if any name server matches the criteria
            for (const nameServer of domainResponse.nameServers) {
                const result = CompareCriteria.compareCriteriaStrings({
                    value: nameServer,
                    threshold: String(threshold),
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return `Domain name server: ${result}`;
                }
            }
            return null;
        }
        // Check domain status code
        if (input.criteriaFilter.checkOn === CheckOn.DomainStatusCode) {
            if (!(domainResponse === null || domainResponse === void 0 ? void 0 : domainResponse.domainStatus) ||
                domainResponse.domainStatus.length === 0) {
                return null;
            }
            // Check if any status matches the criteria
            for (const status of domainResponse.domainStatus) {
                const result = CompareCriteria.compareCriteriaStrings({
                    value: status,
                    threshold: String(threshold),
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return `Domain status: ${result}`;
                }
            }
            return null;
        }
        // Check if domain is expired
        if (input.criteriaFilter.checkOn === CheckOn.DomainIsExpired) {
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (!(domainResponse === null || domainResponse === void 0 ? void 0 : domainResponse.expiresDate)) {
                return null;
            }
            const expiresDate = new Date(domainResponse.expiresDate);
            const now = new Date();
            const isExpired = expiresDate.getTime() < now.getTime();
            if (isExpired && isTrue) {
                return `Domain is expired (expired on ${domainResponse.expiresDate}).`;
            }
            if (!isExpired && isFalse) {
                return `Domain is not expired (expires on ${domainResponse.expiresDate}).`;
            }
            return null;
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DomainMonitorCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=DomainMonitorCriteria.js.map