import{T as b,el as h}from"./index-DFVuS8SP.js";import{getAuthHeaders as M}from"./authHeaders-C650O0HP.js";function R(){return M({"Content-Type":"application/json"})}const L=["saved-venues"];async function j(e){const t=await R(),n=await fetch(b("/users/saved-venues"),{method:"POST",headers:t,credentials:"include",body:JSON.stringify({listingId:e})});if(!n.ok){const i=await n.json().catch(()=>({message:"Failed to save venue"}));throw new Error(i.message||"Failed to save venue")}await h.invalidateQueries({queryKey:L})}async function Q(e){const t=await R(),n=await fetch(b(`/users/saved-venues/${e}`),{method:"DELETE",headers:t,credentials:"include"});if(!n.ok){const i=await n.json().catch(()=>({message:"Failed to unsave venue"}));throw new Error(i.message||"Failed to unsave venue")}await h.invalidateQueries({queryKey:L})}const I="saved",K="Saved",q=2,z=["wishlists"],G=`plec_wishlists:v${q}:`,T="plecWishlistsChanged",X=":mutation",Y=":migrated",x=":migrated-backup",N=":ping",p=new Map;class J extends Error{constructor(){super("Wishlist identity is not available yet."),this.name="WishlistIdentityUnavailableError"}}function ae(e){const t=typeof(e==null?void 0:e.publicId)=="string"?e.publicId.trim():"";return t||(typeof(e==null?void 0:e.id)=="string"?e.id.trim():"")||null}function c(e){const t=typeof e=="string"?e.trim():"";if(!t)throw new J;return t}function v(e){return`${G}${encodeURIComponent(c(e))}`}function B(e){return`${v(e)}${X}`}function C(e,t){const i=(p.get(e)??Promise.resolve()).catch(()=>{}).then(t),s=i.then(()=>{},()=>{});return p.set(e,s),i.finally(()=>{p.get(e)===s&&p.delete(e)})}function Z(e,t){const n=B(e),i=typeof navigator<"u"&&navigator.locks&&typeof navigator.locks.request=="function"?navigator.locks:null;if(i){let s=!1;return new Promise((a,o)=>{i.request(n,{mode:"exclusive"},()=>(s=!0,t())).then(a,o)}).catch(a=>{if(s)throw a;return C(n,t)})}return C(n,t)}function ee(){return M({"Content-Type":"application/json"})}async function f(e,t){const n=await ee(),i=await fetch(b(`/users/wishlists${e}`),{method:t.method,headers:n,credentials:"include",...t.body===void 0?{}:{body:JSON.stringify(t.body)}});if(!i.ok){const s=await i.json().catch(()=>null);throw new Error((s==null?void 0:s.message)||t.fallbackMessage)}return await i.json().catch(()=>null)}async function V(){const e=await f("",{method:"GET",fallbackMessage:"Failed to load wishlists"});return Array.isArray(e==null?void 0:e.wishlists)?e.wishlists:[]}async function w(e){return await P(e),await h.fetchQuery({queryKey:[...z,e],queryFn:V,staleTime:5e3})}async function O(e){await h.invalidateQueries({queryKey:[...z,e]}),await h.invalidateQueries({queryKey:L})}function A(e){if(!(typeof window>"u")){try{window.localStorage.setItem(`${v(e)}${N}`,`${Date.now()}:${Math.random().toString(36).slice(2,9)}`)}catch{}window.dispatchEvent(new CustomEvent(T,{detail:{ownerId:e}})),window.dispatchEvent(new CustomEvent("savedVenuesChanged"))}}function re(e,t){if(typeof window>"u")return()=>{};const n=c(e),i=`${v(n)}${N}`;let s=null;try{s=window.localStorage}catch{}const r=o=>{s!==null&&o.storageArea===s&&(o.key===i||o.key===null)&&t()},a=o=>{var g;((g=o.detail)==null?void 0:g.ownerId)===n&&t()};return window.addEventListener("storage",r),window.addEventListener(T,a),()=>{window.removeEventListener("storage",r),window.removeEventListener(T,a)}}function te(e){if(!e||typeof e!="object")return!1;const t=e;return typeof t.id=="string"&&t.id.trim().length>0&&typeof t.name=="string"&&t.name.trim().length>0&&typeof t.createdAt=="string"&&Array.isArray(t.listingIds)&&t.listingIds.every(n=>typeof n=="string")}function D(e,t){try{window.localStorage.setItem(`${e}:corrupt`,t)}catch{}}function ne(e){if(typeof window>"u")return[];const t=c(e),n=v(t);let i;try{i=window.localStorage.getItem(n)}catch{return[]}if(!i)return[];try{const s=JSON.parse(i);if(s.version!==q||s.ownerId!==t||!Array.isArray(s.lists))return D(n,i),[];const r=new Set,a=[];for(const o of s.lists)!te(o)||r.has(o.id)||(r.add(o.id),a.push({id:o.id,name:o.name,listingIds:Array.from(new Set(o.listingIds.filter(u=>u.trim().length>0))),createdAt:o.createdAt,...typeof o.coverImageUrl=="string"&&o.coverImageUrl.trim()?{coverImageUrl:o.coverImageUrl}:{}}));return a}catch{return D(n,i),[]}}function E(e){if(typeof window>"u")return null;try{return window.localStorage.getItem(e)}catch{return null}}function U(e,t){if(!(typeof window>"u"))try{window.localStorage.setItem(e,t)}catch{}}const S=new Map;function _(e){return e.trim().toLowerCase()}async function P(e){const t=c(e),n=v(t),i=`${n}${Y}`;if(E(i))return;const s=S.get(t);if(s)return await s;const r=Z(t,async()=>{if(E(i))return;const a=ne(t);if(a.length===0){U(i,new Date().toISOString());return}const o=await V(),u=new Map;for(const d of o)d.isDefault||u.set(_(d.name),d);for(const d of a){const k=_(d.name);let m=u.get(k);if(!m){const l=await f("",{method:"POST",body:{name:d.name},fallbackMessage:"Failed to upload wishlist"});m={...l,items:(l==null?void 0:l.items)??[]},u.set(k,m)}const $=new Set((m.items??[]).map(l=>l.listingId));for(const l of d.listingIds)$.has(l)||(await f(`/${encodeURIComponent(m.id)}/items`,{method:"POST",body:{listingId:l,...d.coverImageUrl?{imageUrl:d.coverImageUrl}:{}},fallbackMessage:"Failed to upload wishlist item"}),$.add(l))}const g=E(n);g&&U(`${n}${x}`,g),U(i,new Date().toISOString())}).catch(a=>{console.warn("Failed to upload local wishlists",a)});S.set(t,r);try{await r}finally{S.get(t)===r&&S.delete(t)}}function F(e){return e.find(t=>t.isDefault)}function W(e,t){var n;return t!==I?t:((n=F(e))==null?void 0:n.id)??null}function y(e){var t;return{id:e.isDefault?I:e.id,name:e.isDefault?K:e.name,itemCount:e.itemCount??((t=e.items)==null?void 0:t.length)??0,coverImageUrl:e.coverImageUrl,isDefault:e.isDefault}}const H={id:I,name:K,itemCount:0,isDefault:!0};async function oe(e){const t=c(e),n=await w(t),i=F(n);return[i?y(i):H,...n.filter(s=>!s.isDefault).map(y)]}async function ce(e,t){const n=c(e);return(await w(n)).filter(s=>(s.items??[]).some(r=>r.listingId===t)).map(s=>s.isDefault?I:s.id)}async function le(e,t){const n=c(e),i=await w(n),s=W(i,t);if(!s)return[];const r=i.find(a=>a.id===s);return((r==null?void 0:r.items)??[]).map(a=>({listingId:a.listingId,listingKind:a.listingKind,title:a.listingTitle,imageUrl:a.imageUrl,location:a.location,capacity:a.capacity,priceRange:a.priceRange,savedAt:a.savedAt}))}async function de(e,t){const n=c(e),i=await w(n);if(t===I){const r=F(i);return r?y(r):H}const s=i.find(r=>r.id===t);return s?y(s):null}async function ue(e,t,n,i){const s=c(e),r=await w(s),a=W(r,t);a===null?await j(n):await f(`/${encodeURIComponent(a)}/items`,{method:"POST",body:{listingId:n,...i!=null&&i.coverImageUrl?{imageUrl:i.coverImageUrl}:{}},fallbackMessage:"Failed to add to wishlist"}),await O(s),A(s)}async function fe(e,t,n){const i=c(e),s=await w(i),r=W(s,t);r===null?await Q(n):await f(`/${encodeURIComponent(r)}/items/${encodeURIComponent(n)}`,{method:"DELETE",fallbackMessage:"Failed to remove from wishlist"}),await O(i),A(i)}async function we(e,t,n){const i=c(e);await P(i);const s=await f("",{method:"POST",body:{name:t.trim()||"New wishlist",...n!=null&&n.addListingId?{listing:{listingId:n.addListingId,...n.coverImageUrl?{imageUrl:n.coverImageUrl}:{}}}:{}},fallbackMessage:"Failed to create wishlist"});return await O(i),A(i),y({...s,items:(s==null?void 0:s.items)??[]})}export{I as D,le as a,fe as b,oe as c,ue as d,we as e,de as f,ce as g,ae as r,re as s};