import*as e from"@woocommerce/stores/store-notices";import*as t from"@wordpress/interactivity";var o={1167:t=>{t.exports=e}},s={};function r(e){var t=s[e];if(void 0!==t)return t.exports;var n=s[e]={exports:{}};return o[e](n,n.exports,r),n.exports}r.d=(e,t)=>{for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);const n=(d={store:()=>t.store},l={},r.d(l,d),l),i="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",c=e=>!!e&&"object"==typeof e&&"string"==typeof e.key,a=(e,t)=>{let o=e.lists[t];return o||(o={items:[],isLoading:!1},e.lists[t]=o),o};var d,l;async function p(e,t,o={}){const s=new Headers(o.headers);s.set("Content-Type","application/json"),e.nonce&&s.set("Nonce",e.nonce);const r=await fetch(`${e.restUrl}${t}`,{...o,headers:s,cache:"no-store",credentials:"include"}),n=r.headers.get("Nonce");if(n&&(e.nonce=n),204===r.status)return null;const i=await r.text(),c=r.headers.get("Content-Type")||"",a=i&&c.includes("json")?JSON.parse(i):null;if(!r.ok){const e=(a&&"object"==typeof a&&"message"in a?String(a.message):"")||r.statusText||"Request failed.";throw new Error(e)}return a}const{state:m,actions:y}=(0,n.store)("woocommerce/shopper-lists",{actions:{*loadList(e){const t=a(m,e);t.isLoading=!0;try{const o=yield p(m,`wc/store/v1/shopper-lists/${encodeURIComponent(e)}/items`,{method:"GET"});if(!Array.isArray(o))throw new Error("Invalid shopper list response.");const s=o.filter(c);t.items=s}catch(e){console.error(e)}finally{t.isLoading=!1}},*addItem(e,t){const o=a(m,e);try{const s=yield p(m,`wc/store/v1/shopper-lists/${encodeURIComponent(e)}/items`,{method:"POST",body:JSON.stringify(t)});if(!c(s))throw new Error("Invalid shopper list item response.");const r=o.items.findIndex(e=>e.key===s.key);r>=0?o.items[r]=s:o.items.push(s)}catch(e){y.showNoticeError(e)}},*removeItem(e,t){const o=m.lists[e];if(!o)return;if(o.items.findIndex(e=>e.key===t)<0)return;try{yield p(m,`wc/store/v1/shopper-lists/${encodeURIComponent(e)}/items/${encodeURIComponent(t)}`,{method:"DELETE"})}catch(e){return void y.showNoticeError(e)}const s=o.items.findIndex(e=>e.key===t);s>=0&&o.items.splice(s,1)},*showNoticeError(e){yield Promise.resolve().then(r.bind(r,1167));const{actions:t}=(0,n.store)("woocommerce/store-notices",{},{lock:i});t.addNotice({notice:e.message,type:"error",dismissible:!0}),console.error(e)}}},{lock:i});window.addEventListener("wc-blocks_store_sync_required",e=>{const t=e.detail;if("shopper-list-item-added"!==t?.type)return;if("string"!=typeof t.slug||0===t.slug.trim().length||!c(t.item))return;const o=a(m,t.slug),s=t.item,r=o.items.findIndex(e=>e.key===s.key);r>=0?o.items[r]=s:o.items.push(s)});