{#- -*- mode: sgml; tab-width: 1; indent-tabs-mode: nil -*-
 #
 # SPDX-License-Identifier: MIT
-#}

{%- import('macros/printElementId.xml.jinja2') as peid %}
{%- import('macros/printAdminData.xml.jinja2') as pad %}
{%- import('macros/printSpecialData.xml.jinja2') as psd %}

{%- macro printUnitSpec(spec) -%}
<UNIT-SPEC>
 {%- if spec.admin_data is not none %}
 {{ pad.printAdminData(spec.admin_data)|indent(1) }}
 {%- endif %}
 {%- if spec.unit_groups %}
 <UNIT-GROUPS>
 {%- for group in spec.unit_groups %}
  {{ printUnitGroup(group)|indent(2) }}
 {%- endfor %}
 </UNIT-GROUPS>
 {%- endif %}
 {%- if spec.units %}
 <UNITS>
 {%- for unit in spec.units %}
  {{ printUnit(unit)|indent(2) }}
 {%- endfor %}
 </UNITS>
 {%- endif %}
 {%- if spec.physical_dimensions %}
 <PHYSICAL-DIMENSIONS>
 {%- for dim in spec.physical_dimensions %}
  {{ printPhysicalDimesion(dim)|indent(2) }}
 {%- endfor %}
 </PHYSICAL-DIMENSIONS>
 {%- endif %}
 {{- psd.printSpecialDataGroups(spec.sdgs)|indent(1, first=True) }}
</UNIT-SPEC>
{%- endmacro -%}


{%- macro printUnit(unit) -%}
<UNIT {{-peid.printElementIdAttribs(unit)}}>
 {{ peid.printElementIdSubtags(unit)|indent(1) }}
 <DISPLAY-NAME>{{ unit.display_name }}</DISPLAY-NAME>
 {%- if unit.factor_si_to_unit is not none %}
 <FACTOR-SI-TO-UNIT>{{ unit.factor_si_to_unit }}</FACTOR-SI-TO-UNIT>
 {%- endif %}
 {%- if unit.offset_si_to_unit is not none %}
 <OFFSET-SI-TO-UNIT>{{ unit.offset_si_to_unit }}</OFFSET-SI-TO-UNIT>
 {%- endif %}
 {%- if unit.physical_dimension_ref %}
 <PHYSICAL-DIMENSION-REF ID-REF="{{ unit.physical_dimension_ref.ref_id }}" />
 {%- endif %}
</UNIT>
{%- endmacro -%}

{%- macro printUnitGroup(group) -%}
<UNIT-GROUP {{-make_xml_attrib("OID", group.oid)}}>
 {{ peid.printElementIdSubtags(group)|indent(1) }}
 <CATEGORY>{{ group.category.value }}</CATEGORY>
 {%- if group.unit_refs %}
 <UNIT-REFS>
 {%- for ref in group.unit_refs %}
  <UNIT-REF ID-REF="{{ ref.ref_id }}" />
 {%- endfor %}
 </UNIT-REFS>
 {%- endif %}
</UNIT-GROUP>
{%- endmacro -%}

{%- macro printPhysicalDimesion(dim) -%}
<PHYSICAL-DIMENSION {{-peid.printElementIdAttribs(dim)}}>
 {{ peid.printElementIdSubtags(dim)|indent(1) }}
 {%- if dim.length_exp %}
 <LENGTH-EXP>{{ dim.length_exp }}</LENGTH-EXP>
 {%- endif %}
 {%- if dim.mass_exp %}
 <MASS-EXP>{{ dim.mass_exp }}</MASS-EXP>
 {%- endif %}
 {%- if dim.time_exp %}
 <TIME-EXP>{{ dim.time_exp }}</TIME-EXP>
 {%- endif %}
 {%- if dim.current_exp %}
 <CURRENT-EXP>{{ dim.current_exp }}</CURRENT-EXP>
 {%- endif %}
 {%- if dim.temperature_exp %}
 <TEMPERATURE-EXP>{{ dim.temperature_exp }}</TEMPERATURE-EXP>
 {%- endif %}
 {%- if dim.molar_amount_exp %}
 <MOLAR-AMOUNT-EXP>{{ dim.molar_amount_exp }}</MOLAR-AMOUNT-EXP>
 {%- endif %}
 {%- if dim.luminous_intensity_exp %}
 <LUMINOUS-INTENSITY-EXP>{{ dim.luminous_intensity_exp }}</LUMINOUS-INTENSITY-EXP>
 {%- endif %}
</PHYSICAL-DIMENSION>
{%- endmacro -%}
