import{ao as R,d as C,bE as A,bF as q,M as T,u as k,Q as v,ap as D,o as G,af as P,bG as _,bw as x,ar as F,bx as K,ah as H,s as g,U as b}from"../index.Crez7j-Y.bundle.js";import{O as Q}from"./OrderSheetAPIs.DiQ9xHhr.bundle.js";function U(t,n){const r=R(n);return $({filters:{...t,status:"pending"}},r).length}function L(t,n){return t.findAll(n.filters).map(r=>n.select?n.select(r):r.state)}function $(t={},n){const r=R(n).getMutationCache(),i=C.useRef(t),a=C.useRef(null);return a.current||(a.current=L(r,t)),C.useEffect(()=>{i.current=t}),C.useSyncExternalStore(C.useCallback(f=>r.subscribe(()=>{const d=A(a.current,L(r,i.current));a.current!==d&&(a.current=d,q.schedule(f))}),[r]),()=>a.current,()=>a.current)}const j=(t,n)=>{const{getProductOptionListAPI:r}=T();return k({queryKey:v.productOptionList(t),queryFn:()=>r(t),enabled:!isNaN(t)&&t>0,...n})},w=()=>{const{createOrderSheetAPI:t}=Q(),n=D({mutationFn:s=>t(s)});U();const r=async s=>await n.mutateAsync(s),i={rentalInfos:null,recurringPaymentDelivery:null,recurringPaymentLastRound:null};return{createOrderSheetAsync:r,convertOrderSheetDto:(s,S,m)=>{const l=s.map(p=>({optionInputs:null,optionNo:p.optionNo,orderCnt:p.orderCnt,productNo:p.productNo,...i}));return{cartNos:null,channelType:S,products:l,trackingKey:m||""}},createMemberOrderSheetRequest:(s,S)=>({convertOrderSheetDto:(l,O)=>{const p=O.map(c=>({optionInputs:c.optionInputs||null,optionNo:c.optionNo,orderCnt:c.orderCnt,productNo:c.productNo,...i}));return{cartNos:l,channelType:s,products:p,trackingKey:S||""}}}),createGuestOrderSheetRequest:(s,S)=>({convertOrderSheetDto:l=>{const O=l.map(c=>c.cartNo),p=l.map(c=>({optionInputs:c.optionInputs||null,optionNo:c.optionNo,orderCnt:c.orderCnt,productNo:c.productNo,...i}));return{cartNos:O,channelType:s,products:p,trackingKey:S||""}}})}},B=()=>{const t=G(),n=R(),r=P(x,e=>e.setSelectedCartNo),{isLogin:i,memberId:a}=P(F,e=>({isLogin:e.isLogin,memberId:e.memberId})),f=P(K,e=>e.addGuestCartItem),{selectedProductOptionList:d,resetProductOptionSelectStore:s}=P(H,e=>({selectedProductOptionList:e.selectedProductOptionList,resetProductOptionSelectStore:e.reset})),{createMemberCartAPI:S}=_(),m=D({mutationFn:e=>S(e)}),l=C.useCallback(e=>{if((e==null?void 0:e.options.length)===1){const u=e.options[0],N={buyPrice:u.buyPrice,optionNo:u.optionNo,optionValue:u.value,orderCnt:1,productNo:e.mallProductNo};s(N)}else s()},[s]),O=async e=>{if(d.length===0){g("error","옵션을 선택해주세요.");return}if(i){const o=d.map(u=>({optionInputs:[],productNo:u.productNo,optionNo:u.optionNo,orderCnt:u.orderCnt,channelType:null}));await m.mutateAsync(o),n.invalidateQueries({queryKey:v.getMemberCartItemCount(a)})}else d.forEach(o=>{f(o.productNo,o.optionNo,o.orderCnt)});l(e),g("success","장바구니에 담았습니다.","쇼핑계속하기","장바구니 가기").then(o=>{o.isConfirmed||t(b.CART)})},{createOrderSheetAsync:p,convertOrderSheetDto:c}=w(),E=async(e,o)=>{if(d.length===0){g("error","옵션을 선택해주세요.");return}const u=c(d,e,o),N=await p(u);N&&t(i?"".concat(b.ORDER_SHEET,"?orderSheetNo=").concat(N.orderSheetNo):"".concat(b.LOGIN,"?redirect=").concat(b.ORDER_SHEET,"?orderSheetNo=").concat(N.orderSheetNo))},I=C.useCallback(e=>{if(e){const o=e.map(u=>u.cartNo);r(o)}},[]),M=C.useCallback(e=>{if(!e)return[];const o=e.deliveryGroups.flatMap(({orderProducts:N})=>N.flatMap(({orderProductOptions:h})=>h.map(y=>({cartNo:y.cartNo,channelType:null,optionInputs:y.optionInputs,optionNo:y.optionNo,orderCnt:y.orderCnt,productNo:y.productNo})))),u=e.invalidProducts.flatMap(({orderProductOptions:N})=>N.map(h=>({cartNo:h.cartNo,channelType:null,optionInputs:h.optionInputs,optionNo:h.optionNo,orderCnt:h.orderCnt,productNo:h.productNo})));return[...o,...u]},[]);return{resetSelectedProductOption:l,onAddItemToCart:O,onPurchaseItemDirect:E,extractCartItemListFromDelivery:M,initializeSelectedCartNoList:I}};export{B as a,w as b,j as u};