{% comment %}
  Optional JSON-LD fragment for Offer list / strikethrough price (Google Merchant / ads).

  Accepts:
  - variant: {Object} required
  - product: {Object} required (for price_visibility check)

  When product.metafields.custom.price_visibility has a value:
  - ListPrice prefers variant.custom.amazon_ebay_price (MAP), then product-level same key,
    then Shopify compare_at_price only as fallback.
  - Omitted if no list value or list price is not greater than variant.price.

  Parsing notes:
  - Do not call money_without_currency on plain text MAP values first; Shopify can return a
    partial fragment (e.g. from "89.990"), which parses tiny, fails sanity, and falls back to compare_at.
  - Money JSON: use .amount / guarded .amount_cents first; money_without_currency only for money type.
  - String / single-line MAP (e.g. "89.990"): coerce via strip + remove $/commas + plus (never value|plus on Money drops).
{% endcomment %}
{%- liquid
  assign show_list_spec = false
  assign price_visibility_enabled = false
  if product != blank and product.metafields.custom.price_visibility != blank
    assign price_visibility_enabled = true
  endif

  if price_visibility_enabled
    assign variant_price_major = variant.price | divided_by: 100.0
    assign list_major = 'unset'

    assign ae_mf = variant.metafields.custom.amazon_ebay_price
    if ae_mf == blank and product != blank
      assign ae_mf = product.metafields.custom.amazon_ebay_price
    endif

    if ae_mf != blank
      assign mftype = ''
      if ae_mf.type != blank
        assign mftype = ae_mf.type | downcase
      endif

      if ae_mf.value != blank
        assign aev = ae_mf.value

        if aev.amount != blank
          assign list_major = aev.amount | plus: 0.0
        elsif aev.amount_cents != blank
          assign cents_val = aev.amount_cents | plus: 0
          assign from_cents = cents_val | divided_by: 100.0
          assign min_cents_for_map = variant.price | times: 0.5
          if cents_val >= min_cents_for_map or cents_val >= 1000
            assign list_major = from_cents
          endif
        endif

        if list_major == 'unset' and mftype contains 'decimal'
          assign list_major = aev | plus: 0.0
        elsif list_major == 'unset' and mftype contains 'integer'
          assign list_major = aev | plus: 0.0
        endif

        if list_major == 'unset' and aev.amount == blank
          assign raw_num = aev | append: '' | strip | remove: ',' | remove: '$' | remove: ' '
          if raw_num != blank
            assign list_major = raw_num | plus: 0.0
          endif
        endif
      endif

      if list_major == 'unset' and mftype contains 'money'
        assign mf_as_money = ae_mf | money_without_currency | strip | remove: ',' | remove: '$' | remove: ' '
        if mf_as_money != blank
          assign list_major = mf_as_money | plus: 0.0
        endif
      endif

      unless list_major == 'unset'
        assign list_major = list_major | plus: 0.0
        assign amazon_floor = variant_price_major | times: 0.015
        if amazon_floor < 0.5
          assign amazon_floor = 0.5
        endif
        unless list_major >= amazon_floor or variant.price < 1000
          assign list_major = 'unset'
        endunless
      endunless
    endif

    if list_major == 'unset' and variant.compare_at_price != blank and variant.compare_at_price > 0
      assign list_major = variant.compare_at_price | divided_by: 100.0
    endif

    unless list_major == 'unset'
      assign list_cents_trunc = list_major | times: 100.0 | floor
      assign list_major = list_cents_trunc | divided_by: 100.0

      assign sell_cents_trunc = variant_price_major | times: 100.0 | floor
      assign sell_major = sell_cents_trunc | divided_by: 100.0

      if list_major > sell_major
        assign show_list_spec = true
      endif
    endunless
  endif
-%}
{%- if show_list_spec -%}
"priceSpecification": {
              "@type": "UnitPriceSpecification",
              "priceType": "https://schema.org/ListPrice",
              "price": {{ list_major | json }},
              "priceCurrency": {{ cart.currency.iso_code | json }}
            }
{%- endif -%}
