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