<p class="d-flex gap-4">
    <a href="/api/fetch/{{ path }}" class="text-decoration-none"><i class="fa-solid fa-download"></i> Download</a>
    {% if scratch %}
    <a href="#" class="text-decoration-none link-danger"
        hx-delete="/htmx/delete/{{path}}" hx-confirm="Do you confirm deletion?"
    >
        <i class="fa-solid fa-trash"></i> Delete
    </a>
    {% endif %}
</p>

<table class="table table-sm">
    <!---  Adapt meta to desired output --->
    {% if meta.schunk %}
        {% set shape = '%s items (%d bytes)' % (meta.shape, meta.schunk.nbytes) %}
        {% set _ = meta.__setattr__('shape', shape) %}

        {% set chunks = '%s items (%d bytes)' % (meta.chunks, meta.schunk.chunksize) %}
        {% set _ = meta.__setattr__('chunks', chunks) %}

        {% set cparams = meta.schunk.cparams %}
        {% set schunk = meta.schunk %}
        {% for key, value in cparams %}
            {% if key not in ['filters', 'filters_meta'] %}
                {% set _ = schunk.__setattr__(key, value) %}
            {% endif %}
        {% endfor %}
        {% set _ = schunk.__delattr__('cparams') %}
        {% set _ = meta.__setattr__('schunk', schunk) %}

        {% set blocks = '%s items (%d bytes)' % (meta.blocks, meta.schunk.blocksize) %}
        {% set _ = meta.__setattr__('blocks', blocks) %}

        {% set nbytes_value = '%d (cbytes: %d ; cratio: %.2f)' %
            (meta.schunk.nbytes, meta.schunk.cbytes, meta.schunk.cratio) %}
        {% set _ = meta.schunk.__setattr__('nbytes', nbytes_value) %}
        {% set _ = meta.schunk.__delattr__('cbytes') %}
        {% set _ = meta.schunk.__delattr__('cratio') %}

    {% else %}
        {% if meta.nbytes %}
            {% set nbytes_value = '%d (cbytes: %d ; cratio: %.2f)' %
                (meta.nbytes, meta.cbytes, meta.cratio) %}
            {% set _ = meta.__setattr__('nbytes', nbytes_value) %}
            {% set _ = meta.__delattr__('cbytes') %}
            {% set _ = meta.__delattr__('cratio') %}
            {% set _ = meta.cparams.__delattr__('filters') %}
            {% set _ = meta.cparams.__delattr__('filters_meta') %}
        {% endif %}
    {% endif %}

    {% for key, value in meta %}
            {% if key in ['schunk', 'cparams'] %}
                {% for key2, value2 in meta[key] %}
                    {% if key2 not in ['chunkshape', 'chunksize', 'vlmeta'] %}
                        {% if key2 == 'urlpath' %}
                            {% set key2 = 'cachepath' %}
                        {% endif %}
                        <tr>
                            <th scope="row">{{ key2 }}</th>
                            <td>{{ value2 }}</td>
                        </tr>
                    {% endif %}
                {% endfor %}
            {% else %}
                {% if key == 'urlpath' %}
                    {% set key = 'cachepath' %}
                {% endif %}
                <tr>
                    <th scope="row">{{ key }}</th>
                    <td>{{ value }}</td>
                </tr>
            {% endif %}
    {% endfor %}
</table>

{% if meta.schunk %}
    {% set vlmeta = meta.schunk.vlmeta %}
{% else %}
    {% set vlmeta = meta.vlmeta %}
{% endif %}
{% if vlmeta %}
    <h4>VLmeta (user attributes)</h4>
    <table class="table table-sm">
        {% for key, value in vlmeta.items() %}
            <tr>
                <th scope="row">{{ key }}</th>
                <td>{{ value }}</td>
            </tr>
        {% endfor %}
    </table>
{% endif %}
