import{template as t,RenderIf as e,isLocalEnv as s,baseAssetURL as r}from"@gem-sdk/core";import{getSettingPreloadData as d}from"../../../helpers.js";import{createStyle as o,createClass as a,createStateOrContext as i,Liquid as p,LiquidIf as n}from"@gem-sdk/system";import{getSizeSettings as c,getContainerStyles as l}from"./common/styles.js";import{getContainerClasses as u}from"./common/classes.js";import m from"./ProductBundleDiscountChildrenItem.liquid.js";let ProductBundleDiscount=({rawChildren:f,setting:g,styles:I,advanced:$,style:P})=>{let S=g?.hasPreSelectItem?g?.selectItemDefault??0:-1,_=t=>S===t,h=(t,e)=>m({index:e,rawChildrenItem:f?.[e],setting:g,styles:I,isSelected:_(e),bundleItem:t}),y=o(c(I)?.gap),A=a(u(g,$?.cssClass)),B=o(l(y,I,P)),D=i({styles:I,settings:{useProductCompareAtPrice:g?.useProductCompareAtPrice,selectItemDefault:g?.selectItemDefault,hideNoDiscountItem:g?.hideNoDiscountItem,hasPreSelectItem:g?.hasPreSelectItem,iconItemColor:g?.iconItemColor}}),j=()=>e(s,`<script ${d('class="gps-link" delay',"src")}="{{ 'gp-product-bundle-discount-v7-5.js' | asset_url }}" defer="defer"></script>`,`<script ${d('class="gps-link" delay',"src")}="${r}/assets-v2/gp-product-bundle-discount-v7-5.js?v={{ shop.metafields.GEMPAGES.ASSETS_VERSION }}" defer="defer"></script>`);return t`
    <gp-product-bundle-discount
      gp-data='${JSON.stringify(D)}'
      style="${B}"
      class="${A}">
      ${p(`
        {%- assign gempages_product_bundle = product.metafields.gp_discount.gp_discount_selected_products -%}
        {%  assign productBundleItem = gempages_product_bundle | split: '($1)'  %}
      `)}
      ${f?.map((e,s)=>t`
          {% for productBundle in productBundleItem %}
            ${p(`
              {% assign attrs = productBundle | split: '($3)' %}
              {% assign parsedAttrs = '' %}
              {% for attr in attrs %}
                {% assign attrParts = attr | split: '($:)' %}
                {% assign attrKey = attrParts[0] %}
                {% assign attrValue = attrParts[2] | strip %}

                {% if attrValue != '' %}
                  {% assign parsedAttrs = parsedAttrs | append: attrKey | append: '=' | append: attrValue | append: ' ' %}
                {% endif %}
              {% endfor %}
            `)}
            ${n(`forloop.index0 == ${s}`,h("{{ parsedAttrs | strip }}",s))}
          {% endfor %}
          `).join("")}
    </gp-product-bundle-discount>

    {% if productBundleItem %}
      ${j()}
    {% endif %}
  `};export{ProductBundleDiscount as default};
