import{RenderIf as e,isLocalEnv as s,baseAssetURL as t,template as i}from"@gem-sdk/core";import{getSettingPreloadData as d,getStaticLocale as o}from"../../../helpers.js";import{If as r}from"@gem-sdk/system";import a from"./ProductListCarouselLayout.liquid.js";import n from"./ProductListGridLayout.liquid.js";import{getStyle as c}from"./common/styles.js";import{getClass as l}from"./common/classes.js";import{getDataLiquid as u}from"./common/helpers.js";let g=g=>{let{setting:p,styles:f,pageContext:m}=g,$=m?.pageType==="GP_FUNNEL_PAGE";if($)return"";let{hasRelatedExclude:P,relatedExcludeTrim:h,collectionID:_,isLatestProducts:y,isGrid:v,numberOfProducts:E,productListId:k,productListHandles:x,sortedBy:S,isReverse:b,productSrc:A,productNumber:L,isSorted:R,relatedTag:I,collectionHandle:F}=u(g),j=()=>i`
    ${e("RelatedProduct"===A,`
        assign relatedTag = '${I}'
        assign assignProduct = product
        assign idAssignProduct = assignProduct.id
        paginate collections.all.products by 10000
          assign allProductsPaginated = collections.all.products
        endpaginate
        assign handleAssignProduct = assignProduct.handle
        case relatedTag
          when 'collection'
            assign hasRelatedExclude = ${P}
            if hasRelatedExclude
              assign relatedExclude = '${h}'
            else
              assign relatedExclude = ''
            endif
            assign arrayRelatedExclude = relatedExclude | split: ","
            assign idSelectedList = ''
            assign relatedListProduct = null
            for collection in collections
            assign collectionId = collection.id
            assign isInCollection = false
            assign collectionsAssignProduct = assignProduct.collections
              for item in collectionsAssignProduct
                unless arrayRelatedExclude contains item.handle
                  if item.id == collectionId
                  for product in collection.products
                    assign product_ids_array = idSelectedList | split: ","
                    if product_ids_array contains product.id or product.id == idAssignProduct
                    elsif product_ids_array.size > limit
                     break
                    else
                      assign idSelectedList = idSelectedList | append: "," | append: product.id
                      assign productSelected = product | sort
                      assign relatedListProduct = relatedListProduct | concat: productSelected
                    endif
                  endfor
                  endif
                endunless
              endfor
            endfor
            assign products = relatedListProduct | uniq
          when 'vendor'
            assign vendorAssignProduct = assignProduct.vendor
            assign productFilter = allProductsPaginated | where: 'vendor', vendorAssignProduct
            for item in productFilter
              assign itemHandle = item.handle
              assign productF = item | sort
              if itemHandle != handleAssignProduct
                assign products = products | concat: productF
              endif
            endfor
          when 'production-type'
            assign typeAssignProduct = assignProduct.type
            assign productFilter = allProductsPaginated | where: 'type', typeAssignProduct
            for item in productFilter
              assign itemHandle = item.handle
              assign productF = item | sort
              if itemHandle != handleAssignProduct
                assign products = products | concat: productF
              endif
            endfor
          when 'tags'
            assign tagsAssignProduct = assignProduct.tags
            assign hasRelatedExclude = ${P}
            if hasRelatedExclude
              assign relatedExclude = '${h}'
            else
              assign relatedExclude = ''
            endif
            assign arrayRelatedExclude = relatedExclude | split: ","
              for item in allProductsPaginated
                unless unique_products contains item.handle
                  assign isExcludedProduct = false
                  for tagExclude in arrayRelatedExclude
                    if item.tags contains tagExclude
                      assign isExcludedProduct = true
                    endif
                  endfor
                  unless isExcludedProduct
                    for tag in tagsAssignProduct
                      if item.tags contains tag and assignProduct.handle != item.handle
                        assign productF = item | sort
                        assign products = products | concat: productF
                      break
                        endif
                      endfor
                  endunless
                endunless
              endfor
            assign products = products | uniq
        endcase
        paginate products by ${E||4}
          assign products = products
        endpaginate
      `)}
  `,{productListWrapStyle:w,productListContentStyle:H,productListWrapAlignStyle:q}=c(g),{productListWrapClass:G}=l(g);return i`
    ${e(s,`
        <script ${d('class="gps-link" delay',"src")}="{{ 'gp-product-v7-5.js' | asset_url }}" defer="defer"></script>
        ${p?.sameHeight?`<script ${d('class="gps-link" delay',"src")}="{{ 'gp-same-height-v7-5.js' | asset_url }}" defer="defer"></script>`:""}
        `,`
        <script ${d('class="gps-link" delay',"src")}="${t}/assets-v2/gp-product-v7-5.js?v={{ shop.metafields.GEMPAGES.ASSETS_VERSION }}" defer="defer"></script>
        ${p?.sameHeight?`<script ${d('class="gps-link" delay',"src")}="${t}/assets-v2/gp-same-height-v7-5.js?v={{ shop.metafields.GEMPAGES.ASSETS_VERSION }}" defer="defer"></script>`:""}
        `)}

    ${e(!!f?.preloadBgImage,`${e(!!f?.backgroundImage?.desktop?.image?.src,`<link rel="preload" href="${f?.backgroundImage?.desktop?.image?.src}" as="image" />`)}${e(!!f?.backgroundImage?.tablet?.image?.src,`<link rel="preload" href="${f?.backgroundImage?.tablet?.image?.src}" as="image" />`)}${e(!!f?.backgroundImage?.mobile?.image?.src,`<link rel="preload" href="${f?.backgroundImage?.mobile?.image?.src}" as="image" />`)}`)}
    <div
      class="${G}"
      style="${w}"
    >
      <div class='gp-flex gp-w-full' style="${q}">
        <div
          style="${H}"
        >
          {%- assign gpBkProduct = product -%}
          {%- assign gpBkProducts = products -%}
          ${(()=>{let e=F?`
          assign target_collection = collections['${F}']
          if target_collection == empty
            for collection in collections
              if collection.id == ${_}
                assign target_collection = collection
                break
              endif
            endfor
          endif
        `:`
          for collection in collections
            if collection.id == ${_}
              assign target_collection = collection
              break
            endif
          endfor
        `;return i`
      {%- liquid
        assign productHandles = '${x.toString()}'| split: ','
        assign products = null
        assign target_collection = null
        assign productSrc = '${A}'
        assign c = 0
        assign limit = ${L??4}

        if request.page_type == 'collection' or preview_page_type == 'collection'
          assign target_collection = collection
          if target_collection == empty or target_collection == null
            if '${_.toLowerCase()}' == 'latest'
              assign target_collection = collections | sort: 'updated_at' ${b?"| reverse":""} | first
            else
              ${e}
            endif
          endif
          paginate target_collection.products by ${E||4}
            assign products = target_collection.products ${R?S:""} ${b?"| reverse":""}
          endpaginate
        else
          if productSrc == 'RelatedProduct'
            ${j()}
          elsif productSrc == 'PickProduct'
            unless ${y}
              assign products = null | sort
              for handle in productHandles
                assign productH = all_products[handle] | sort
                assign products = products | concat: productH
              endfor
              assign limit = products.length
            else
              if ${k.length} == 0
                paginate collections.all.products by 100000
                  assign products = collections.all.products  | sort : "created_at"  | reverse
                endpaginate
              endif
            endunless
          else
            if '${_.toLowerCase()}' == 'latest'
              assign target_collection = collections | sort: 'updated_at' ${b?"| reverse":""} | first
            else
              ${e}
            endif
            paginate target_collection.products by ${E||4}
              assign products = target_collection.products ${R?S:""} ${b?"| reverse":""}
            endpaginate
          endif
        endif
      -%}
    `})()}
          ${i`
    {%- if products == null -%}
    {%- if count == 0 and ${k.length} == 0 -%}
      {% if '${A}' == 'RelatedProduct' -%}
          <div>${o("ProductList","no_related_products")}</div>
        {% else %}
          <div>${o("ProductList","no_products_found")}</div>
      {%- endif -%}
    {%- endif -%}
    {%-else-%}
      ${r(v,i`${n({...g,productListId:k,productSrc:A})}`,i`${a({...g,productListId:k,productSrc:A})}`)}
    {%- endif -%}
    `}
          {%- assign product = gpBkProduct -%}
          {%- assign products = gpBkProducts -%}
        </div>
      </div>
    </div>
  `};export{g as default};
