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