{%- extends "components/form/row/index.jinja" %}

{% assert form_object is defined, "form_object: form_object must be defined." %}

{% assert form_object.errors is defined, "form_object: errors must be defined." %}
{% assert form_object.relation is defined, "form_object: relation must be defined." %}

{% block row_form_attributes %}
  data-controller="deletable_field movable_field"
  mid="{{ form_object.relation.relation_mid }}"
  data-testid="grammar-form-relation-row"
{% endblock row_form_attributes %}

{% block row_left %}
{# Explicitly nothing. #}
{% endblock row_left %}

{% block row_content scoped %}

  {% set relation_type_errors = form_object.get_errors(form_object.relation.relation_type_input_name()) -%}
  <sdoc-form-field-group
    data-field-label="Relation"
    {% if relation_type_errors|length -%}
      errors="true"
    {%- endif -%}
  >

  {% if
    form_object.relation.relation_type == "Parent"
    or
    form_object.relation.relation_type == "Child"
    or
    form_object.relation.relation_type == "File"
  -%}

    {%- if relation_type_errors|length > 0 -%}
      {%- for error_ in relation_type_errors -%}
      <sdoc-form-error>
        {{ error_ }}
      </sdoc-form-error>
      {%- endfor -%}
    {%- endif -%}

    <sdoc-form-field field-type="select">
      <label for="relation_type_{{ form_object.relation.relation_mid }}">Relation type</label>
        <select
          class="sdoc-form-select"
          name="{{ form_object.relation.relation_type_input_name() }}"
          id="relation_type_{{ form_object.relation.relation_mid }}"
          mid="{{ form_object.relation.relation_mid }}"
          data-testid="select-relation-type"
        >
          <option
            value="Parent"
            {{ "selected" if form_object.relation.relation_type == "Parent" else "" }}
          >Parent</option>
          <option
            value="Child"
            {{ "selected" if form_object.relation.relation_type == "Child" else "" }}
          >Child</option>
          <option
            value="File"
            {{ "selected" if form_object.relation.relation_type == "File" else "" }}
          >File</option>
        </select>
    </sdoc-form-field>

    {%- with
      field_class_name = "monospace",
      field_editable = true,
      field_required = false,
      field_input_name = form_object.relation.relation_role_input_name(),
      field_label = "Relation role:",
      field_placeholder = "Enter relation role here...",
      field_type = "singleline",
      field_value = form_object.relation.relation_role,
      mid = form_object.relation.relation_mid,
      testid_postfix = "relation-role",
      errors=form_object.get_errors(form_object.relation.relation_role_input_name())
    %}
      {%- include "components/form/field/contenteditable/index.jinja" %}
    {%- endwith -%}

  {% endif %}
  </sdoc-form-field-group>
{% endblock row_content %}

{% block row_right %}
  {%- set action_button_context = namespace() %}
  {%- set action_button_context.field_actions = {"delete": true} %}
  {%- set action_button_context.field_name = "relation" %}
  {%- set action_button_context.mid = form_object.relation.relation_mid %}
  {%- set action_button_context.testid_postfix = "relation" %}
  {%- include "components/form/field_action_button/index.jinja" -%}
{% endblock row_right %}
