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