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 l}from"../../../constants/ProductOfferPlaceholder.js";let s=()=>{let s=o(o=>o.productOffers),u=o(o=>o.publicStoreFrontData),{isDynamicProduct:c}=i(),f=o(o=>o.dynamicDiscountOffer),{isEditMode:m}=t(),P=n(()=>c?f?.appliedProductCount:s?.length,[f?.appliedProductCount,c,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:C,data:D}=e(b||[],{revalidateOnMount:!0},{allStatus:!0,fetcher:a(u)?p(u):void 0}),v=n(()=>h||C,[h,C]),O=n(()=>r(D?.products),[D?.products]),g=n(()=>c&&f?.appliedProductCount?Array(f.appliedProductCount).fill(l):b?.map(o=>O?.find(t=>t?.baseID===o)).filter(Boolean),[c,f?.appliedProductCount,b,O]);return d(()=>{if(v||!m)return;let o=new CustomEvent("editor:toolbar:force-active-component",{bubbles:!0,detail:{}});window.dispatchEvent(o)},[v,m]),{isLoading:v,productSortedById:g,totalProduct:P}};export{s as useProductOfferData};
