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