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 l=l=>{let{option:o,variantGroup:c,variants:d,initValue:m,hasPreSelected:s,presetConfiguration:u}=l,{setSelectedOption:y,forceSelectedOption:g,selectedOptions:k}=e(),p=t(),v=r(()=>{if(!u)return;let{presets:e,optionType:t}=u;return e?.[t||"rectangle_list"]},[u]),[f,h]=a(s?m:"blank");i(()=>{h(k?.[o?.name??"blank"])},[k,o]),i(()=>{s||h("blank")},[s]);let O=e=>{let t=d?.find(t=>t?.id===e);if(t&&t.selectedOptions){let e=n(t.selectedOptions);e&&g(e,p?.id,!1,!0)}};return{value:f,preset:v,handleOnChangeSelect:e=>{c?O(e.target?.value):y(o?.name,e.target?.value,p?.id)},checkVariantInStock:e=>!!e&&(e?.inventoryQuantity&&e.inventoryQuantity>0||!e?.manageInventory||"CONTINUE"===e.inventoryPolicy)}};export{l as useDropdownSwatches};
