import{usePageStore as o,useRenderMode as t,useProductOffersQuery as e,flattenConnection as r}from"@gem-sdk/core";import{useDynamicProduct as i}from"../../../hooks/useDynamicProduct.js";import{useMemo as n,useEffect as d}from"react";import{hasPublicStoreFrontData as a,createStoreFrontFetcher as p}from"../../../helpers/index.js";import{productOfferPlaceholder as u}from"../../../constants/ProductOfferPlaceholder.js";let useProductOfferData=()=>{let s=o(o=>o.productOffers),c=o(o=>o.publicStoreFrontData),{isDynamicProduct:l}=i(),f=o(o=>o.dynamicDiscountOffer),{isEditMode:m}=t(),P=n(()=>l?f?.appliedProductCount:s?.length,[f?.appliedProductCount,l,s?.length]),b=n(()=>s?.sort((o,t)=>o?.node?.position&&t?.node?.position?o.node.position-t.node.position:0).map(o=>o?.node?.objectBaseID).filter(Boolean),[s]),{isLoading:h,isValidating:D,data:C}=e(b||[],{revalidateOnMount:!0},{allStatus:!0,fetcher:a(c)?p(c):void 0}),O=n(()=>h||D,[h,D]),v=n(()=>r(C?.products),[C?.products]),g=n(()=>l&&f?.appliedProductCount?Array(f.appliedProductCount).fill(u):b?.map(o=>v?.find(t=>t?.baseID===o)).filter(Boolean),[l,f?.appliedProductCount,b,v]);return d(()=>{if(O||!m)return;let o=new CustomEvent("editor:toolbar:force-active-component",{bubbles:!0,detail:{}});window.dispatchEvent(o)},[O,m]),{isLoading:O,productSortedById:g,totalProduct:P}};export{useProductOfferData};
