import{RenderIf as t,isLocalEnv as e,baseAssetURL as i,template as r}from"@gem-sdk/core";import a from"../../../grid/components/row/Row.liquid.js";import{LiquidIf as d,If as o}from"@gem-sdk/system";import{getSettingPreloadData as n,getStaticLocale as s}from"../../../helpers.js";import{getData as c,DYNAMIC_PRODUCT_STATUS as p,STATIC_PRODUCT_STATUS as l,PRODUCT_ID_LATEST as u}from"./common/helpers.js";let f=f=>{let{children:m,setting:g,style:y,builderProps:v,rawChildren:_,advanced:$}=f,{isSyncProduct:S,productSetting:I}=g??{},b=I?.hasPreSelected??!1,{rowSetting:j,rowStyles:h,formatVariantId:q,formatProductId:P,productStatus:V,preSelectedOptionIds:k,productHandle:w,advancedStyle:E}=c(f);return r`
    ${r`
      {%- assign gpBkProduct = product -%}
      ${o(V==p,r`
      {%- liquid
        if request.page_type != 'product'
          assign product = all_products['${w}']
          assign productId = '${P}' | times: 1
          if product == empty or product == null
            paginate collections.all.products by 100000
              for item in collections.all.products
                if item.id == productId
                  assign product = item
                endif
              endfor
            endpaginate
          endif
        endif
      -%}
    `,r`
      {%- liquid
        if request.page_type == 'product'
          if '${V}' == '${l}'
            if '${P}' == '${u}'
              paginate collections.all.products by 100000
                assign product = collections.all.products | sort: 'created_at' | reverse | first
              endpaginate
            else
              assign product = all_products['${w}']
              assign productId = '${P}' | times: 1
              if product == empty or product == null
                paginate collections.all.products by 100000
                  for item in collections.all.products
                    if item.id == productId
                      assign product = item
                    endif
                  endfor
                endpaginate
              endif
            endif
          endif
        else
          if '${P}' == '${u}'
            paginate collections.all.products by 100000
              assign product = collections.all.products | sort: 'created_at'| reverse | first
            endpaginate
          else
            assign product = all_products['${w}']
            assign productId = '${P}' | times: 1
            if product == empty or product == null
              paginate collections.all.products by 100000
                for item in collections.all.products
                  if item.id == productId
                    assign product = item
                  endif
                endfor
              endpaginate
            endif
          endif
        endif
      -%}
    `)}
    `}
    ${d("product != empty and product != null",r`
      ${r`
      {%- assign initVariantId = ${q} -%}
      {%- assign product_form_id = 'product-form-' | append: "${v?.uid}" -%}
      ${q?"{%- assign variant = product.variants | where: 'id', initVariantId | first -%}":"{%- assign variant = product.selected_or_first_available_variant -%}"}
      ${q?"{%- assign productSelectedVariant = product.variants | where: 'id', initVariantId | first -%}":"{%- assign productSelectedVariant = product.selected_or_first_available_variant -%}"}
      {%-if productSelectedVariant == empty or productSelectedVariant == null -%}
        {%- assign productSelectedVariant = product.selected_or_first_available_variant -%}
      {%- endif -%}
      {%-if variant == empty or variant == null -%}
        {%- assign variant = product.selected_or_first_available_variant -%}
      {%- endif -%}
    `}
      <gp-product
        data-uid="${v?.uid}"
        data-id="${v?.uid}"
        style="${E}"
        class="${$?.cssClass}"
        gp-context='{"productId": {{ product.id }}, "preSelectedOptionIds": [${k}], "isSyncProduct": "${S}", "hasPreSelected": ${b}, "variantSelected": {{ variant | json | escape }},  "inventory_management": {{ variant.inventory_management | json | escape  }}, "inventory_policy": {{ variant.inventory_policy | json | escape }}, "inventoryQuantity": {{ variant.inventory_quantity | default: 0 }}, "quantity": 1, "formId": "{{ product_form_id }}" }'
        gp-data='{"variantSelected": {{ variant | json | escape }}, "quantity": 1, "productUrl":{{ product.url | json | escape }}, "productHandle":{{ product.handle | json | escape }}, "collectionUrl": {{ collection.url | json | escape }}, "collectionHandle": {{ collection.handle | json | escape }}}'
      >
        <product-form class="product-form">
          {%- form 'product', product, id: product_form_id, class: 'form contents', data-type: 'add-to-cart-form', autocomplete: 'off'  -%}
            <input type="hidden" name="id" value="{{ variant.id }}" />
            <input type="hidden" name="quantity" value="{{ quantity }}" />
            <button type="submit" onclick="return false;" style="display:none;"></button>
            ${a({builderProps:{...v,uid:v?.uid,uidInteraction:`${v?.uid}-row`},style:y,styles:h,setting:j,children:m,rawChildren:_})}
          {%- endform -%}
        </product-form>
      </gp-product>
      {%- assign product = gpBkProduct -%}
    `,r`
      <div class="gp-text-center">${s("Product","product_not_found")}</div>
    `)}
    ${t(e,`<script ${n('class="gps-link" delay',"src")}="{{ 'gp-product-v7-5.js' | asset_url }}" defer="defer"></script>`,`<script ${n('class="gps-link" delay',"src")}="${i}/assets-v2/gp-product-v7-5.js?v={{ shop.metafields.GEMPAGES.ASSETS_VERSION }}" defer="defer"></script>`)}
  `};export{f as default};
