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