import{clone as C,create as m,fromBinary as T,fromJsonString as h,toBinary as w,toJsonString as s}from"@bufbuild/protobuf";import{AddressSchema as y,DraftOrderSchema as u,OrderIdSchema as I,OrderSchema as l,PhoneNumberSchema as g}from"@orderlyshop/core-client";var E="orderly.web-components.basket.v1",F="orderly.web-components.checkout-profile.v1",B="orderly.web-components.orders.v1",b=class{draft;load(){return this.draft}save(r){this.draft=r}clear(){this.draft=void 0}},v=class{constructor(r=E){this.key=r}key;load(){let r=i();if(!r)return;let t=r.getItem(this.key);if(t)try{return h(u,t)}catch{return}}save(r){let t=i();if(t)try{t.setItem(this.key,s(u,r))}catch{try{t.setItem(this.key,s(u,J(r)))}catch{}}}clear(){i()?.removeItem(this.key)}},k=class{profile;load(){return this.profile}save(r){this.profile=p(r)}clear(){this.profile=void 0}},N=class{orders=new Map;load(r){return this.orders.get(d(r))}save(r){let t=d(r.Id);t&&this.orders.set(t,r)}clear(r){if(r===void 0){this.orders.clear();return}this.orders.delete(d(r))}},P=class{constructor(r=B){this.key=r}key;load(r){let t=i();if(!t)return;let n=d(r);if(!n)return;let a=this.loadOrders(t)[n];if(a)try{return h(l,a)}catch{return}}save(r){let t=i();if(!t)return;let n=d(r.Id);if(!n)return;let o=this.loadOrders(t);try{o[n]=s(l,r)}catch{try{o[n]=s(l,j(r))}catch{return}}try{t.setItem(this.key,JSON.stringify(o))}catch{}}clear(r){let t=i();if(!t)return;if(r===void 0){t.removeItem(this.key);return}let n=d(r),o=this.loadOrders(t);delete o[n],t.setItem(this.key,JSON.stringify(o))}loadOrders(r){let t=r.getItem(this.key);if(!t)return{};try{let n=JSON.parse(t);return Object.fromEntries(Object.entries(n).filter(o=>typeof o[1]=="string"))}catch{return{}}}},D=class{constructor(r=F){this.key=r}key;load(){let r=i();if(!r)return;let t=r.getItem(this.key);if(t)try{let n=JSON.parse(t);return p({address:n.address?h(y,n.address):void 0,email:n.email,phoneNumber:n.phoneNumber?h(g,n.phoneNumber):void 0,updatedAt:n.updatedAt})}catch{return}}save(r){let t=i();if(!t)return;let n=p(r);t.setItem(this.key,JSON.stringify({address:n.address?s(y,n.address):void 0,email:n.email,phoneNumber:n.phoneNumber?s(g,n.phoneNumber):void 0,updatedAt:n.updatedAt}))}clear(){i()?.removeItem(this.key)}};function q(){return m(u,{OrderLines:[],ExtraLines:[],Errors:[],PaymentOptions:[],Discounts:[],Vouchers:[],Messages:[],Related:[]})}function J(e){let r=C(u,e);return r.Messages=[],r.Related=[],r.OrderLines.forEach(A),r.ExtraLines.forEach(A),r.Discounts.forEach(f),r.Transport=r.Transport?M(r.Transport):void 0,r}function j(e){let r=C(l,e);return O(r),r}function A(e){f(e)}function M(e){return f(e),U(e.Context?.TransportMethod),e.Features?.Items.forEach(z),e.Context?.TransportMethod?.Features.forEach(x),e}function U(e){e?.Features.forEach(x)}function z(e){f(e)}function x(e){f(e.DefaultValue)}function f(e){e&&typeof e=="object"&&"Details"in e&&delete e.Details}function O(e,r=new Set){if(!e||typeof e!="object"||r.has(e))return;if(r.add(e),Array.isArray(e)){e.forEach(n=>O(n,r));return}let t=e;delete t.Details,delete t.Payload,Object.values(t).forEach(n=>O(n,r))}function p(e){return{address:e?.address?m(y,e.address):void 0,email:typeof e?.email=="string"&&e.email.length>0?e.email:void 0,phoneNumber:e?.phoneNumber?m(g,e.phoneNumber):void 0,updatedAt:typeof e?.updatedAt=="string"&&e.updatedAt.length>0?e.updatedAt:new Date().toISOString()}}function G(e){return p({address:e?.Transport?.Address,email:e?.Transport?.Email?.Value,phoneNumber:e?.Transport?.ContactPhone})}function S(e){return K(w(I,e))}function R(e){if(e)try{return T(I,L(e))}catch{return}}function d(e){if(!e)return"";if(typeof e=="string"){let r=R(e);return r?S(r):e}return S(e)}function K(e){if(typeof btoa=="function"){let t="";for(let n of e)t+=String.fromCharCode(n);return btoa(t)}let r=globalThis.Buffer;if(!r)throw new Error("Base64 encoding is not available in this environment.");return r.from(e).toString("base64")}function L(e){let r=e.trim().replace(/\s/g,"+").replace(/-/g,"+").replace(/_/g,"/"),t=r.padEnd(Math.ceil(r.length/4)*4,"=");if(typeof atob=="function"){let o=atob(t),a=new Uint8Array(o.length);for(let c=0;c<o.length;c+=1)a[c]=o.charCodeAt(c);return a}let n=globalThis.Buffer;if(!n)throw new Error("Base64 decoding is not available in this environment.");return new Uint8Array(n.from(t,"base64"))}function i(){try{return globalThis.localStorage}catch{return}}export{v as LocalStorageBasketStore,D as LocalStorageCheckoutProfileStore,P as LocalStorageOrderStore,b as MemoryBasketStore,k as MemoryCheckoutProfileStore,N as MemoryOrderStore,G as checkoutProfileFromDraft,q as createEmptyDraftOrder,R as decodeOrderId,E as defaultBasketStorageKey,F as defaultCheckoutProfileStorageKey,B as defaultOrderStorageKey,S as encodeOrderId,p as sanitizeCheckoutProfile};
//# sourceMappingURL=stores.js.map