1import{w as p,s as I,au as A,av as k,c as x,l as U,aw as D,ax as b,a4 as V,ay as B,J as g,e as H,T as K,az as w,H as q,D as z,aA as W,aB as j,aC as G,aD as J,S as Q,C as X}from"./storefrontApp.CsApItsi.min.js";import{P as Y}from"./chunk-product_http_api.CErFNOr.js";import{S as Z}from"./chunk-storefront_settings_constants.DIF02Gz.js";import{U as v,J as tt,B as et,e as d}from"./chunk-vendor-lit.NvY0u02.js";import{P as it,a as st,b as ot,V as P,c as nt,d as m,T as at,S as rt,F as ct,C as lt,R as pt,e as dt}from"./chunk-text_variant_option.Cu2Ghz5.js";import{m as T,O as ut,e as ht,o as St,g as Ot,h as y,f as _,c as N,s as M,i as Vt}from"./chunk-vendor-rxjs.Dp7kflo.js";import{P as gt}from"./chunk-product_stock_mapper.CggvzlW.js";import{P as f}from"./chunk-product_events_constants.BHFu5GP.js";import{S as mt}from"./chunk-subscription_controller.pXrelaM.js";import{a as _t}from"./chunk-form_controller.DnsWIaz.js";import{S as ft}from"./chunk-storefront_base_lit_element.lX23Jvc.js";import{b as kt,F as Pt}from"./chunk-control_connector.Ckt-76h.js";import{C as Ct}from"./chunk-context_provider_controller.CCbkLM7.js";import"./chunk-vendor-di.Bh23gdA.js";import"./chunk-vendor-lodash.CWHFt7Z.js";import"./chunk-vendor-phoenix.cdKS79L.js";import"./chunk-base_http_api_url.H743YfZ.js";import"./chunk-producer_mapper.CMRXClb.js";import"./chunk-price_mapper.RARQykB.js";import"./chunk-description_mapper.LUIBH8e.js";import"./chunk-product_code_mapper.uZ5tufp.js";import"./chunk-loyalty_points_mapper.DhjGhUJ.js";import"./chunk-color_swatches_control_constants.Goty5zR.js";import"./chunk-control_connector_constants.CepOMss.js";import"./chunk-radio_constants.BmnQvEx.js";import"./chunk-file_picker_constants.5Ry9C2V.js";import"./chunk-form_state_constants.Gcz9tYH.js";import"./chunk-index.esm.BJO8Gf1.js";import"./chunk-select_utils.BhqqJnk.js";import"./chunk-context_consumer_controller.DQ84A7A.js";class c{static isVirtualOptionValue(t){return!t.option.isStock}static isStockOptionValue(t){return!!t.option.isStock}static toProductOptionValues(t){return t.map(e=>this.toProductOptionValue(e))}static toProductOptionValue({value:t,option:e,name:i,label:s,userValue:o,additionalValue:n}){const a={id:t,optionId:e.id,order:e.order,label:s,name:i,userValue:o,additionalValue:n,type:e.type};return e.type===it.color?st.create({...a,color:""}):ot.create(a)}static toProductOptionIds(t){return t.map(e=>e.value)}}class It{#t;constructor({store:t}){this.#t=t}setVariantId(t){this.#t.update(p(e=>e.variantId=t))}getVariantId(){return this.#t.getValue().variantId}getProductId(){return this.#t.getValue().productId}getStockOptionsCount(){return this.#t.getValue().stockOptionsCount}getSelectedOptions(){return this.#t.getValue().selectedOptions}getProductInstanceKey(){return this.#t.getValue().productInstanceKey}getSelectedStocksOptions(){return this.getSelectedOptions().filter(c.isStockOptionValue)}getLastSelectedStocksOption(){const t=this.getSelectedStocksOptions();return t[t.length-1]}getSelectedVirtualOptions(){return this.getSelectedOptions().filter(c.isVirtualOptionValue)}selectSelectedStocksOptions$(){return this.#t.pipe(I(t=>t.selectedOptions),T(t=>t.filter(c.isStockOptionValue)),A())}selectSelectedVirtualOptions$(){return this.#t.pipe(I(t=>t.selectedOptions),T(t=>t.filter(c.isVirtualOptionValue)),A())}addSelectedStocksOption(t){this.#t.update(p(e=>e.selectedOptions.push(t)))}hasSelectedOption(t){return!!this.getSelectedOptions().find(e=>e.option.id===t.option.id)}removeSelectedStocksOption(t){this.#t.update(p(e=>e.selectedOptions=e.selectedOptions.filter(i=>i.option.id!==t.option.id)))}getSelectedSockOptionsCount(){return this.getSelectedStocksOptions().length}removeAllSelectedStockOptionsAfterSpecific(t){this.#t.update(p(e=>{e.selectedOptions=e.selectedOptions.filter(i=>!i.option.isStock||c.isStockOptionValue(i)&&i.stockOrder<t.stockOrder)}))}getLatestVirtualOptionWithImage(){return[...this.getSelectedOptions()].reverse().find(t=>c.isVirtualOptionValue(t)&&t.hasProductImage)}getLatestVirtualOptionWithImageIndex(){return[...this.getSelectedOptions()].reverse().findIndex(t=>c.isVirtualOptionValue(t)&&t.hasProductImage)}
1getLatestStockOptionIndex(){return[...this.getSelectedOptions()].reverse().findIndex(c.isStockOptionValue)}addSelectedVirtualOption(t){k(()=>{this.hasSelectedOption(t)&&this.removeSelectedOption(t),this.#t.update(p(e=>e.selectedOptions.push(t)))})}removeSelectedOption(t){this.#t.update(p(e=>e.selectedOptions=e.selectedOptions.filter(i=>i.option.id!==t.option.id)))}clearSelectedVirtual(){this.#t.update(p(t=>t.selectedOptions=t.selectedOptions.filter(e=>e instanceof P)))}getCachedProductStocks(){return this.#t.getValue().cachedProductStocks}getCachedProductStock(t){const e=this.getCachedProductStocks();return e?e[t]:null}}const At=({storeName:r="productVariants",productId:t,stockOptionsCount:e,variantId:i=null,selectedOptions:s=[],productInstanceKey:o,cachedProductStocks:n=null})=>x({name:r},U({productId:t,variantId:i,stockOptionsCount:e,selectedOptions:s,productInstanceKey:o,cachedProductStocks:n})),E=r=>new ut(t=>{const{response:e,cancelRequest:i}=r;return e.then(s=>{t.next?.(s.data),t.complete?.()}).catch(s=>{t.error?.(s)}),()=>{i()}});class vt{toModel(t){return nt.create(t)}toProductOptionsAndValuesDTO(t){return t.reduce((e,i)=>({...e,[i.optionId]:i.id}),{})}}class Tt{constructor({productId:t,variantId:e,productHttpApi:i,stockOptionsCount:s,selectedOptions:o,cachedProductStocks:n,productInstanceKey:a,pageManagerApi:C,settingsApi:R}){this.areVariantStockOptionsFulfilled=h=>{const S=this.#t.getStockOptionsCount();return S?S===h.length:!1},this.shouldFetchProductVariantOnStockChanged=h=>this.areVariantStockOptionsFulfilled(h)||!h.length,this.shouldFetchStockOptionsValues=([,h])=>{const S=h.length,L=this.#t.getStockOptionsCount();return!!S&&S<L};const $=At({productId:t,variantId:e,stockOptionsCount:s,selectedOptions:o,productInstanceKey:a,cachedProductStocks:n});this.#t=new It({store:$}),this.#e=i,this.#i=C,this.#s=R,this._changeUrlOnSelectedOptionsChange()}#t;#e;#i;#s;_changeUrlOnSelectedOptionsChange(){ht([this.selectSelectedVirtualOptions$(),this.selectSelectedStocksOptions$()]).subscribe(([t,e])=>{const i=[...t,...e],s=this._createHashUrlFromVariantOptions(i);s&&this._replaceHistory(s)})}_createHashUrlFromVariantOptions(t){return t.map(e=>{const i=e.option.type==="text"?e.userValue:e.value;return i.length<1?"":`${e.option.id}:${encodeURIComponent(i)}`}).filter(Boolean).join(";")}_replaceHistory(t){this.#i.then(e=>{D.nextTick(()=>{const i=new URL(window.location.href);i.hash=t?`variantOptions=${t}`:"",e.replaceHistory(i.toString())})})}getSelectedStocksOptions(){return this.#t.getSelectedStocksOptions()}getSelectedVirtualOptions(){return this.#t.getSelectedVirtualOptions()}selectSelectedStocksOptions$(){return this.#t.selectSelectedStocksOptions$()}selectSelectedVirtualOptions$(){return this.#t.selectSelectedVirtualOptions$()}getLastSelectedStocksOption(){return this.#t.getLastSelectedStocksOption()}getProductId(){return this.#t.getProductId()}getProductInstanceKey(){return this.#t.getProductInstanceKey()}selectFetchNewVariantProduct$(t){const e=c.toProductOptionValues(this.getSelectedStocksOptions()),i=new vt,s=e.length===this.#t.getStockOptionsCount(),o=i.toProductOptionsAndValuesDTO([...s?e:[],...c.toProductOptionValues(this.getSelectedVirtualOptions())]),n=t?c.toProductOptionValue(t).optionId:void 0;if(this.#s.isFeatureEnabled("stock_optimization")&&this.#s.isFeatureEnabled("dev_storefront_stock_optimization")){const a=this._getCachedProductStock(o,n);if(a)return St(this._normalizeCachedStockDto(a))}return E(this.#e.getProductVariant(this.getProductId(),{variantOptions:o,modVOption:n}))}_getCachedProductStock(t,e){if(e!==void 0)return null;const i=this._buildVariantOptionsCacheKey(t);return this.#t.getCachedProductStock(i)}_buildVariantOptionsCacheKey(t){return Object.values(t).sort((e,i)=>Number(e)-Number(i)).join(",")}_normalizeCachedStockDto(t){const e={...t,price:{...t.price,gross:{...t.price.gross},net:{...t.price.net}}};return e.price&&(e.price.historical_lowest_price=e.historical_lowest_price),new gt().toModel(e)}selectFetchStockOptionValues$(){return E(this.#e.getProductVariantStockOptionValues(this.getProductId(),c.toProductOptionIds(this.getSelectedStocksOptions())))}fetchStockOptionsValues(t){return this.#e.getProductVariantStockOptionValues(this.getProductId(),c.toProductOptionIds(t))}getLatestVirtualOptionWithImage(){const t=this.#t.getLatestStockOptionIndex(),e=this.#t.getLatestVirtualOptionWithImageIndex();if(!(this.areVariantStockOptionsFulfilled(this.getSelectedStocksOptions())&&t<e))return this.#t.getLatestVirtualOptionWithImage()}selectStockOption(t){k(()=>{this.#t.hasSelectedOption(t)&&this.#t.removeAllSelectedStockOptionsAfterSpecific(t),this.#t.addSelectedStocksOption(t)})}removeSelectedStocksOption(t){this.#t.removeSelectedStocksOption(t)}removeAllSelectedStockOptionsAfterSpecific(t){this.#t.removeAllSelectedStockOptionsAfterSpecific(t)}removeSelectedOption(t){this.#t.removeSelectedOption(t)}selectVirtualOption(t){k(()=>{this.#t.hasSelectedOption(t)&&this.#t.removeSelectedOption(t),this.#t.addSelectedVirtualOption(t)})}}const F=100;class O{static getDefaultSelectedStockOptionsValues(t){return this.getAllStockVariants(t).map(e=>e.getSelectedOption()).filter(e=>e)}static resetOptionsValuesBelow(t,e){this.getAllStockVariants(t).filter(o=>o.stockOrder>e.stockOrder).forEach(o=>o.setValues([]))}static getAllStockVariants(t){return[...t.querySelectorAll(`[${b}]`)]}}class yt{constructor({host:t,variantsService:e,eventBus:i}
1){this._appendLoaderToStockOptionsThatValuesAreLoaded=()=>{const s=this.#e.getLastSelectedStocksOption();if(!s)return;const o=this._getNextVariantStockOptionToSetValues(s);o&&(o.hidden=!1,V.appendLoader(o,B.full))},this._updateVariantStockOptionValues=(s,o)=>{this.#i.emit(new g(f.variantOptionChanged));const n=o??this.#e.getLastSelectedStocksOption();if(!n)return;const a=this._getNextVariantStockOptionToSetValues(n);a&&(V.removeLoader(a),a.setValues(s,!n?.isAvailable))},this._dispatchProductVariantChanged=s=>{s.optionValues=c.toProductOptionValues([...this.#e.getSelectedStocksOptions(),...this.#e.getSelectedVirtualOptions()]),s.productInstanceKey=this.#e.getProductInstanceKey(),this.#i.emit(new g(f.stockChanged,s))},this.#t=t,this.#e=e,this.#i=i,this.#s=new mt(t),this.#t.addController(this)}#t;#e;#i;#s;hostConnected(){this._subscribeToStockOptionsValuesChange(),this._subscribeToFetchProductVariant(),this.#t.hasDefaultVariant&&this._updateOptionsForVariantDefaultValue()}async _updateOptionsForVariantDefaultValue(){const t=O.getDefaultSelectedStockOptionsValues(this.#t),e=this._getAllStockOptionsValuesSelectedSequentiallyExpectFirst(t);if(e.length<1){V.removeLoader(this.#t);return}try{await Promise.all(e.map(i=>new Promise(s=>{this.#e.fetchStockOptionsValues(i).response.then(o=>{const n=i.pop();n&&this._showStockOptionValues(n,o.data),s(!0)})})))}catch(i){console.error(i)}finally{V.removeLoader(this.#t)}}_showStockOptionValues(t,e){const i=this._getNextVariantStockOptionToSetValues(t);i&&i.showValues(e,!t?.isAvailable)}_getAllStockOptionsValuesSelectedSequentiallyExpectFirst(t){const e=t.shift();if(!e)return[];const i=t.reduce((s,o,n)=>[...s,[...s[s.length-1],o]],[[e]]);return i.pop(),i}_subscribeToFetchProductVariant(){this.#s.subscribe(Ot(this.#e.selectSelectedVirtualOptions$(),this._selectSelectedStockOptionsToFetchProductVariant$()).pipe(y(),_(([t,e])=>!(!t.length&&!e.length)),N(F),M(()=>(this.#i.emit(new g(f.stockChanging,this.#e.getProductId())),this.#e.selectFetchNewVariantProduct$(this.#e.getLatestVirtualOptionWithImage())))),this._dispatchProductVariantChanged)}_selectSelectedStockOptionsToFetchProductVariant$(){return this.#e.selectSelectedStocksOptions$().pipe(_(this.#e.shouldFetchProductVariantOnStockChanged))}_subscribeToStockOptionsValuesChange(){this.#s.subscribe(this.#e.selectSelectedStocksOptions$().pipe(y(),_(this.#e.shouldFetchStockOptionsValues),Vt(this._appendLoaderToStockOptionsThatValuesAreLoaded),N(F),M(()=>this.#e.selectFetchStockOptionValues$())),this._updateVariantStockOptionValues)}_getNextVariantStockOptionToSetValues(t){return this._getStockOptionWithSpecificOrder(t?.stockOrder+1)}_getStockOptionWithSpecificOrder(t){return this.#t.querySelector(`[stock-order="${t}"]`)}}var Nt=Object.defineProperty,u=(r,t,e,i)=>{for(var s=void 0,o=r.length-1,n;o>=0;o--)(n=r[o])&&(s=n(t,e,s)||s);return s&&Nt(t,e,s),s};class l extends ft{constructor(){if(super(),this.hasDefaultVariant=!1,this.moduleInstanceId="",this.noMessages=!1,this.groupFlashMessengerName="",this._context=new Ct(this),this.watchChildrenConnected=!0,this._validateVariantOptions=async()=>{const t=await this._formController?.formService.validateControls();return!t&&this._validationMessageId&&this._hideValidationMessageBox(),t&&this._showValidationMessageBoxes(),{isValid:!t,errors:t}},this._deselectVariantOption=t=>{const e=t.detail,i=e instanceof P;i?this._variantService?.removeAllSelectedStockOptionsAfterSpecific(e):this._variantService?.removeSelectedOption(e),i&&O.resetOptionsValuesBelow(this,e)},this._selectVariantOption=t=>{const e=t.detail,i=e instanceof P;i&&O.resetOptionsValuesBelow(this,e),requestAnimationFrame(()=>{i?this._variantService?.selectStockOption(e):this._variantService?.selectVirtualOption(e)})},this.getAttribute("product-id")===null)throw new Error("Missing product-id");this.getApi(H.api).then(t=>{this._flashMessengerApi=t}),this.getApi(K.api).then(t=>{this._translationsApi=t,this._formController=new _t({host:this,withoutFormElement:!0},this._translationsApi),this._context.provide(kt,this._formController),this._context.provide(Pt,this._formController.formService)})}static{this.moduleName=w.feature}allChildrenConnected(){super.allChildrenConnected();const t=this.getApiSync(q),e=this.getApiSync(Z.api),i=this.getApi(z.api);this._variantService=new Tt({variantId:this.variantId?Number(this.variantId):null,productId:this.productId,productHttpApi:new Y({httpApi:t,settingsApi:e}),stockOptionsCount:O.getAllStockVariants(this).length,selectedOptions:O.getDefaultSelectedStockOptionsValues(this),cachedProductStocks:this._getCachedProductStocks(e),productInstanceKey:this.productInstanceKey,pageManagerApi:i,settingsApi:e});const s=this.querySelector(`[${b}]`);s&&v.show(s),new yt({variantsService:this._variantService,eventBus:this.eventBus,host:this}),this._selectVariantOptionsFromUrlHash()}connectedCallback(){if(super.connectedCallback(),!this.productId)throw Error("Missing product-id");this.commandBus.on(`${W.validateVariantsOptions}-${this.productInstanceKey||this.productId}
1`,this._validateVariantOptions),this.addEventListener(m.select,this._selectVariantOption),this.addEventListener(m.deselect,this._deselectVariantOption)}_getCachedProductStocks(t){if(!t.isFeatureEnabled("stock_optimization")||!t.isFeatureEnabled("dev_storefront_stock_optimization"))return null;try{const e=this._getProductStocksCacheConfig();return e?tt.fromJson(et.decode(e)):null}catch{return null}}_getProductStocksCacheConfig(){const t=`#productStocksCache-${this.moduleInstanceId}`;return j.readConfig(t)||void 0}_selectVariantOptionsFromUrlHash(){const t=this._getVariantOptionsFromUrlHash(),e=this.querySelectorAll("[option-name]").length;let i=0;this.addEventListener(m.ready,async()=>{i+=1,e===i&&this._selectVariantOptions(t)})}_getVariantOptionsFromUrlHash(){const e=location.hash.slice(1).split("&").find(i=>i.includes("variantOptions="));return e?e.split("=")[1].split(";"):[]}async _selectVariantOptions(t){for(const[e,i]of t.entries()){const[s,...o]=i.split(":"),n=this._decodeVariantOptionValue(o.join(":")),a=this.querySelector(`[option-id="${s}"]`);a&&(a.isStock&&this._hasAnotherStockOptionToRestore(t,e)?await a.setVariantOption(n):a.setVariantOption(n))}}_hasAnotherStockOptionToRestore(t,e){return t.slice(e+1).some(i=>{const[s]=i.split(":");return this.querySelector(`[option-id="${s}"]`)?.isStock})}_decodeVariantOptionValue(t){try{return decodeURIComponent(t)}catch{return t}}_hideValidationMessageBox(){this._validationMessageId&&this._flashMessengerApi?.removeFlashMessage(this._validationMessageId,this._getValidationMessageContainerName())}_showValidationMessageBoxes(){this.noMessages||this._addValidationFlashMessage(this._translationsApi?.translate("First, select the required product fields")||"",this._getValidationMessageContainerName()),this.groupFlashMessengerName&&this._addValidationFlashMessage(this._translationsApi?.translate("First, select the required fields for all products.")||"",this.groupFlashMessengerName);const t=this.querySelector(`.${G}`);t&&v.show(t)}_getValidationMessageContainerName(){return`${J}-${this.moduleInstanceId}`}_addValidationFlashMessage(t,e){this._validationMessageId=this._flashMessengerApi?.addFlashMessage({content:t,type:"error",isCloseable:!1,timeout:0},e)}}u([d({type:Number,attribute:"product-id"})],l.prototype,"productId");u([d({type:Number,attribute:"variant-id"})],l.prototype,"variantId");u([d({type:Number,attribute:"product-instance-key"})],l.prototype,"productInstanceKey");u([d({type:String,attribute:"has-default-variant"})],l.prototype,"hasDefaultVariant");u([d({type:String,attribute:"module-instance-id"})],l.prototype,"moduleInstanceId");u([d({type:Boolean,attribute:"no-messages"})],l.prototype,"noMessages");u([d({type:String,attribute:"group-flash-messenger-name"})],l.prototype,"groupFlashMessengerName");class ae extends Q{static{this.featureName=w.feature}static{this._components={colorVariantOption:dt,radioVariantOption:pt,checkboxVariantOption:lt,fileVariantOption:ct,selectVariantOption:rt,textVariantOption:at}}init(){return{cores:[{type:X,instance:[l]}]}}}export{ae as ProductVariantsInitializer};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.