'use client';
import{jsx as e,Fragment as t,jsxs as r}from"react/jsx-runtime";import o from"./variants/VariantGroup.js";import{createClass as i,createStyle as n}from"@gem-sdk/system";import{getDefaultVariantClasses as s,getContainerClasses as a,getDropdownVariantWrapperClasses as d}from"../common/classes.js";import{getWrapperStyles as l}from"../common/styles.js";import c from"./ProductOption.js";import{useVariants as m,useProduct as p,useIsSyncProduct as u,useSelectedOption as f,useEditorMode as h,useInitialSwatchesOptions as w,useSwatchesOptions as v,DEVICES as g,getSelectedVariant as P}from"@gem-sdk/core";import{useTransition as y}from"../hooks/useTransition.js";import{useState as L,useEffect as j,useMemo as N,useCallback as O}from"react";import{checkDefaultVariant as V}from"../../../../../product/helpers/product.js";let PostPurchaseProductVariants=({builderProps:E,style:S,styles:b,setting:k})=>{let{hasPreSelected:x}=k??{},I="variant",R=!1,{dropdownItemWidth:U}=b??{},{optionType:G,variantPresets:J}=k??{},T=m(),A=p(),W=u(),{selectedOptions:q,setSelectedOption:z,forceSelectedOption:B}=f(),[C,D]=L(""),F=h();j(()=>{let e=new URL(window.location.href),t=e.searchParams.get(I);t&&D(t)},[]);let H=()=>{let e=new URL(window.location.href),t=e.searchParams.get(I);if(t){let e=T.find(e=>e?.id===t);return e}return null};j(()=>{let e=H();if(e){let t=e?.selectedOptions.reduce((e,t)=>t.name?{...e,[t.name]:t.value}:e,{})??{};B(t,A?.id,!0)}},[]);let K=A?.options;w(K);let M=v(K),[Q,X]=L(!1),Y=V(A),Z=N(()=>"groupOption"===G,[G]),$=()=>{let e={};return g.forEach(t=>{e[t]=U?.[t]==="fill"}),e},_=O(e=>{let t=J?.find(t=>t.optionName===e);return t||J?.find(e=>"base"===e.optionName)},[J]),ee=O(e=>{let t=e.detail,r=A?.id;X(!0),W&&t&&r==t.productId&&z(t.optionId,t.optionValue,r,!0)},[W,A?.id,z]),et=O(e=>{let t=e.detail,r=A?.id;X(!0),W&&t&&r==t.productId&&B(t.selectedOption,r,!0)},[B,W,A?.id]),er=e=>{let t=new URL(window.location.href);e?t.searchParams.set(I,e):t.searchParams.delete(I),history.replaceState(null,"",t.toString())};j(()=>(window.addEventListener("set-selected-option",ee),window.addEventListener("force-selected-option",et),()=>{window.removeEventListener("set-selected-option",ee),window.removeEventListener("force-selected-option",et)}),[et,ee]),y(e=>{if(!R){R=!0;return}let t=JSON.stringify(e),r=JSON.stringify(q);if(e&&t!==r){let e=P(T,q);er(e?.id||"")}},[q]);let eo=i({...s()}),ei=i({...a()}),en=i({...d(b,Z)}),es=n({...l({setting:k,styles:b,style:S})});return e(t,{children:Y?e("div",{children:"edit"===F&&e("p",{className:eo,children:"This product has only default variant"})}):r("div",{className:ei,style:es,children:[!Z&&M?.map((t,r)=>e(c,{option:t,indexOption:r,productVariantSettings:{getVariantConfiguration:_,getSwatchDropdownAutoWidth:$,hasPreSelected:x,variantId:C,selectedOptions:q,isChangeVariant:Q},setting:k,styles:b,builderProps:E,optionLength:M.length},t.id)),Z&&e("div",{className:en,children:e(o,{...b,...k,variantGroup:!0,label:"Variants",swatchAutoWidth:$()})})]})})};export{PostPurchaseProductVariants as default};
