{%- comment -%}
  bugd.one – Meldeknopf und JavaScript-Fehler für Shopify-Themes (https://bugd.one/docs#shopify).

  Wird vom App-Embed-Block „bugd.one“ (blocks/bugdone.liquid) gerendert und funktioniert unverändert
  auch ohne App: als snippets/bugdone.liquid ins Theme kopieren und in layout/theme.liquid vor dem
  schließenden head-Tag per render-Tag einbinden (Parameter project_key, report_button, js_errors,
  environment, button_style, button_position, primary_color, release – siehe README.md).

  Sicherheit: Jeder Wert wird hier geprüft und nur ausgegeben, wenn er ausschließlich aus erlaubten
  Zeichen besteht oder einer festen Auswahl entspricht. Shopify dokumentiert für den json-Filter nur,
  dass er Anführungszeichen maskiert, nicht spitze Klammern oder das Und-Zeichen – deshalb hängt die
  Sicherheit im Skript nicht am json-Filter: ausgegeben werden nur Buchstaben, Ziffern und
  _ - . + / # Leerzeichen. Zeichenprüfung in Blöcken von höchstens 40 Zeichen, weil Shopify für
  for-Schleifen höchstens 50 Durchläufe dokumentiert.
  Keine Kundendaten (kein customer-Objekt), keine Cookies.
{%- endcomment -%}
{%- liquid
  assign bd_ok = false
  assign bd_key = project_key | default: '' | strip
  if bd_key.size >= 20 and bd_key.size <= 158
    assign bd_prefix = bd_key | slice: 0, 8
    if bd_prefix == 'bk_live_'
      assign bd_ok = true
      for bd_i in (0..3)
        assign bd_off = bd_i | times: 40
        assign bd_chars = bd_key | slice: bd_off, 40 | split: ''
        for bd_c in bd_chars
          unless 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-' contains bd_c
            assign bd_ok = false
            break
          endunless
        endfor
      endfor
    endif
  endif

  assign bd_widget = true
  if report_button == false
    assign bd_widget = false
  endif
  assign bd_js = false
  if js_errors == true
    assign bd_js = true
  endif
  if bd_widget == false and bd_js == false
    assign bd_ok = false
  endif
  if request.visual_preview_mode
    assign bd_ok = false
  endif

  assign bd_env = ''
  case environment
    when 'staging'
      assign bd_env = 'staging'
    when 'development'
      assign bd_env = 'development'
  endcase

  assign bd_style = ''
  case button_style
    when 'pill-always'
      assign bd_style = 'pill-always'
    when 'icon'
      assign bd_style = 'icon'
    when 'text'
      assign bd_style = 'text'
    when 'tab'
      assign bd_style = 'tab'
  endcase

  assign bd_position = 'bottom-right'
  if button_position == 'bottom-left'
    assign bd_position = 'bottom-left'
  endif

  assign bd_color = primary_color | default: '' | append: '' | strip | downcase
  assign bd_color_ok = false
  if bd_color.size == 7
    assign bd_hash = bd_color | slice: 0
    if bd_hash == '#'
      assign bd_color_ok = true
      assign bd_chars = bd_color | slice: 1, 6 | split: ''
      for bd_c in bd_chars
        unless '0123456789abcdef' contains bd_c
          assign bd_color_ok = false
          break
        endunless
      endfor
    endif
  endif
  unless bd_color_ok
    assign bd_color = ''
  endunless

  assign bd_release = release | default: '' | strip
  if bd_release.size > 50
    assign bd_release = ''
  endif
  assign bd_release_ok = true
  for bd_i in (0..1)
    assign bd_off = bd_i | times: 40
    assign bd_chars = bd_release | slice: bd_off, 40 | split: ''
    for bd_c in bd_chars
      unless 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._+- ' contains bd_c
        assign bd_release_ok = false
        break
      endunless
    endfor
  endfor
  unless bd_release_ok
    assign bd_release = ''
  endunless

  assign bd_lang = request.locale.iso_code | default: '' | slice: 0, 2 | downcase
  assign bd_locale = ''
  case bd_lang
    when 'de', 'en', 'fr', 'es', 'it', 'pt', 'nl', 'pl'
      assign bd_locale = bd_lang
  endcase

  assign bd_privacy = ''
  if shop.privacy_policy
    assign bd_privacy = shop.privacy_policy.url | default: '' | strip
    assign bd_first = bd_privacy | slice: 0
    if bd_first != '/' or bd_privacy.size > 200
      assign bd_privacy = ''
    endif
    assign bd_privacy_ok = true
    for bd_i in (0..4)
      assign bd_off = bd_i | times: 40
      assign bd_chars = bd_privacy | slice: bd_off, 40 | split: ''
      for bd_c in bd_chars
        unless 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/_.-' contains bd_c
          assign bd_privacy_ok = false
          break
        endunless
      endfor
    endfor
    unless bd_privacy_ok
      assign bd_privacy = ''
    endunless
  endif
-%}
{%- if bd_ok -%}
  {%- if bd_js %}
<script src="https://bugd.one/crash-reporter.umd.js" defer></script>
  {%- endif -%}
  {%- if bd_widget %}
<script src="https://bugd.one/widget.umd.js" defer></script>
  {%- endif %}
<script>
(function () {
  var key = {{ bd_key | json }};
  var host = 'https://bugd.one';
  function start() {
    {%- if bd_js %}
    if (window.BugdoneCrashReporter) {
      var crash = { projectKey: key, host: host };
      {%- if bd_release != '' %}
      crash.release = {{ bd_release | json }};
      {%- endif %}
      {%- if bd_env != '' %}
      crash.environment = {{ bd_env | json }};
      {%- endif %}
      window.BugdoneCrashReporter.initCrashReporter(crash);
    }
    {%- endif %}
    {%- if bd_widget %}
    if (window.Bugdone) {
      var widget = { projectKey: key, host: host, position: {{ bd_position | json }} };
      {%- if bd_style != '' %}
      widget.triggerStyle = {{ bd_style | json }};
      {%- endif %}
      {%- if bd_color != '' %}
      widget.primaryColor = {{ bd_color | json }};
      {%- endif %}
      {%- if bd_locale != '' %}
      widget.locale = {{ bd_locale | json }};
      {%- endif %}
      {%- if bd_env != '' %}
      widget.environment = {{ bd_env | json }};
      {%- endif %}
      {%- if bd_release != '' %}
      widget.appVersion = {{ bd_release | json }};
      {%- endif %}
      {%- if bd_privacy != '' %}
      widget.privacyUrl = window.location.origin + {{ bd_privacy | json }};
      {%- endif %}
      window.Bugdone.initBugdone(widget);
    }
    {%- endif %}
  }
  // Beide Skripte laden mit defer (blockieren den Seitenaufbau nicht) und laufen vor DOMContentLoaded.
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', start);
  } else {
    start();
  }
})();
</script>
{%- endif -%}
