import { validateConditionExpression } from "../components/utils/conditionValidation.js";
import { validateActions } from "../components/utils/actionValidation.js";
function validateConditionExpressionForApi(expression, t) {
  if (expression === null || expression === void 0) {
    return { valid: true };
  }
  try {
    const result = validateConditionExpression(expression, 0, 5, t);
    if (result.valid) {
      return { valid: true };
    }
    return {
      valid: false,
      error: `Invalid condition expression: ${result.errors.join("; ")}`,
      errors: result.errors
    };
  } catch (error) {
    return {
      valid: false,
      error: `Failed to validate condition expression: ${error instanceof Error ? error.message : "Unknown error"}`,
      errors: [error instanceof Error ? error.message : "Unknown error"]
    };
  }
}
function validateActionsForApi(actions, fieldName = "actions", t) {
  if (!actions || Array.isArray(actions) && actions.length === 0) {
    return { valid: true };
  }
  if (!Array.isArray(actions)) {
    return {
      valid: false,
      error: `${fieldName} must be an array`,
      errors: [`${fieldName} must be an array`]
    };
  }
  try {
    const result = validateActions(actions, t);
    if (result.valid) {
      return { valid: true };
    }
    return {
      valid: false,
      error: `Invalid ${fieldName}: ${result.errors.join("; ")}`,
      errors: result.errors
    };
  } catch (error) {
    return {
      valid: false,
      error: `Failed to validate ${fieldName}: ${error instanceof Error ? error.message : "Unknown error"}`,
      errors: [error instanceof Error ? error.message : "Unknown error"]
    };
  }
}
function validateRulePayload(payload, t) {
  const errors = [];
  const conditionResult = validateConditionExpressionForApi(payload.conditionExpression, t);
  if (!conditionResult.valid && conditionResult.errors) {
    errors.push(...conditionResult.errors.map((e) => `Condition: ${e}`));
  }
  const successResult = validateActionsForApi(payload.successActions, "successActions", t);
  if (!successResult.valid && successResult.errors) {
    errors.push(...successResult.errors.map((e) => `Success actions: ${e}`));
  }
  const failureResult = validateActionsForApi(payload.failureActions, "failureActions", t);
  if (!failureResult.valid && failureResult.errors) {
    errors.push(...failureResult.errors.map((e) => `Failure actions: ${e}`));
  }
  if (errors.length > 0) {
    return {
      valid: false,
      error: errors.join("; "),
      errors
    };
  }
  return { valid: true };
}
function isSafeExpression(expression) {
  if (!expression) return true;
  const MAX_DEPTH = 10;
  const MAX_RULES_PER_GROUP = 50;
  const MAX_FIELD_PATH_LENGTH = 200;
  function checkDepth(expr, depth) {
    if (depth > MAX_DEPTH) return false;
    if (expr && typeof expr === "object" && "rules" in expr && Array.isArray(expr.rules)) {
      if (expr.rules.length > MAX_RULES_PER_GROUP) return false;
      return expr.rules.every((rule) => checkDepth(rule, depth + 1));
    }
    if (expr && typeof expr === "object" && "field" in expr) {
      const fieldPath = String(expr.field);
      if (fieldPath.length > MAX_FIELD_PATH_LENGTH) return false;
    }
    return true;
  }
  return checkDepth(expression, 0);
}
export {
  isSafeExpression,
  validateActionsForApi,
  validateConditionExpressionForApi,
  validateRulePayload
};
//# sourceMappingURL=payload-validation.js.map
