1import{a as x}from"/build/_shared/chunk-DEEIYPRZ.js";import{a as tt,f as et}from"/build/_shared/chunk-4EGRZSE7.js";import{f as Y}from"/build/_shared/chunk-AJ5VSEHK.js";import{f as q}from"/build/_shared/chunk-5L6KHJFX.js";import{j as J}from"/build/_shared/chunk-D7PKK72C.js";import{a as Z}from"/build/_shared/chunk-QO23KVT4.js";import{y as b}from"/build/_shared/chunk-O2G27HIH.js";import{t as U}from"/build/_shared/chunk-ZQZP2RIC.js";import{A as Q,h as N,q as j,z as X}from"/build/_shared/chunk-ZSEETMYI.js";import{a as C}from"/build/_shared/chunk-JHYR5QDS.js";import{v as W}from"/build/_shared/chunk-XHXRPQY5.js";import{O as M}from"/build/_shared/chunk-FES3B5QL.js";import{p as H}from"/build/_shared/chunk-XC7SPKRF.js";import{a as G}from"/build/_shared/chunk-G3QUQOKW.js";import{a as lt}from"/build/_shared/chunk-F3RAJA4O.js";import{e as $}from"/build/_shared/chunk-O3BUCIVF.js";var f=$(lt());var B={items:[]};Q();var it=e=>{let a=[],l=[];if(Array.isArray(e.key)&&e.updateMultipleStatus)e.key.forEach(y=>{let m={correlationId:Math.random().toString(16).slice(2),action:e.action,item:e.title||e.attributes?{title:e.title,attributes:e.attributes,type:e.type}:void 0,key:y,title:e.title,attributes:e.attributes};e.type==="OFFER"?a.push(m):l.push(m)});else if(Array.isArray(e.actions))l.push(...e.actions);else{let y=e.type==="OFFER"?a:l;e.action==="UPDATE_ITEM_ATTRIBUTES"?(y.push({correlationId:Math.random().toString(16).slice(2),action:e.action,key:e.key,attributes:e.attributes}),y.push({correlationId:Math.random().toString(16).slice(2),action:"UPDATE_ITEM_TITLE",key:e.key,title:e.title})):e.action==="UPDATE_ITEM_TITLE"?y.push({correlationId:Math.random().toString(16).slice(2),action:e.action,key:e.key,title:e.title}):e.action==="ADD_ITEM"?y.push({correlationId:Math.random().toString(16).slice(2),action:e.action,key:e.key,item:{title:e.title,attributes:e.attributes,type:e.type},title:e.title,attributes:e.attributes}):y.push({correlationId:Math.random().toString(16).slice(2),action:e.action,key:e.key})}return{offerActions:a,productActions:l}};async function ot(){var e,a,l,y;try{let m=window.ENV.apiBaseURL,E=await b(`${m}/shopping-list/api/v1/shopping-lists/default`,void 0,!0),_=(e=E==null?void 0:E.items)==null?void 0:e.map(d=>{var R;return(R=d==null?void 0:d.attributes)==null?void 0:R.productId}).filter(d=>!!d),s=await X();if(!s){let d=await Y();s=((a=d==null?void 0:d.attributes)==null?void 0:a.selectedStore)||""}let o=await et(_,s),c={...E,items:(l=E==null?void 0:E.items)==null?void 0:l.map(d=>{let R=o==null?void 0:o.find(A=>{var F;return A.id===((F=d==null?void 0:d.attributes)==null?void 0:F.productId)});return{...d,product:R}})},{shoppingListOffers:O}=Z;if(O){let d=await b(`${m}
1/shopping-list/api/v1/shopping-lists/offers`,void 0,!0);c.items=(y=c==null?void 0:c.items)==null?void 0:y.concat(d==null?void 0:d.items)}return c}catch(m){console.log("Shopping Error: ",m);return}}async function Et(e,a){try{let l=window.ENV.apiBaseURL,y=await e.json(),{offerActions:m,productActions:E}=it(y),_={revision:a||"",actions:E},s={revision:a||"",actions:m},o={actionResults:[]};if(_.actions.length>0){let c=await b(`${l}/shopping-list/api/v1/shopping-lists/default/actions`,e,!0,void 0,"POST",JSON.stringify(_));c!=null&&c.actionResults&&o.actionResults.push(...c.actionResults)}if(s.actions.length>0){let c=await b(`${l}/shopping-list/api/v1/shopping-lists/offers/actions`,e,!0,void 0,"POST",JSON.stringify(s));c!=null&&c.actionResults&&(c==null||c.actionResults.forEach(O=>{let d=s.actions.find(R=>R.correlationId===O.correlationId);d&&(O.item=d)}),o.actionResults.push(...c.actionResults))}return o}catch(l){console.log("Shopping Error: ",l);return}}async function Pt(e,a){try{let l=window.ENV.apiBaseURL,y=await e.json(),{offerActions:m,productActions:E}=y.items.reduce((s,o)=>{let c={correlationId:Math.random().toString(16).slice(2),action:"DELETE_ITEM"};return o.type==="OFFER"?s.offerActions.push({...c,key:o.key}):s.productActions.push({...c,key:o.key}),s},{offerActions:[],productActions:[]}),_={actionResults:[]};if(m.length){let s=await b(`${l}
1/shopping-list/api/v1/shopping-lists/offers/actions`,e,!0,void 0,"POST",JSON.stringify({revision:a||"",actions:m}));s!=null&&s.actionResults&&_.actionResults.push(...s.actionResults)}if(E.length){let s=await b(`${l}/shopping-list/api/v1/shopping-lists/default/actions`,e,!0,void 0,"POST",JSON.stringify({revision:a||"",actions:E}));s!=null&&s.actionResults&&_.actionResults.push(...s.actionResults)}return _}catch(l){console.error("Shopping Error: ",l);return}}function Lt(e){let a=new Date,l=new Date(e.validFrom),y=new Date(e.validTo);return isNaN(l.getTime())||isNaN(y.getTime())||l>y?!1:a>=l&&a<=y}function ut(e){return e&&typeof e.offerId=="string"?e:null}function z(e){return e&&typeof e.productId=="string"?e:null}function k(e){return e.type==="OFFER"&&!!ut(e.attributes)}function K(e){return!!z(e.attributes)}function D(e){return"flyerId"in e&&typeof e.flyerId=="string"}function V(e){return"id"in e&&typeof e.id=="string"}Q();var v=$(G()),nt=f.default.createContext({shoppingList:B.items,revision:B.revision,isInitialSync:!1,setIsInitialSync:()=>{},setList:()=>{}}),Bt=({children:e})=>{let[a,l]=H({key:"shopping-list-data",defaultValue:B}),[y,m]=(0,f.useState)(!1);return(0,v.jsx)(nt.Provider,{value:{shoppingList:a==null?void 0:a.items,revision:a==null?void 0:a.revision,isInitialSync:y,setIsInitialSync:m,setList:l},children:e})},Vt=()=>{let e=(0,f.useContext)(nt),a=(0,f.useRef)(null),l=(0,f.useRef)(!1),[y,m]=(0,f.useState)(!1),[E,_]=(0,f.useState)(!1),s=q(),o=W({key:"shopping-list"}),{user:{isLoggedIn:c}}=J(),O=(0,f.useCallback)(()=>e.setList(t=>({items:[]})),[e]),d=(0,f.useCallback)(t=>{e.setList(r=>{var S,g,u;let i=(r==null?void 0:r.items)||[];if(t.action==="ADD_ITEM"){if(i.find(h=>{var T,L;return K(h)?h.attributes.productId===((T=t.product)==null?void 0:T.id):h.title===(t.title||((L=t==null?void 0:t.item)==null?void 0:L.title)||"")}))return r;let p={status:"ACTIVE",key:`${Date.now()}-${Math.random().toString(36).substring(2,11)}`,title:t.title||((S=t==null?void 0:t.item)==null?void 0:S.title)||"",product:t.product};return(g=t.product)!=null&&g.id&&(p.attributes={productId:(u=t.product)==null?void 0:u.id.toString()}),{...r,items:[...i,p]}}switch(t.action){case"UPDATE_ITEM_TITLE":case"UPDATE_ITEM_ATTRIBUTES":i=i.map(n=>{var p,h,T;if(n.key===t.key){let L;return(p=t.product)!=null&&p.id&&(L={productId:(h=t.product)==null?void 0:h.id.toString()}),{...n,title:(t==null?void 0:t.title)||((T=t==null?void 0:t.item)==null?void 0:T.title)||"",attributes:L,product:t==null?void 0:t.product}}return n});break;case"SET_ITEM_COMPLETED":case"SET_ITEM_ACTIVE":i=i.map(n=>n.key===t.key?{...n,status:t.action==="SET_ITEM_COMPLETED"?"COMPLETED":"ACTIVE"}:n);break;case"DELETE_ITEM":let I=i.findIndex(n=>(n==null?void 0:n.key)===(t==null?void 0:t.key));I!==-1&&i.splice(I,1);break;default:return r}return{items:[...i]}})},[e]),R=(0,f.useCallback)(async({items:t=[],revision:r,shoppingListData:i})=>{if(o.state==="idle"&&(i!=null&&i.items))try{let S=new Map(i.items.map(u=>[u.title.toLowerCase(),u])),g=t.filter(u=>!S.has(u.title.toLowerCase()));if(g.length>0){let u={revision:r,actions:g.map(n=>{var p;return{correlationId:n.key,action:"ADD_ITEM",key:n.key,item:{title:n.title,attributes:{...n.attributes,productId:(p=n.product)==null?void 0:p.id}},product:n.product}})};o.submit(u,{method:"POST",action:"/shopping-list",encType:"application/json"}),m(!0);let I=N([...i.items,...g]);e==null||e.setList({items:I,revision:r})}e==null||e.setIsInitialSync(!0)}catch(S){console.error("Error syncing with API:",S);let g=N([...t]);e==null||e.setList({items:g,revision:r})}},[e,o]),A=(0,f.useCallback)(async()=>{var t;if(!(l.current||!c||E&&e.isInitialSync)){l.current=!0,m(!0);try{let r=j(),i=await ot();if(i!=null&&i.revision){let S=new Map(i.items.filter(K).map(n=>{var p;return[(p=n.attributes)==null?void 0:p.productId,n]})),g=new Map(i.items.filter(n=>!z(n.attributes)&&!k(n)).map(n=>[n.title.toLowerCase(),n])),u=new Map(i.items.filter(k).map(n=>[n.attributes.offerId,n])),I=((t=r==null?void 0:r.items)==null?void 0:t.filter(n=>{var p;return(p=n.product)!=null&&p.id?!S.has(n.product.id):k(n)?!u.has(n.attributes.offerId):!g.has(n.title.toLowerCase())}))||[];if(I.length>0){let n={revision:i.revision,actions:I.map(p=>{var h,T;return{correlationId:p.key,action:"ADD_ITEM",key:p.key,item:{title:p.title,attributes:(h=p.product)!=null&&h.id?{productId:p.product.id}:void 0},product:p.product,type:(T=p.type)!=null?T:"PRODUCT"}})};try{o.submit(n,{method:"POST",action:"/shopping-list",encType:"application/json"});let p=N([...i.items,...I]);e==null||e.setList({items:p,revision:i.revision})}catch(p){console.error("Failed to sync items with server:",p),e==null||e.setList({items:(r==null?void 0:r.items)||[],revision:i.revision})}}else e==null||e.setList({items:i.items,revision:i.revision})}_(!0),e==null||e.setIsInitialSync(!0)}catch(r){console.error("Error during initial sync:",r);let i=j();i!=null&&i.items&&(e==null||e.setList({items:i.items,revision:i.revision}))}finally{l.current=!1,m(!1)}}},[e,o,E,c]),F=(0,f.useCallback)(async t=>{var i,S,g;await A();let r=(t.key||Math.random().toString(16).slice(2)).toString();if(d({action:t.action,correlationId:r,key:r,item:{title:(i=t.title)!=null?i:"",type:t.type},product:t.product}),c){let u={key:r,action:t.action,title:(S=t.title)!=null?S:"",type:t.type};t.action==="ADD_ITEM"?((g=t.product)!=null&&g.id&&(u.attributes={productId:t.product.id}),s({eventName:"shopping_list_one_item",faroEventProperties:{searchTerm:t.title,searchType:"product"},logType:"FARO"})):t.key&&t.action==="UPDATE_ITEM_ATTRIBUTES"&&t.product.id&&(u.attributes={productId:t.product.id});try{o.submit(u,{method:"POST",action:"/shopping-list",encType:"application/json"})}catch(I){console.error("Failed to sync item with server:",I)}}t.product&&t.product.hasCoupon&&(t.action==="ADD_ITEM"||t.action==="UPDATE_ITEM_ATTRIBUTES")&&s({eventName:"shopping_list_coupon_added",faroEventProperties:{itemId:t.product.id},logType:"FARO"})},[A,o,c,s,d]),rt=(0,f.useCallback)(async(t,r)=>{var h,T,L,w;await A();let i=D(t)?t.tapTargetId:t.id;if((h=e==null?void 0:e.shoppingList)==null?void 0:h.find(P=>P.product&&r==="PRODUCT"&&P.product.id===i||P.attributes&&k(P)&&i===P.attributes.offerId)){C.show({icon:(0,v.jsx)(U,{role:"presentation",size:"1rem"}),message:(0,v.jsx)(M,{fs:"16px",fw:500,children:`${t.name} is already in your Shopping list`}),autoClose:5e3,id:"shopping-list",className:"shopping-list-notification"});return}let g=Math.random().toString(16).slice(2),u;V(t)?u={productId:i}:u={offerId:i,imageUrl:t.externalUrl,description:t.description,brand:t.brand,subtitle:t.subtitle,regularPrice:t.regularPrice,offerDescription:t.offerDescription,flyerValidDates:t.flyerValidDates,productIds:t.productIds,flyerId:t.flyerId,flyerPage:String(t.flyerPage),validFrom:t.validFrom,validTo:t.validTo};let I={key:g,title:(T=t.name)!=null?T:"",status:"ACTIVE",attributes:u,type:r};if(V(t)&&(I.product=t),D(t)){let P={itemId:t.tapTargetId,flyerId:t.flyerId,flyerPage:String(t.flyerPage)};s({eventName:"offer_add_to_list_attempt",faroEventProperties:P,gaEventProperties:P,logType:"ALL"})}let n=[...(L=e==null?void 0:e.shoppingList)!=null?L:[],I];if(e==null||e.setList({items:n,revision:e==null?void 0:e.revision}),a.current={name:t.name||"",id:i,action:"ADD_ITEM"},c){let P={key:g,action:"ADD_ITEM",title:(w=t.name)!=null?w:"",type:r,attributes:D(t)?{offerId:i,imageUrl:t.externalUrl,description:t.description,brand:t.brand,subtitle:t.subtitle,regularPrice:t.regularPrice,offerDescription:t.offerDescription,validRange:t.subtitleNote,flyerValidDates:t.flyerValidDates,productIds:t.productIds,flyerId:t.flyerId,flyerPage:String(t.flyerPage),type:"offer"}:{productId:i}};r==="PRODUCT"&&(s({eventName:"product_list_attempt",faroEventProperties:{productId:i},gaEventProperties:{productId:i},logType:"ALL"}),s({eventName:x.product_list_load_attempt,logType:"AMPLITUDE",faroEventProperties:{productId:i}}));try{o.submit(P,{method:"POST",action:"/shopping-list",encType:"application/json"})}catch(dt){console.error("Failed to add item to server:",dt),e==null||e.setList({items:e.shoppingList||[],revision:e.revision})}}let p={itemId:D(t)?t.tapTargetId:t.id,listActionType:"Add",listItemType:r,listItemName:t.name};if(s({eventName:"list_item_action",logType:"ALL",gaEventProperties:p,faroEventProperties:p,vibesEventProperties:{action:"add",payload:{id:p.itemId,brand:t.brand,name:t.name,price:tt(D(t)?t.regularPrice:t.basePrice)}}}),V(t)&&t.hasCoupon&&s({eventName:"shopping_list_coupon_added",faroEventProperties:{itemId:t.id},logType:"FARO"}),!c){if(D(t)){let P={itemId:t.tapTargetId,flyerId:t.flyerId,flyerPage:String(t.flyerPage)};s({eventName:"offer_add_to_list_success",faroEventProperties:P,gaEventProperties:P,logType:"ALL"})}C.show({icon:(0,v.jsx)(U,{role:"presentation",size:"1rem"}),message:(0,v.jsx)(M,{fs:"16px",fw:500,children:`${t.name} was added to your Shopping list`}),autoClose:5e3,id:"shopping-list",className:"shopping-list-notification"})}},[e,A,c,o,s]),st=(0,f.useCallback)(async(t,r,i)=>{var u,I,n;await A();let S=(n=(I=(u=e==null?void 0:e.shoppingList)==null?void 0:u.find(p=>{var h;return i==="PRODUCT"?((h=p.product)==null?void 0:h.id)===t:i==="OFFER"?k(p)&&p.attributes.offerId===t:!1}))==null?void 0:I.key)!=null?n:"";if(c){let p={key:S,action:"DELETE_ITEM",type:i};t&&i==="PRODUCT"&&(s({eventName:"product_list_attempt",faroEventProperties:{productId:t},gaEventProperties:{productId:t},logType:"ALL"}),s({eventName:x.product_list_load_attempt,logType:"AMPLITUDE",faroEventProperties:{productId:t}})),o.submit(p,{method:"POST",action:"/shopping-list",encType:"application/json"})}d({action:"DELETE_ITEM",correlationId:Math.random().toString(16).slice(2),key:S}),a.current={name:r,id:t,action:"DELETE_ITEM"};let g={itemId:t,listActionType:"Delete",listItemType:i,listItemName:r};s({eventName:"list_item_action",logType:"ALL",gaEventProperties:g,faroEventProperties:g}),c||(C.show({icon:(0,v.jsx)(U,{role:"presentation",size:"1rem"}),message:(0,v.jsx)(M,{fs:"16px",fw:500,children:`The item ${a.current.name} was removed from your Shopping list`}),autoClose:5e3,id:"shopping-list",className:"shopping-list-notification"}),a.current=null)},[A,e==null?void 0:e.shoppingList,c,d,o,s]),at=(0,f.useCallback)(t=>{if(c){let r={items:t,deleteAll:!0};o.submit(r,{method:"POST",action:"/shopping-list",encType:"application/json"})}e.setList(r=>({items:[],revision:r.revision}))},[e,o,c]),pt=(0,f.useCallback)(t=>{let r=t.map(i=>i.key);if(c){let i={key:r,updateMultipleStatus:!0,action:"SET_ITEM_ACTIVE"};o.submit(i,{method:"POST",action:"/shopping-list",encType:"application/json"})}t==null||t.forEach(i=>
1{i.key&&d({action:"SET_ITEM_ACTIVE",item:{title:"",type:i.type},key:String(i.key),correlationId:Math.random().toString(16).slice(2)})})},[o,c,d]),ct=(0,f.useCallback)(async()=>(c&&await A(),(e==null?void 0:e.shoppingList)||[]),[e==null?void 0:e.shoppingList,A,c]);if((0,f.useEffect)(()=>{var r,i,S,g,u,I,n,p;if(o.state!=="idle"||!o.data||a.current===null)return;let t;if(((S=(i=(r=o.data)==null?void 0:r.data)==null?void 0:i.actionResults)==null?void 0:S.length)>0&&(t=o.data.data.actionResults.find(T=>{var L,w,P;return((L=T.item)==null?void 0:L.action)==="ADD_ITEM"&&((P=(w=T.item)==null?void 0:w.item)==null?void 0:P.type)==="OFFER"})),(g=o==null?void 0:o.data)!=null&&g.success&&o.data.data!==void 0){if(((n=(I=(u=o.data)==null?void 0:u.data)==null?void 0:I.actionResults)==null?void 0:n[0].result)==="SUCCESS"&&a.current&&(C.hide("shopping-list"),C.show({icon:(0,v.jsx)(U,{role:"presentation",size:"1rem"}),message:(0,v.jsxs)(M,{fs:"16px",fw:500,children:[a.current.action==="ADD_ITEM"&&`The item ${a.current.name} was added to your Shopping list`,a.current.action==="DELETE_ITEM"&&`The item ${a.current.name} was removed from your Shopping list`]}),autoClose:5e3,id:"shopping-list",className:"shopping-list-notification"}),a.current=null),t){let h={itemId:t.item.attributes.offerId,flyerId:t.item.attributes.flyerId,flyerPage:String(t.item.attributes.flyerPage)},T;t.result==="FAILED"?T="offer_add_to_list_failure":t.result==="SUCCESS"&&(T="offer_add_to_list_success"),T&&s({eventName:T,logType:"ALL",gaEventProperties:h,faroEventProperties:h})}}else o!=null&&o.data&&((p=o==null?void 0:o.data)==null?void 0:p.success)===!1&&a.current&&(s({eventName:"product_list_failure",faroEventProperties:{productId:a.current.id},gaEventProperties:{productId:a.current.id},logType:"ALL"}),s({eventName:x.product_list_load_failure,logType:"AMPLITUDE",faroEventProperties:{productId:a.current.id}}),a.current=null)},[o==null?void 0:o.data,o==null?void 0:o.state,s]),(0,f.useEffect)(()=>{var t,r;y&&(o.data!==void 0||o.data!==null)&&((r=(t=o.data)==null?void 0:t.actionResults)==null?void 0:r[0].result)==="SUCCESS"&&(m(!1),e.setIsInitialSync(!0))},[o.data,y,e]),!e)throw new Error("ShoppingListContext must have a provider. Please add ShoppingListContextProvider higher in the tree.");return{...e,addShoppingItemAPI:rt,deleteAllShoppingListItems:at,ensureSync:A,getShoppingListData:ct,hasSyncedOnce:E,initialSyncLoading:y,removeShoppingItemAPI:st,resetList:O,syncLocalWithAPI:R,updateLocalShoppingListItem:d,updateShoppingListItem:F,updateShoppingListItemsStatus:pt}};export{Lt as a,ut as b,k as c,K as d,ot as e,Et as f,Pt as g,Bt as h,Vt as i};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.