import logger from "../../../Server/Utils/Logger";
import FilterCondition from "../../Filter/FilterCondition";
import { ConditionType, } from "./NotificationRuleCondition";
export class WorkspaceNotificationRuleUtil {
    static isRuleMatching(data) {
        const notificationRule = data.notificationRule;
        logger.debug("Checking if rule matches for notificationRule:");
        logger.debug(notificationRule);
        // no filters means all filters are matched
        if (data.notificationRule.filters.length === 0) {
            logger.debug("No filters found. Rule matches by default.");
            return true;
        }
        const filterCondition = notificationRule.filterCondition;
        logger.debug("Filter condition:");
        logger.debug(filterCondition);
        for (const filter of notificationRule.filters) {
            const value = data.values[filter.checkOn];
            const condition = filter.conditionType;
            const filterValue = filter.value;
            logger.debug("Evaluating filter:");
            logger.debug(filter);
            logger.debug("Value:");
            logger.debug(value);
            logger.debug("Condition:");
            logger.debug(condition);
            logger.debug("Filter value:");
            logger.debug(filterValue);
            if (!condition) {
                logger.debug("No condition found for filter. Skipping.");
                continue;
            }
            const isMatched = this.didConditionMatch({
                value,
                condition,
                filterValue,
            });
            logger.debug("Filter match result:");
            logger.debug(isMatched);
            if (filterCondition === FilterCondition.All) {
                if (!isMatched) {
                    logger.debug("Filter condition is 'All' and a filter did not match. Rule does not match.");
                    return false;
                }
            }
            if (filterCondition === FilterCondition.Any) {
                if (isMatched) {
                    logger.debug("Filter condition is 'Any' and a filter matched. Rule matches.");
                    return true;
                }
            }
        }
        if (filterCondition === FilterCondition.All) {
            logger.debug("All filters matched. Rule matches.");
            return true;
        }
        if (filterCondition === FilterCondition.Any) {
            logger.debug("No filters matched. Rule does not match.");
            return false;
        }
        logger.debug("No valid filter condition found. Rule does not match.");
        return false;
    }
    static didConditionMatch(data) {
        const value = data.value;
        const condition = data.condition;
        const filterValue = data.filterValue;
        logger.debug("Checking condition match:");
        logger.debug("Value:");
        logger.debug(value);
        logger.debug("Condition:");
        logger.debug(condition);
        logger.debug("Filter value:");
        logger.debug(filterValue);
        if (value === undefined || filterValue === undefined) {
            logger.debug("Value or filter value is undefined. Condition does not match.");
            return false;
        }
        switch (condition) {
            case ConditionType.EqualTo: {
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                const result = values.every((val) => {
                    return filterValues.every((fVal) => {
                        // if val and fVal can be  converted to numbers, compare them as numbers
                        if (!isNaN(Number(val)) && !isNaN(Number(fVal))) {
                            return Number(val) === Number(fVal);
                        }
                        return val === fVal;
                    });
                });
                logger.debug("EqualTo condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.NotEqualTo: {
                const result = Array.isArray(value) && Array.isArray(filterValue)
                    ? !value.every((val) => {
                        return filterValue.includes(val);
                    })
                    : value !== filterValue;
                logger.debug("NotEqualTo condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.GreaterThan: {
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                const result = values.every((val) => {
                    return filterValues.every((fVal) => {
                        // if val and fVal can be  converted to numbers, compare them as numbers
                        if (!isNaN(Number(val)) && !isNaN(Number(fVal))) {
                            return Number(val) > Number(fVal);
                        }
                        return val > fVal;
                    });
                });
                logger.debug("GreaterThan condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.LessThan: {
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                const result = values.every((val) => {
                    return filterValues.every((fVal) => {
                        // if val and fVal can be  converted to numbers, compare them as numbers
                        if (!isNaN(Number(val)) && !isNaN(Number(fVal))) {
                            return Number(val) < Number(fVal);
                        }
                        return val < fVal;
                    });
                });
                logger.debug("LessThan condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.GreaterThanOrEqualTo: {
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                const result = values.every((val) => {
                    return filterValues.every((fVal) => {
                        // if val and fVal can be  converted to numbers, compare them as numbers
                        if (!isNaN(Number(val)) && !isNaN(Number(fVal))) {
                            return Number(val) >= Number(fVal);
                        }
                        return val >= fVal;
                    });
                });
                logger.debug("GreaterThanOrEqualTo condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.LessThanOrEqualTo: {
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                const result = values.every((val) => {
                    return filterValues.every((fVal) => {
                        // if val and fVal can be  converted to numbers, compare them as numbers
                        if (!isNaN(Number(val)) && !isNaN(Number(fVal))) {
                            return Number(val) <= Number(fVal);
                        }
                        return val <= fVal;
                    });
                });
                logger.debug("LessThanOrEqualTo condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.ContainsAny:
            case ConditionType.Contains: {
                const result = false;
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                logger.debug("Values:");
                logger.debug(values);
                logger.debug("Filter values:");
                logger.debug(filterValues);
                for (const val of values) {
                    for (const fVal of filterValues) {
                        if (val.includes(fVal)) {
                            logger.debug("ContainsAny condition result:");
                            logger.debug(true);
                            return true;
                        }
                    }
                }
                logger.debug("ContainsAny condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.NotContains: {
                const result = true;
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                for (const val of values) {
                    for (const fVal of filterValues) {
                        if (val.includes(fVal)) {
                            logger.debug("NotContains condition result:");
                            logger.debug(false);
                            return false;
                        }
                    }
                }
                logger.debug("NotContains condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.StartsWith: {
                const result = false;
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                for (const val of values) {
                    for (const fVal of filterValues) {
                        if (val.startsWith(fVal)) {
                            logger.debug("StartsWith condition result:");
                            logger.debug(true);
                            return true;
                        }
                    }
                }
                logger.debug("StartsWith condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.EndsWith: {
                const result = false;
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                for (const val of values) {
                    for (const fVal of filterValues) {
                        if (val.endsWith(fVal)) {
                            logger.debug("StartsWith condition result:");
                            logger.debug(true);
                            return true;
                        }
                    }
                }
                logger.debug("StartsWith condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.IsEmpty: {
                const result = Array.isArray(value)
                    ? value.length === 0
                    : value === "";
                logger.debug("IsEmpty condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.IsNotEmpty: {
                const result = Array.isArray(value)
                    ? value.length > 0
                    : value !== "";
                logger.debug("IsNotEmpty condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.True: {
                const result = value === "true";
                logger.debug("True condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.False: {
                const result = value === "false";
                logger.debug("False condition result:");
                logger.debug(result);
                return result;
            }
            case ConditionType.ContainsAll: {
                const values = Array.isArray(value) ? value : [value];
                const filterValues = Array.isArray(filterValue)
                    ? filterValue
                    : [filterValue];
                const result = filterValues.every((fVal) => {
                    return values.some((val) => {
                        return val.includes(fVal);
                    });
                });
                logger.debug("ContainsAll condition result:");
                logger.debug(result);
                return result;
            }
            default: {
                logger.debug("Unknown condition type. Condition does not match.");
                return false;
            }
        }
    }
}
//# sourceMappingURL=NotificationRuleUtil.js.map