import{useSelectedOption as e,useProduct as t,parseSelectedOption as n}from"@gem-sdk/core";import{useMemo as r,useState as a,useEffect as i}from"react";let useDropdownSwatches=o=>{let{option:l,variantGroup:s,variants:d,initValue:u,hasPreSelected:c,presetConfiguration:m}=o,{setSelectedOption:p,forceSelectedOption:y,selectedOptions:v}=e(),f=t(),g=r(()=>{if(!m)return;let{presets:e,optionType:t}=m;return e?.[t||"rectangle_list"]},[m]),[k,w]=a(c?u:"blank");i(()=>{w(v?.[l?.name??"blank"])},[v,l]),i(()=>{c||w("blank")},[c]);let b=e=>!!e&&(e?.inventoryQuantity&&e.inventoryQuantity>0||!e?.manageInventory||"CONTINUE"===e.inventoryPolicy),O=e=>{let t=d?.find(t=>t?.id===e);if(t&&t.selectedOptions){let e=n(t.selectedOptions);e&&y(e,f?.id,!1,!0)}},h=e=>{s?O(e.target?.value):p(l?.name,e.target?.value,f?.id)};return{value:k,preset:g,handleOnChangeSelect:h,checkVariantInStock:b}};export{useDropdownSwatches};
