{%- set fileAttachments = attachments|filter(a => not (a.mimeType starts with "image/" or a.mimeType starts with "video/")) -%}
{%- set imageAttachments = attachments|filter(a => a.mimeType starts with "image/" or a.mimeType starts with "video/") -%}

{%- set canReply =
    not post.deleted
    and not topic.isLocked
    and currentUser is not null
    and currentUser.hasPermission(permission("CREATE_OWN_POST"))
-%}

{%- set canEdit =
    not post.deleted
    and not topic.isLocked
    and currentUser is not null
    and (
        (
            postAuthor is not null
            and postAuthor.id == currentUser.id
            and postAuthor.hasPermission(permission("EDIT_OWN_POST"))
        )
        or currentUser.hasPermission(permission("EDIT_OTHER_POST"))
    ) -%}

{%- set canDelete =
    not post.deleted
    and currentUser is not null
    and (
        (
            postAuthor is not null
            and postAuthor.id == currentUser.id
            and postAuthor.hasPermission(permission("DELETE_OWN_POST"))
        )
        or currentUser.hasPermission(permission("DELETE_OTHER_POST"))
    ) -%}

{%- set canViewAttachments = currentUser is not null -%}

{%- set your_are_the_author =
    currentUser is not null
    and postAuthor is not null
    and currentUser.id == postAuthor.id
-%}

{%- set is_op =
    topicAuthor is not null
    and postAuthor is not null
    and postAuthor.id == topicAuthor.id
-%}

{% if post.deleted %}
    <div class="post" id="post-{{ post.id }}">
        <div class="post-pfp">
            <div class="media-object" style="width:64px"></div>
        </div>
        <div class="post-body">
            <div class="post-status post-status-danger">
                <svg viewBox="0 0 24 24" class="icon"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/></svg>
                <em>{{ __("This post has been deleted") }}</em>
            </div>
        </div>
    </div>
{% else %}
    <div class="post" data-text="{{ post.content }}" style="overflow: visible;" data-post-id="{{ post.id }}">
        <div id="post-{{ post.id }}" class="post-anchor"></div>
        {% if not hide_pfp %}
            <div class="post-pfp">
                {% if postAuthor %}
                    {% if hide_actions %}
                        <img class="media-object" alt="{{ __("Profile picture") }}" src="?_action=profilepicture&amp;user={{ postAuthor.id|url_encode }}" width="64" height="64">
                    {% else %}
                        <a href="?_action=viewuser&amp;user={{ postAuthor.id|url_encode }}">
                            <img class="media-object" alt="{{ __("Profile picture") }}" src="?_action=profilepicture&amp;user={{ postAuthor.id|url_encode }}" width="64" height="64">
                        </a>
                    {% endif %}
                {% else %}
                    <div class="media-object" style="width:64px;height:64px"></div>
                {% endif %}
            </div>
        {% endif %}
        <div class="post-body" style="overflow: visible;">
            <div class="post-title">
                {% if not hide_actions %}
                    <div class="pull-right title-controls">
                        <a href="#post-{{ post.id }}" class="btn btn-iconic" title="{{ __("Permalink") }}">
                            <svg viewBox="0 0 24 24" class="icon"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
                        </a>
                        {% if canReply %}
                            <button data-post-id="{{ post.id }}" class="btn js-only _reply-post btn-iconic" title="{{ __("Reply to post") }}">
                                <svg viewBox="0 0 24 24" class="icon"><polyline points="9 17 4 12 9 7"/><path d="M20 18v-2a4 4 0 0 0-4-4H4"/></svg>
                            </button>
                        {% endif %}
                        {% if canEdit %}
                            <button data-post-id="{{ post.id }}" class="btn js-only _edit-post btn-iconic" title="{{ __("Edit post") }}">
                                <svg viewBox="0 0 24 24" class="icon"><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/><path d="m15 5 4 4"/></svg>
                            </button>
                        {% endif %}
                        {% if canDelete %}
                            <form action="?_action=deletepost" method="post">
                                <input type="hidden" name="post" value="{{ post.id }}">
                                <button type="submit" class="btn btn-danger btn-iconic" title="{{ __("Delete post") }}">
                                    <svg viewBox="0 0 24 24" class="icon"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/></svg>
                                </button>
                            </form>
                        {% endif %}
                    </div>
                {% endif %}
                {% if postAuthor %}
                    {% if hide_actions %}
                        {{ postAuthor.displayName }}
                    {% else %}
                        <a href="?_action=viewuser&amp;user={{ postAuthor.id|url_encode }}">{{ postAuthor.displayName }}</a>
                        {% if is_op %}
                            <svg viewBox="0 0 24 24" class="icon is-op icon-in-text"><title>{{ __("Created this topic") }}</title><path d="m11 7.601-5.994 8.19a1 1 0 0 0 .1 1.298l.817.818a1 1 0 0 0 1.314.087L15.09 12"/><path d="M16.5 21.174C15.5 20.5 14.372 20 13 20c-2.058 0-3.928 2.356-6 2-2.072-.356-2.775-3.369-1.5-4.5"/><circle cx="16" cy="7" r="5"/></svg>
                        {% endif %}
                    {% endif %}
                    {% if your_are_the_author %}
                        <svg viewBox="0 0 24 24" class="icon is-you icon-in-text"><title>{{ __("You") }}</title><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                    {% endif %}
                {% else %}
                    <em class="text-muted">{{ __("(deleted)") }}</em>
                {% endif %}
                <div class="post-info">
                    <span class="_time">{{ post.postDate.format("c") }}</span>
                    {% if post.edited %}
                        <em class="text-muted">{{ __("(edited)") }}</em>
                    {% endif %}
                </div>
            </div>
            <div class="post-main">
                <div class="post-content">{{ renderPost(post.content) }}</div>
                {% if imageAttachments|length > 0 %}
                    <div class="post-images">
                        {% for attachment in imageAttachments %}
                            {% if hide_actions %}
                                <span class="image-attachment" title="{{ attachment.name }}">
                                    <img class="image-attachment-image" src="?_action=thumb&amp;attachment={{ attachment.id|url_encode }}" alt="" width="100">
                                </span>
                            {% else %}
                                <a class="
                                    image-attachment
                                    attachment
                                    {{ attachment.mimeType starts with "video/" ? "video-attachment" }}
                                " href="?_action=attachment&amp;attachment={{ attachment.id|url_encode }}" title="{{ attachment.name }}" data-attachment-id="{{ attachment.id }}">
                                    <img class="image-attachment-image" src="?_action=thumb&amp;attachment={{ attachment.id|url_encode }}" alt="" width="100">
                                    {% if not canViewAttachments %}
                                        <svg viewBox="0 0 24 24" class="attachment-lock icon"><rect width="18" height="11" x="3" y="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>
                                    {% elseif attachment.mimeType starts with "video/" %}
                                        <svg viewBox="0 0 24 24" class="video-player-icon icon"><circle cx="12" cy="12" r="10"/><polygon points="10 8 16 12 10 16 10 8"/></svg>
                                    {% endif %}
                                </a>
                            {% endif %}
                        {% endfor %}
                    </div>
                {% endif %}
            </div>
            {% if fileAttachments|length > 0 %}
                <div class="post-attachments">
                    <div class="btn-toolbar">
                        <div class="btn-group">
                            {% for attachment in fileAttachments %}
                                {% if hide_actions %}
                                    <button>{{ attachment.name }}</button>
                                {% else %}
                                    <a class="btn attachment" href="?_action=attachment&amp;attachment={{ attachment.id|url_encode }}">
                                        {% if not canViewAttachments %}
                                            <svg viewBox="0 0 24 24" class="icon"><circle cx="12" cy="12" r="10"/><path d="m4.9 4.9 14.2 14.2"/></svg>
                                        {% else %}
                                            <svg viewBox="0 0 24 24" class="icon"><path d="m21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l8.57-8.57A4 4 0 1 1 18 8.84l-8.59 8.57a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>
                                        {% endif %}
                                        <span>{{ attachment.name }}</span>
                                    </a>
                                {% endif %}
                            {% endfor %}
                        </div>
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
{% endif %}