"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var core=require("@gem-sdk/core"),Text_liquid=require("../../../text/components/Text.liquid.js"),index_liquid=require("./components/variants/index.liquid.js"),Dropdown_liquid=require("./components/dropdown-swatches/Dropdown.liquid.js"),DropdownGroup_liquid=require("./components/dropdown-group/DropdownGroup.liquid.js"),helpers=require("../../../helpers.js"),mappingVariantStyles=require("./utils/mappingVariantStyles.js"),system=require("@gem-sdk/system"),getAttr=require("./common/main/getAttr.js"),styles=require("./common/main/styles.js"),classes=require("./common/main/classes.js"),getData=require("./common/main/getData.js"),classes$1=require("./common/variants/classes.js"),_function=require("../../helpers/function.js"),styles$1=require("./common/variants/styles.js"),OptionVariantsNewFormat_liquid=require("./OptionVariantsNewFormat.liquid.js"),OptionVariantsOldFormat_liquid=require("./OptionVariantsOldFormat.liquid.js");const DEFAULT_ADVANCED_DISPLAY_SETTING={desktop:!0,tablet:!0,mobile:!0},ProductVariants=e=>{let{builderProps:t,styles:s,setting:i,advanced:a}=e,{align:o,swatchItemWidth:n,dropdownItemWidth:r,swatchSpacing:l,optionTypo:p,marginBottom:d}=s??{},{label:u,optionType:m,showAsSwatches:c,layout:y,hasPreSelected:v}=i??{},g=a?.d??DEFAULT_ADVANCED_DISPLAY_SETTING,{uid:_}=t??{},f="groupOption"===m,q=()=>{let e={};return core.DEVICES.forEach(t=>{let s=core.getResponsiveValueByScreen(n,t);e[t]="auto"===s}),e},$=()=>{let e={};return core.DEVICES.forEach(t=>{e[t]=r?.[t]==="fill"}),e},h=i?.variantPresets?mappingVariantStyles.mappingVariantStyles(i?.variantPresets,s):[],S=h?.map(e=>({...e,presets:{[e.optionType]:e.presets[e.optionType]}})),w={...i,variantPresets:S},L=e=>{let t={"'":"&apos;",'"':"&quot;"},s=e;for(let i in t)e.indexOf(i)>=0&&(s=s.replaceAll(i,t[i]));return s},j=()=>i?.variantPresets?.map(e=>`${L(e.optionName)}($2)${e.optionType}`).join("($1)"),O=()=>i?.variantPresets?.filter(e=>!!e?.hide)?.map(e=>e.optionName).join(","),V=({showDropdown:e,isRenderDefault:t,typeOfOption:i})=>core.template`
    ${system.LiquidFor("value in values",index_liquid.default({id:"{{option.name | escape | handleize}}-{{value | escape | handleize}}",variantKey:"{{option.name | escape}}-{{option.position}}-{{ forloop.index }}",showAsSwatches:c,uid:_,name:"{{option.name | escape}}",value:{label:"{{value | escape}}"},checked:v?"{%- if option.selected_value == value -%}true{% else %}false{%- endif -%}":"false",optionPosition:"rootForloop",typeOfOption:i,activeClass:v?"{%- if option.selected_value == value -%}option-item-active{%- endif -%}":"",...s,...w,swatchAutoWidth:q(),optionTypo:p,isRenderDefault:t}))}
      ${system.LiquidIf(e?'type == "dropdown"':"false",Dropdown_liquid.default({...s,...w,swatchAutoWidth:$(),optionTypo:p}))}`,D=i?.variantPresets?.map(e=>({name:L(e.optionName),type:e.optionType})).map(e=>`${e.name}($1)${e.type}`).join("($2)").toString(),A={renderVariantItem:V,variantPresetsLiquid:D??""},b=system.createClass(classes.getVariantOptionGroupClass(o)),P=system.createStyle(styles.getVariantOptionGroupStyle(l,l)),E=getData.getTextVariantTitleData(e,"Liquid"),x=system.createClass(classes$1.getWrapVariantOptionClass(y,o)),N=()=>core.template`
      ${system.Liquid(`{%- assign presets = "${j()}" | split: '($1)' -%}`)}
      ${system.Liquid(`{% assign hiddenPresetOptions = "${O()}" | split: ',' %}`)}
      
      ${system.Liquid(`{% assign all_option_names = options | map: 'name' %}
      {% assign filtered_options_string = '' %}
      {% assign delimiter = '||' %}
      
      {% for item_name in all_option_names %}
        {% assign is_excluded = false %}
        {% for exclude_name in hiddenPresetOptions %}
          {% if item_name == exclude_name %}
            {% assign is_excluded = true %}
          {% endif %}
        {% endfor %}
        
        {% unless is_excluded %}
          {% if filtered_options_string == '' %}
            {% assign filtered_options_string = item_name %}
          {% else %}
            {% assign filtered_options_string = filtered_options_string | append: delimiter | append: item_name %}
          {% endif %}
        {% endunless %}
      {% endfor %}
      
      {% assign filtered_options_array = filtered_options_string | split: delimiter %}`)}
      
      ${system.Liquid("{% assign last_option_name = filtered_options_array | last %}")}
      
      ${system.LiquidFor("option in options",`
        <div
        option-name="{{option.name | escape}}"
        class="${x} ${system.LiquidIf("hiddenPresetOptions contains option.name","gp-hidden")}"
        style="${_function.getStyleOfObject(styles$1.getMarginBottomStyle(d))}${system.Liquid("{% if forloop.last or option.name == last_option_name %}--mb:0;--mb-mobile:0;--mb-tablet:0;{% endif %}")}"
        >
          ${system.Liquid("{%- assign showVariantClass = 'variant-display' -%}")}
          ${system.Liquid("{%- assign optionName = option.name  -%}")}
          ${system.LiquidFor("preset in presets",`
            ${system.Liquid("{%- assign presetDetail = preset | split: '($2)' -%}")}
            ${system.LiquidIf("presetDetail[1] == 'dropdown' and presetDetail[0] == optionName",`${system.Liquid("{%- assign showVariantClass = '' -%}")} ${system.Liquid("{%- break -%}")}`)}
          `)}
          ${system.If(u,Text_liquid.default(E)," ")}
          <div
              variant-option-name="{{option.name | escape}}"
              class="${b}"
              style="${_function.getStyleOfObject(P)}"
            >
              ${system.Liquid("{%- assign values = option.values -%}")}
              ${system.Liquid("{%- assign rootForloop = forloop.index0 -%}")}
              ${system.Liquid(`
                {%- if option.position == 1 -%}
                  {%- assign selectedValue = variant.option1 -%}
                {%- elsif option.position == 2 -%}
                  {%- assign selectedValue = variant.option2 -%}
                {%- else -%}
                  {%- assign selectedValue = variant.option3 -%}
                {%- endif -%}
              `)}
              ${system.If(!v,system.Liquid("{%- assign selectedValue = null -%}"),"")}
              ${system.If(c,T(),V({showDropdown:!1,isRenderDefault:!0,typeOfOption:"optionType"}))}
          </div>
      </div>`)}
    `,T=()=>core.template`
      ${system.LiquidIf("shop.metafields.GEMPAGES.swatchesJSONFormat and shop.metafields.GEMPAGES.swatchesJSONFormat != blank",OptionVariantsNewFormat_liquid.renderOptionValuesNewFormatSwatches(A),OptionVariantsOldFormat_liquid.renderOptionValuesOld(A))}
      `,I=system.createClass(classes.getWrapDropdownClass(o)),F=getData.getDropdownGroupData(e),C=()=>`
      <div className="${I}">
        ${DropdownGroup_liquid.default(F)}
      </div>
    `,G=system.createAttr(getAttr.getAttrs({uid:_,hasPreSelected:v,cssClass:a?.cssClass})),k=system.createStyle(styles.getWrapperStyle(e)),z=system.createClass({"gp-flex gp-flex-col !gp-ml-0":!0}),W=()=>{let e=core.isLocalEnv?"{{ 'gp-product-variant-v7-5.js' | asset_url }}":`${core.baseAssetURL}/assets-v2/gp-product-variant-v7-5.js?v={{ shop.metafields.GEMPAGES.ASSETS_VERSION }}`;return`<script ${helpers.getSettingPreloadData('class="gps-link" delay',"src")}="${e}" defer="defer"></script>`};return core.template`
    ${system.Liquid("{%- assign total_combinations = 1 -%}")}
    ${system.LiquidFor("option in product.options_with_values","{%- assign total_combinations = total_combinations | times: option.values.size -%}")}
    <gp-product-variants
      ${G}
      gp-data='{
        "setting":${JSON.stringify(w)?.replaceAll("'","&#039;")},
        "styles":${JSON.stringify(s)},
        "variants":{{product.variants | json | escape}},
        "optionsWithValues": {{product.options_with_values | json | escape}},
        "variantSelected": {{ variant | json | escape }},
        "variantInventoryQuantity": {{product.variants | map: 'inventory_quantity' | json | escape}},
        "variantInventoryPolicy": {{product.variants | map: 'inventory_policy' | json | escape}},
        "moneyFormat": {{shop.money_format | json | escape}},
        "productId": {{product.id | json | escape}},
        "productUrl": {{product.url | json | escape}},
        "productHandle": {{product.handle | json | escape}},
        "displayState": ${JSON.stringify(g)},
        "totalVariantCombinations": {{total_combinations}},
        "firstAvailableVariant": {{product.selected_or_first_available_variant | json | escape}}
      }
    '>
      <div class="gp-hidden" style="--hvr-shadow: none; --hvr-shadow-tablet: none; --hvr-shadow-mobile: none"></div>
      ${system.Liquid("{%- assign options = product.options_with_values -%}")}
      ${system.Liquid("{%- assign variants = product.variants -%}")}
      ${system.LiquidIf("options.size == 0 or options.size == 1 and variants.size == 1 and variants[0].title == 'Default Title' and variants[0].option1 == 'Default Title'","<div></div>",core.template`<div
            class="${z}"
            style="${k}"
          >
          ${system.If(f,C(),N())}
        </div>`)}
    </gp-product-variants>

    ${W()}
  `};exports.default=ProductVariants;
