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, } from "../../../../Types/Monitor/CriteriaFilter";
import logger from "../../Logger";
import CaptureSpan from "../../Telemetry/CaptureSpan";
export default class CustomCodeMonitoringCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        // Server Monitoring Checks
        let threshold = input.criteriaFilter.value;
        const syntheticMonitorResponse = input.monitorResponse;
        if (input.criteriaFilter.checkOn === CheckOn.ExecutionTime) {
            threshold = CompareCriteria.convertToNumber(threshold);
            const currentExecutionTime = syntheticMonitorResponse.executionTimeInMS || 0;
            return CompareCriteria.compareCriteriaNumbers({
                value: currentExecutionTime,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        if (input.criteriaFilter.checkOn === CheckOn.Error) {
            const emptyNotEmptyResult = CompareCriteria.compareEmptyAndNotEmpty({
                value: syntheticMonitorResponse.scriptError,
                criteriaFilter: input.criteriaFilter,
            });
            if (emptyNotEmptyResult) {
                return emptyNotEmptyResult;
            }
            if (threshold &&
                typeof syntheticMonitorResponse.scriptError === "string") {
                const result = CompareCriteria.compareCriteriaStrings({
                    value: syntheticMonitorResponse.scriptError,
                    threshold: threshold.toString(),
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return result;
                }
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.ResultValue) {
            const emptyNotEmptyResult = CompareCriteria.compareEmptyAndNotEmpty({
                value: syntheticMonitorResponse.result,
                criteriaFilter: input.criteriaFilter,
            });
            if (emptyNotEmptyResult) {
                return emptyNotEmptyResult;
            }
            let thresholdAsNumber = null;
            try {
                if (threshold) {
                    thresholdAsNumber = parseFloat(threshold.toString());
                }
            }
            catch (err) {
                logger.error(err);
                thresholdAsNumber = null;
            }
            if (thresholdAsNumber !== null &&
                typeof syntheticMonitorResponse.result === "number") {
                const result = CompareCriteria.compareCriteriaNumbers({
                    value: syntheticMonitorResponse.result,
                    threshold: thresholdAsNumber,
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return result;
                }
            }
            if (threshold && typeof syntheticMonitorResponse.result === "string") {
                const result = CompareCriteria.compareCriteriaStrings({
                    value: syntheticMonitorResponse.result,
                    threshold: threshold.toString(),
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return result;
                }
            }
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], CustomCodeMonitoringCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=CustomCodeMonitorCriteria.js.map