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