"use strict";var core=require("@gem-sdk/core"),React=require("react"),useVariantInteraction=require("./useVariantInteraction.js"),common=require("../common/common.js"),product=require("../../../helpers/product.js"),mappingVariantStyles=require("../utils/mappingVariantStyles.js");const VARIANT_PARAM_PATH="variant";let isMounted=!1;const useGpVariants=(e,t)=>{let{builderProps:a,styles:n,setting:i}=e,{optionType:r,hasPreSelected:o}=i??{},{redirectProductShopifyLink:s}=core.useProductShopifyEditLink(),c=core.useProduct(),l=core.useIsSyncProduct(),u=React.useRef(null),{dispatchInteractionChangeOptions:d,dispatchInteractionChangeOptionsInit:p}=useVariantInteraction.useVariantInteraction({element:u,uid:a?.uid}),A=core.useEditorMode(),{selectedOptions:R,setSelectedOption:f,forceSelectedOption:m}=core.useSelectedOption(),{setHasPreSelected:w,hasPreSelected:P}=core.useHasPreSelected(),[S,V]=React.useState("");React.useEffect(()=>{let e=new URL(window.location.href),t=e.searchParams.get(VARIANT_PARAM_PATH);void 0===P&&w(o||!!t)},[o,P,w]),React.useEffect(()=>{let e=new URL(window.location.href),t=e.searchParams.get(VARIANT_PARAM_PATH);t&&V(t)},[]);let h=()=>{let e=new URL(window.location.href),a=e.searchParams.get(VARIANT_PARAM_PATH);return a?t.find(e=>e?.id===a):null},g=e=>{let a=core.getSelectedVariant(t,e);return core.checkInStock(a)};React.useEffect(()=>{let e=h();if(e){let t=e?.selectedOptions.reduce((e,t)=>t.name?{...e,[t.name]:t.value}:e,{})??{},a=g(t);p(t,a),m(t,c?.id,!0)}else if(i?.hasPreSelected){let e=g(R);p(R,e)}},[]);let I=c?.options;core.useInitialSwatchesOptions(I);let M=core.useSwatchesOptions(I),[T,_]=React.useState(!1),v=product.checkDefaultVariant(c),E=React.useMemo(()=>"groupOption"===r,[r]),L=React.useCallback(e=>{let t=e.detail,a=c?.id;_(t.isManualChange||!1),l&&t&&a==t.productId&&f(t.optionId,t.optionValue,a,!0)},[l,c?.id,f]),N=React.useCallback(e=>{let t=e.detail,a=c?.id;_(t.isManualChange||!1),l&&t&&a==t.productId&&m(t.selectedOption,a,!0)},[l,c?.id,m]),y=e=>{let t=new URL(window.location.href);e?t.searchParams.set(VARIANT_PARAM_PATH,e):t.searchParams.delete(VARIANT_PARAM_PATH),history.replaceState(null,"",t.toString())};React.useEffect(()=>(window.addEventListener("set-selected-option",L),window.addEventListener("force-selected-option",N),()=>{window.removeEventListener("set-selected-option",L),window.removeEventListener("force-selected-option",N)}),[N,L]),common.useTransition(e=>{if(!isMounted){isMounted=!0;return}let a=JSON.stringify(e),n=JSON.stringify(R);if(window.interactionLoaded){let e=g(R);d(R,e)}if(e&&a!==n){let e=core.getSelectedVariant(t,R);y(e?.id||"")}},[R]);let k=React.useCallback(e=>{let t=mappingVariantStyles.mappingVariantStyles(i?.variantPresets,n),a=t?.find(t=>t.optionName===e);return a||t?.find(e=>"base"===e.optionName)},[i,n]);return{mode:A,options:M,variants:t,variantId:S,elementRef:u,variantGroup:E,isChangeVariant:T,selectedOptions:R,isDefaultVariant:v,getVariantConfiguration:k,redirectProductShopifyLink:s}};exports.useGpVariants=useGpVariants;
