{# Link Previews - Wikipedia-style hover cards #}
{# Self-sufficient: auto-detects if JSON files are available #}
{# Supports cross-site previews for whitelisted hosts (RFC: Cross-Site Link Previews) #}
{% let _link_previews_enabled = site.link_previews.enabled ?? true %}
{% let _per_page_json = 'json' in (config.output_formats.per_page ?? []) %}

{% if _link_previews_enabled %}
<script defer src="{{ asset_url('js/enhancements/link-previews.js') }}"></script>
{% end %}

{# Config bridge: only include when both link previews and per-page JSON are enabled #}
{% if _link_previews_enabled and _per_page_json %}
{% let _lp = site.link_previews %}
<script id="bengal-config" type="application/json">
{
  "linkPreviews": {
    "enabled": {{ _link_previews_enabled | lower }},
    "hoverDelay": {{ _lp.hover_delay ?? 200 }},
    "hideDelay": {{ _lp.hide_delay ?? 150 }},
    "showSection": {{ (_lp.show_section ?? true) | lower }},
    "showReadingTime": {{ (_lp.show_reading_time ?? true) | lower }},
    "showWordCount": {{ (_lp.show_word_count ?? true) | lower }},
    "showDate": {{ (_lp.show_date ?? true) | lower }},
    "showTags": {{ (_lp.show_tags ?? true) | lower }},
    "maxTags": {{ _lp.max_tags ?? 3 }},
    "includeSelectors": {{ (_lp.include_selectors ?? ['.prose']) | tojson }},
    "excludeSelectors": {{ (_lp.exclude_selectors ?? []) | tojson }},
    "allowedHosts": {{ (_lp.allowed_hosts ?? []) | tojson }},
    "allowedSchemes": {{ (_lp.allowed_schemes ?? ['https']) | tojson }},
    "hostFailureThreshold": {{ _lp.host_failure_threshold ?? 3 }},
    "showDeadLinks": {{ (_lp.show_dead_links ?? true) | lower }}
  }
}
</script>
{% end %}
