const VALID_COMPARISON_OPERATORS = [
  "=",
  "==",
  "!=",
  ">",
  ">=",
  "<",
  "<=",
  "IN",
  "NOT_IN",
  "CONTAINS",
  "NOT_CONTAINS",
  "STARTS_WITH",
  "ENDS_WITH",
  "MATCHES",
  "IS_EMPTY",
  "IS_NOT_EMPTY"
];
const VALID_LOGICAL_OPERATORS = ["AND", "OR", "NOT"];
function isGroupCondition(expr) {
  return expr && typeof expr === "object" && "operator" in expr && "rules" in expr && Array.isArray(expr.rules);
}
function isSimpleCondition(expr) {
  return expr && typeof expr === "object" && "field" in expr && "operator" in expr && "value" in expr;
}
function validateConditionExpression(expr, depth = 0, maxDepth = 5, t) {
  const errors = [];
  const translate = t || ((key) => key);
  if (!expr) {
    return { valid: true, errors: [] };
  }
  if (depth > maxDepth) {
    errors.push(translate("business_rules.validation.condition.maxDepthExceeded", { maxDepth }));
    return { valid: false, errors };
  }
  if (isGroupCondition(expr)) {
    if (!VALID_LOGICAL_OPERATORS.includes(expr.operator)) {
      errors.push(translate("business_rules.validation.condition.invalidLogicalOperator", { operator: expr.operator, validOperators: VALID_LOGICAL_OPERATORS.join(", ") }));
    }
    if (!Array.isArray(expr.rules) || expr.rules.length === 0) {
      errors.push(translate("business_rules.validation.condition.groupMustHaveRules"));
    } else {
      expr.rules.forEach((rule, index) => {
        const result = validateConditionExpression(rule, depth + 1, maxDepth, t);
        if (!result.valid) {
          errors.push(translate("business_rules.validation.condition.ruleError", { index: index + 1, errors: result.errors.join(", ") }));
        }
      });
    }
  } else if (isSimpleCondition(expr)) {
    if (!expr.field || typeof expr.field !== "string") {
      errors.push(translate("business_rules.validation.condition.fieldRequired"));
    } else if (!isValidFieldPath(expr.field)) {
      errors.push(translate("business_rules.validation.condition.invalidFieldPath", { field: expr.field }));
    }
    if (!expr.operator) {
      errors.push(translate("business_rules.validation.condition.operatorRequired"));
    } else if (!VALID_COMPARISON_OPERATORS.includes(expr.operator)) {
      errors.push(translate("business_rules.validation.condition.invalidComparisonOperator", { operator: expr.operator, validOperators: VALID_COMPARISON_OPERATORS.join(", ") }));
    }
    if (expr.value === void 0 && !expr.valueField) {
      errors.push(translate("business_rules.validation.condition.valueRequired"));
    }
    if (expr.valueField && typeof expr.valueField !== "string") {
      errors.push(translate("business_rules.validation.condition.valueFieldMustBeString"));
    }
  } else {
    errors.push(translate("business_rules.validation.condition.invalidStructure"));
  }
  return {
    valid: errors.length === 0,
    errors
  };
}
function isValidFieldPath(path) {
  if (!path || typeof path !== "string") return false;
  return /^[a-zA-Z_][a-zA-Z0-9_.\[\]]*$/.test(path);
}
function getComparisonOperators(t) {
  return [
    { value: "=", label: t("business_rules.validation.operators.equals") },
    { value: "==", label: t("business_rules.validation.operators.equalsStrict") },
    { value: "!=", label: t("business_rules.validation.operators.notEquals") },
    { value: ">", label: t("business_rules.validation.operators.greaterThan") },
    { value: ">=", label: t("business_rules.validation.operators.greaterThanOrEqual") },
    { value: "<", label: t("business_rules.validation.operators.lessThan") },
    { value: "<=", label: t("business_rules.validation.operators.lessThanOrEqual") },
    { value: "IN", label: t("business_rules.validation.operators.in") },
    { value: "NOT_IN", label: t("business_rules.validation.operators.notIn") },
    { value: "CONTAINS", label: t("business_rules.validation.operators.contains") },
    { value: "NOT_CONTAINS", label: t("business_rules.validation.operators.notContains") },
    { value: "STARTS_WITH", label: t("business_rules.validation.operators.startsWith") },
    { value: "ENDS_WITH", label: t("business_rules.validation.operators.endsWith") },
    { value: "MATCHES", label: t("business_rules.validation.operators.matches") },
    { value: "IS_EMPTY", label: t("business_rules.validation.operators.isEmpty") },
    { value: "IS_NOT_EMPTY", label: t("business_rules.validation.operators.isNotEmpty") }
  ];
}
function getLogicalOperators(t) {
  return [
    { value: "AND", label: t("business_rules.validation.logical.and") },
    { value: "OR", label: t("business_rules.validation.logical.or") },
    { value: "NOT", label: t("business_rules.validation.logical.not") }
  ];
}
export {
  getComparisonOperators,
  getLogicalOperators,
  isGroupCondition,
  isSimpleCondition,
  isValidFieldPath,
  validateConditionExpression
};
//# sourceMappingURL=conditionValidation.js.map
