{%- capture grich -%}
{%- assign position = 1 %}
<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [{
      "@type": "ListItem",
      "position": {{ position }},
      "name": "{{ 'general.breadcrumb.home' | t }}",
      "item": "{{ shop.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- if template.name == 'product' -%}
    {%- if product.collections.size > 0 -%},{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ collection.title | default: product.collections.first.title | json }},
      "item": "{{ shop.url }}{{ collection.url | default: product.collections.first.url }}"
      {%- assign position = position | plus: 1 -%}
    }{%- endif -%},{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ product.title | json }},
      "item": "{{ shop.url }}{{ product.url | within: collection }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- elsif template.name == 'list-collections' -%},{
      "@type": "ListItem",
      "position": {{ position }},
      "name": "Collections",
      "item": "{{ shop.url }}/collections"
      {%- assign position = position | plus: 1 -%}
    }
    {%- elsif template.name == 'collection' and collection.handle -%},{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ collection.title | json }},
      "item": "{{ shop.url }}{{ collection.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- if current_tags -%}
    {%- for tag in current_tags -%},{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ tag | json }},
      "item": "{{ shop.url }}{{ collection.url }}/{{ tag | handle }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- endfor -%}
    ,{
      "@type": "ListItem",
      "position": {{ position }},
      "name": "{{ page_title }}{%- if current_tags -%}{%- assign meta_tags = current_tags | join: ', ' %} &ndash; {{ 'general.meta.tag' | t: tags: meta_tags -}}{%- endif -%}",
      "item": "{{ canonical_url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- endif -%}
    {%- elsif template.name == 'article' -%}
    ,{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ blog.title | json }},
      "item": "{{ shop.url }}{{ blog.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    ,{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ article.title | json }},
      "item": "{{ shop.url }}{{ article.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- elsif template.name == 'blog' -%}
    ,{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ blog.title | json }},
      "item": "{{ shop.url }}{{ blog.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- elsif template contains 'customers' -%}
    ,{
      "@type": "ListItem",
      "position": {{ position }},
      "name": "{{ 'customer.account.title' | t }}",
      "item": "{{ shop.url }}{{ page.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- else -%}
    {%- unless template.name == 'index' -%}
    ,{
      "@type": "ListItem",
      "position": {{ position }},
      "name": {{ page.title | json }},
      "item": "{{ shop.url }}{{ page.url }}"
      {%- assign position = position | plus: 1 -%}
    }
    {%- endunless -%}
    {%- endif -%}
    ]
  }
</script>

{%- if request.page_type == 'index' -%}
{%- assign potential_action_target = shop.url | append: routes.search_url | append: "?q={search_term_string}" -%}
<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "WebSite",
    "name": {{ shop.name | json }},
    "potentialAction": {
      "@type": "SearchAction",
      "target": {{ potential_action_target | json }},
      "query-input": "required name=search_term_string"
    },
    "url": {{ shop.url | append: page.url | json }}
  }
</script>
{%- endif -%}

{%- if request.page_type == 'product' -%}
<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@id": {{ canonical_url | append: '#product' | json }},
    "@type": "Product",
    {%- if product.vendor != blank %}
    "brand": {
      "@type": "Brand",
      "name": {{ product.vendor | json }}
    },
    {% endif -%}
    {%- if product.selected_or_first_available_variant.sku != blank %}
    "sku": {{ product.selected_or_first_available_variant.sku | json }},
    {% endif -%}
    {%- if product.selected_or_first_available_variant.barcode != blank or product.selected_or_first_available_variant.sku != blank %}
    "mpn": {{ product.selected_or_first_available_variant.barcode | default: product.selected_or_first_available_variant.sku | json }},
    {% endif -%}
    "description": {{ product.description | json }},
    "url": {{ canonical_url | json }},
    "name": {{ product.title | json }},
    {%- if product.featured_image %}
    "image": "https:{{ product.featured_image | image_url: 'grande' }}",
    {% endif -%}
    "itemCondition": "http://schema.org/NewCondition",
    "offers": [
    {% for variant in product.variants %}{
      "@type": "Offer",
      "price": "{{ variant.price | times: 1.0 | divided_by: 100.0 | round: 2 }}",
      "priceCurrency": "{{ shop.currency }}",
      "priceValidUntil": "{{ product.published_at }}",
      "itemCondition": "http://schema.org/NewCondition",
      "url": {{ 'https://' | append: shop.domain | append: variant.url | json }},
      "image": "https:{{ variant.image | default: product.featured_image | image_url: 'grande' }}",
      "sku": {{ variant.sku | json }},
      "mpn": "{{ variant.sku | default: variant.barcode }}",
      "availability": "https://schema.org/{% if variant.available %}InStock{% else %}OutOfStock{% endif %}"
      {%- if variant.barcode.size >= 12 and variant.barcode.size <= 14 %},
      "gtin{{ variant.barcode.size }}": "{{ variant.barcode }}"
      {% endif %}    
    }{%- unless forloop.last -%},{%- endunless -%}
    {%- endfor -%}]

    {%- capture ratingValue -%}
      {{ product.metafields.reviews.rating.value.rating }}
      {{ product.metafields.ryviu.product_reviews_info | split: ";" | last }}
      {{ product.metafields.ali.reviews_average }}
      {{ product.metafields.loox.avg_rating }}
      {{ product.metafields.stamped.reviews_average }}
    {%- endcapture -%}
    {%- capture ratingCount -%}
      {{ product.metafields.reviews.rating_count }}
      {{ product.metafields.ryviu.product_reviews_info | split: ";" | first }}
      {{ product.metafields.ali.reviews_count }}
      {{ product.metafields.loox.num_reviews }}
      {{ product.metafields.stamped.reviews_count }}
    {%- endcapture -%}
    {%- assign ratingValue = ratingValue | strip_newlines | split: " " | uniq | first -%}
    {%- assign ratingCount = ratingCount | strip_newlines | split: " " | uniq | first -%}

    {% comment %} "review": {
      "@type": "Review",
      "reviewRating": {
        "@type": "Rating",
        "ratingValue": {{ product.metafields.reviews.rating_count | default: 0 }},
        "bestRating": {{ product.metafields.reviews.rating.value.scale_max | default: 0 }},
        "worstRating": {{ product.metafields.reviews.rating.value.scale_min | default: 0 }},
      }
    }, {% endcomment %}
    {%- if ratingValue != blank -%},
    "aggregateRating": {
      "@type": "AggregateRating",
      "ratingValue": {{ ratingValue | default: 0 }},
      "ratingCount": {{ ratingCount | default: 0 }}
    }{% endif %}
  }
</script>
{%- endif -%}

{%- if request.page_type == 'article' -%}
<script type="application/ld+json">
  {
    "@context": "https://schema.org", 
    "@type": "BlogPosting",
    "headline": "{{ article.title }}",
    "image": "{{ article.image.src | image_url }}",
    "editor": "{{ article.author }}", 
    "genre": "search engine optimization", 
    "keywords": "{{ article.tags | join: ' ' }}",
    "url": "{{ shop.url }}{{ article.url }}",
    "datePublished": "{{ article.published_at | date: "%Y-%m-%d" }}",
    "dateCreated": "{{ article.created_at | date: "%Y-%m-%d" }}",
    "dateModified": "{{ article.updated_at | date: "%Y-%m-%d" }}",
    "description": "{{ article.excerpt_or_content | strip_html | truncatewords: 20 }}",
    "articleBody": "{{ article.content | strip_html | strip_newlines }}",
    "author": {
      "@type": "{% if article.user.account_owner %}Admin{% else %}Staff{% endif %}",
      "name": "{{ article.author }}"
    }
  }
</script>
{%- endif -%}
{%- endcapture -%}
{{- grich | strip_newlines | remove: '  ' -}}