{% macro context_source_return_url(add_context_data, grant, db_form, enum) %}
  {% if add_context_data.field == "component" %}
    {% if not add_context_data.component_id %}
      {% set return_url = url_for("deliver_grant_funding.add_question", grant_id=grant.id, form_id=db_form.id, parent_id=add_context_data.parent_id, question_data_type=add_context_data.data_type.name) %}
    {% else %}
      {% set return_url = url_for("deliver_grant_funding.edit_question", grant_id=grant.id, question_id=add_context_data.component_id) %}
    {% endif %}
  {% elif add_context_data.field == "guidance" %}
    {% if add_context_data.is_add_another_guidance %}
      {% set return_url = url_for("deliver_grant_funding.manage_add_another_guidance", grant_id=grant.id, group_id=add_context_data.component_id) %}
    {% else %}
      {% set return_url = url_for("deliver_grant_funding.manage_guidance", grant_id=grant.id, question_id=add_context_data.component_id) %}
    {% endif %}
  {% elif add_context_data.field == "condition_depends_on" %}
    {% set return_url = url_for("deliver_grant_funding.add_question_condition_select_question", grant_id=grant.id, component_id=add_context_data.component_id) %}
  {% elif add_context_data.field == enum.expression_type.CONDITION %}
    {% if not add_context_data.expression_id %}
      {% if add_context_data.is_custom == true %}
        {% set return_url = url_for("deliver_grant_funding.add_calculated_condition", grant_id=grant.id, component_id=add_context_data.component_id) %}
      {% else %}
        {% set return_url = url_for("deliver_grant_funding.add_question_condition",grant_id=grant.id, component_id=add_context_data.component_id, subject_reference=add_context_data.subject_reference) %}
      {% endif %}
    {% else %}
      {% if add_context_data.is_custom == true %}
        {% set return_url = url_for("deliver_grant_funding.edit_calculated_condition", grant_id=grant.id, expression_id=add_context_data.expression_id) %}
      {% else %}
        {% set return_url = url_for("deliver_grant_funding.edit_question_condition", grant_id=grant.id, expression_id=add_context_data.expression_id) %}
      {% endif %}
    {% endif %}
  {% elif add_context_data.field == enum.expression_type.VALIDATION %}
    {% if add_context_data.is_group == true %}
      {% if not add_context_data.expression_id %}
        {% set return_url = url_for("deliver_grant_funding.add_group_validation", grant_id=grant.id, group_id=add_context_data.component_id) %}
      {% else %}
        {% set return_url = url_for("deliver_grant_funding.edit_group_validation", grant_id=grant.id, group_id=add_context_data.component_id, expression_id=add_context_data.expression_id) %}
      {% endif %}
    {% elif not add_context_data.expression_id %}
      {% if add_context_data.is_custom == true %}
        {% set return_url = url_for("deliver_grant_funding.add_custom_question_validation", grant_id=grant.id, question_id=add_context_data.component_id) %}
      {% else %}
        {% set return_url = url_for("deliver_grant_funding.add_question_validation", grant_id=grant.id, question_id=add_context_data.component_id) %}
      {% endif %}
    {% else %}
      {% if add_context_data.is_custom == true %}
        {% set return_url = url_for("deliver_grant_funding.edit_custom_question_validation", grant_id=grant.id, question_id=add_context_data.component_id, expression_id=add_context_data.expression_id) %}
      {% else %}
        {% set return_url = url_for("deliver_grant_funding.edit_question_validation", grant_id=grant.id, expression_id=add_context_data.expression_id) %}
      {% endif %}
    {% endif %}
  {% endif %}
  {{ return_url }}
{% endmacro %}
