{% import 'elementor/macros' as m %}
{% set explore = coresocial_explore|default(false) %}
{% if not explore or coresocial_pro|default(false) %}
{% if explore and (settings.use_global_settings ?? true) %}
    {% set settings = settings|merge(coresocial_data.defaults|default({})) %}
{% endif %}
{% set counts_active = settings.share_count_active ?? (not explore) %}
{% set networks = settings.context == 'block' ? coresocial_data.document_networks : coresocial_data.global_networks %}
{% if settings.networks is empty %}
    {% set selection = networks|keys %}
{% elseif settings.networks matches '/^\\[\\s*("[a-zA-Z0-9_-]+"\\s*(,\\s*"[a-zA-Z0-9_-]+"\\s*)*)?\\]$/' %}
    {% set selection = settings.networks|replace({'[':'', ']':'', '"':'', ' ':''})|split(',') %}
{% else %}
    {% set selection = [] %}
{% endif %}
{% if coresocial_pro|default(false) and settings.order_by_count|default(false) %}
    {% set ordered = [] %}
    {% for key in selection %}
        {% if key in networks|keys and key not in ordered %}
            {% set before = [] %}{% set after = [] %}
            {% for existing in ordered %}
                {% if networks[existing].count >= networks[key].count %}{% set before = before|merge([existing]) %}{% else %}{% set after = after|merge([existing]) %}{% endif %}
            {% endfor %}
            {% set ordered = before|merge([key])|merge(after) %}
        {% endif %}
    {% endfor %}
    {% set selection = ordered %}
{% endif %}
{% set layout = settings.layout ?? 'left' %}
{% set color = settings.color ?? 'fill' %}
{% set custom_background = settings.custom_background|default('#102030') %}
{% set custom_text = settings.custom_text|default('#FEFEFE') %}
{% if not (custom_background matches '/^#([0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$|^(rgb|rgba|hsl|hsla)\\([0-9.,%+ \t\\/-]+\\)$/i') %}{% set custom_background = '#102030' %}{% endif %}
{% if not (custom_text matches '/^#([0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$|^(rgb|rgba|hsl|hsla)\\([0-9.,%+ \t\\/-]+\\)$/i') %}{% set custom_text = '#FEFEFE' %}{% endif %}
{% set align = settings.align ?? 'center' %}
{% set styling = settings.styling ?? 'normal' %}
{% set show_label = layout != 'icon' %}
{% set uid = coresocial_uid|default(id) %}
<div class="{{ settings.classes|default([])|join(' ')|e('html_attr') }}" data-interaction-id="{{ interaction_id|e('html_attr') }}"{{ m.render_custom_attributes(settings) }}>
    <div class="coresocial-block-wrapper coresocial-block-{{ explore ? 'explore' : 'share' }} {{ settings.class|default('')|e('html_attr') }}"{% if coresocial_editor|default(false) %} inert{% endif %}>
        <div class="coresocial_share_block coresocial_share_inline __inline_block __layout_{{ layout|e('html_attr') }} __color_{{ color|e('html_attr') }}{% if align != 'none' %} __align_{{ align|e('html_attr') }}{% endif %}{% if styling != 'normal' %} __styling_{{ styling|e('html_attr') }}{% endif %}{% if show_label %} __has_label{% endif %}"
                style="--coresocial-inline-custom-background:{{ custom_background|e('html_attr') }};--coresocial-inline-custom-text:{{ custom_text|e('html_attr') }};--coresocial-inline-font-size:{{ (settings.font_size ?? 16)|number_format(0, '.', '') }}px;--coresocial-inline-button-size:{{ (settings.button_size ?? 40)|number_format(0, '.', '') }}px;--coresocial-inline-button-gap:{{ (settings.button_gap ?? 8)|number_format(0, '.', '') }}px;--coresocial-inline-rounded:{{ (settings.rounded ?? 4)|number_format(0, '.', '') }}px">
            {% set seen = [] %}
            {% set limit = coresocial_pro|default(false) and settings.limit_visible_buttons|default(false) ? max(1, settings.limit_buttons_count|default(5)) : 0 %}
            {% for key in selection %}
                {% if key in networks|keys and key not in seen %}
                    {% if limit and seen|length == limit %}
                        <div class="coresocial_social_network coresocial_network_more"><button type="button" class="coresocial-more" aria-label="{{ coresocial_data.more_label|default('Show more sharing options')|e('html_attr') }}">&#8230;</button></div>
                    {% endif %}
                    {% set is_hidden = limit and seen|length >= limit %}
                    {% set seen = seen|merge([key]) %}
                    {% set network = networks[key] %}
                    {% set show_count = counts_active and (not (settings.share_count_hide_if_zero ?? true) or network.count > 0) %}
                    <div{% if is_hidden %} hidden data-coresocial-reveal style="--coresocial-reveal-index:{{ min(10, seen|length - limit - 1) }}"{% endif %} id="coresocial-{{ uid|e('html_attr') }}-{{ key|e('html_attr') }}" class="coresocial_social_network coresocial_network_{{ network.network|e('html_attr') }}{% if show_label or show_count %} __has_count_or_label{% endif %}{% if not show_label %} __no_label{% endif %}{% if layout == 'right' %} __icon_is_right{% else %} __icon_is_left{% endif %}{% if network.action == 'none' %} __not_active{% endif %}">
                        <a title="{{ (show_label ? network.name : network.label)|e('html_attr') }}" rel="nofollow{% if explore %} noopener noreferrer{% endif %}"{% if explore %} target="_blank" aria-label="{{ network.accessible_label|e('html_attr') }}"{% endif %}{% if not coresocial_editor|default(false) %} href="{{ network.url|e('html_attr') }}"{% endif %} data-network="{{ network.network|e('html_attr') }}" data-item="{{ network.item|e('html_attr') }}" data-action="{{ network.action|e('html_attr') }}" data-module="{{ explore ? 'explore' : 'inline' }}" data-show-count="{{ counts_active ? '1' : '0' }}" data-hide-empty-count="{{ (settings.share_count_hide_if_zero ?? true) ? '1' : '0' }}" data-id="{{ network.item_id|e('html_attr') }}" data-url="{{ network.real|e('html_attr') }}" data-title="{{ network.title|e('html_attr') }}" data-check="{{ network.check|e('html_attr') }}" data-popup-width="{{ network.popup_width|e('html_attr') }}" data-popup-height="{{ network.popup_height|e('html_attr') }}"{% for name,value in network.extra %} data-{{ name|e('html_attr') }}="{{ value|e('html_attr') }}"{% endfor %}>
                            {% if layout != 'right' %}{{ network.icon|raw }}{% endif %}
                            {% if show_label or show_count %}<span>{% if show_label %}<span class="__label">{{ network.label|e }}</span>{% endif %}{% if show_count %}
                                <span class="__count">{{ network.display_count|e }}</span>{% endif %}</span>{% endif %}
                            {% if layout == 'right' %}{{ network.icon|raw }}{% endif %}
                        </a>
                    </div>
                {% endif %}
            {% endfor %}
        </div>
    </div>
</div>
{% endif %}
