import{aG as Y,V as G,aw as K,ax as J,aM as Q,ah as X,cc as Z,a as ee,I as te,B as T,bG as se,bh as re,cB as _,eg as ae,f as ne}from"./index-DFVuS8SP.js";import{r as c,j as t}from"./vendor-react-DgEFbQIi.js";import{r as ie,c as oe,g as ce,s as le,b as de,d as ue,e as fe}from"./wishlists-NNbira8h.js";import{I as me}from"./ImageWithFallback-C1HTj5IA.js";import{g as H}from"./listing-utils-CICni2H8.js";import{i as ge}from"./claimRequests-54T7aAUE.js";import{p as R}from"./parseISO-C4sM-PC7.js";/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Se=Y("Package",[["path",{d:"M11 21.73a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73z",key:"1a0edw"}],["path",{d:"M12 22V12",key:"d0xqtd"}],["path",{d:"m3.3 7 7.703 4.734a2 2 0 0 0 1.994 0L20.7 7",key:"yx3hmr"}],["path",{d:"m7.5 4.27 9 5.15",key:"1c824w"}]]);function De({open:s,onOpenChange:a,listingId:r,listingTitle:l,listingImageUrl:u,onSavedChange:f}){const[m,o]=c.useState([]),[C,y]=c.useState(new Set),[v,N]=c.useState(!1),[P,L]=c.useState(null),[q,S]=c.useState(!1),[W,k]=c.useState(""),[E,F]=c.useState(!1),[U,M]=c.useState(null),{toast:w}=re(),{user:V,loading:x,isAuthReady:b}=G(),n=ie(V),p=c.useRef(n),D=c.useRef(0);p.current=n,c.useEffect(()=>{D.current+=1,o([]),y(new Set),M(null),L(null),F(!1),S(!1),k("")},[n]),c.useEffect(()=>{s&&(!b||x||!n)&&a(!1)},[x,b,a,s,n]);const I=c.useCallback(async()=>{if(!s||!r)return;const e=++D.current;if(!b||x||!n){o([]),y(new Set),M(null),N(!0);return}N(!0);try{const[i,g]=await Promise.all([oe(n),ce(n,r)]);if(e!==D.current||p.current!==n)return;o(i),y(new Set(g)),M(n)}catch(i){if(e!==D.current||p.current!==n)return;console.warn("Failed to load wishlists",i),o([]),y(new Set),M(n),w({title:"Couldn't load wishlists",description:"Please try again.",variant:"destructive"})}finally{e===D.current&&p.current===n&&N(!1)}},[x,b,r,s,n,w]);c.useEffect(()=>{I()},[I]),c.useEffect(()=>{if(!(!s||!n||!b||x))return le(n,()=>{I()})},[x,b,I,s,n]);const $=async e=>{var j,O;if(P||!n)return;const i=n;L(e);const g=C.has(e);try{if(g){if(await de(i,e,r),p.current!==i)return;y(d=>{const h=new Set(d);return h.delete(e),h}),o(d=>d.map(h=>h.id===e?{...h,itemCount:Math.max(0,h.itemCount-1)}:h)),w({title:"Removed from list",description:`Removed from ${((j=m.find(d=>d.id===e))==null?void 0:j.name)??"list"}.`})}else{if(await ue(i,e,r,{coverImageUrl:u}),p.current!==i)return;y(d=>new Set(d).add(e)),o(d=>d.map(h=>h.id===e?{...h,itemCount:h.itemCount+1,coverImageUrl:h.coverImageUrl||u}:h)),w({title:"Saved to list",description:`Added to ${((O=m.find(d=>d.id===e))==null?void 0:O.name)??"list"}.`})}f==null||f()}catch(d){w({title:"Something went wrong",description:d instanceof Error?d.message:"Please try again.",variant:"destructive"})}finally{p.current===i&&L(null)}},A=async()=>{const e=W.trim();if(!e||E||!n)return;const i=n;F(!0);try{const g=await fe(i,e,{addListingId:r,coverImageUrl:u});if(p.current!==i)return;o(j=>[...j,{...g,itemCount:1}]),y(j=>new Set(j).add(g.id)),k(""),S(!1),w({title:"Wishlist created",description:`"${g.name}" created and listing added.`}),f==null||f()}catch(g){w({title:"Couldn't create list",description:g instanceof Error?g.message:"Please try again.",variant:"destructive"})}finally{p.current===i&&F(!1)}},z=!!n&&b&&!x&&U===n;return t.jsx(K,{open:s&&!!n&&b&&!x,onOpenChange:a,children:t.jsxs(J,{className:"max-w-md grid gap-0 p-0 overflow-hidden",hideCloseButton:!1,children:[t.jsx("div",{className:"p-6 pb-0",children:t.jsx(Q,{className:"text-left",children:t.jsx(X,{className:"text-xl font-semibold",children:"Save to wishlist"})})}),v||!z?t.jsx("div",{className:"px-6 grid grid-cols-2 gap-4 min-h-[220px]",children:[1,2,3,4].map(e=>t.jsxs("div",{className:"rounded-2xl overflow-hidden bg-gray-100 animate-pulse border border-gray-100",children:[t.jsx("div",{className:"aspect-[4/3]"}),t.jsxs("div",{className:"p-4 space-y-2",children:[t.jsx("div",{className:"h-4 bg-gray-200 rounded w-3/4"}),t.jsx("div",{className:"h-3 bg-gray-200 rounded w-1/2"})]})]},e))}):t.jsxs(t.Fragment,{children:[t.jsx("div",{className:"px-6 pt-4 grid grid-cols-2 gap-4 max-h-[300px] overflow-y-auto pb-4",children:m.map(e=>{const i=C.has(e.id),g=P===e.id;return t.jsxs("button",{type:"button",onClick:()=>$(e.id),disabled:g,className:ee("rounded-2xl overflow-hidden text-left transition-all duration-200","border-2 focus:outline-none focus:ring-2 focus:ring-brand focus:ring-offset-2 focus:ring-offset-background","hover:shadow-md active:scale-[0.98]",i?"border-brand shadow-sm ring-2 ring-brand/20":"border-gray-200 hover:border-gray-300 bg-white"),children:[t.jsxs("div",{className:"relative aspect-[4/3] bg-gray-100 overflow-hidden",children:[e.itemCount>0&&e.coverImageUrl?t.jsx(me,{src:e.coverImageUrl,alt:"",className:"w-full h-full object-cover"}):t.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-gray-50",children:t.jsx("span",{className:"text-4xl text-gray-300","aria-hidden":!0,children:"♥"})}),i&&t.jsx("span",{className:"absolute top-2.5 right-2.5 w-8 h-8 rounded-full bg-gray-900 flex items-center justify-center text-white shadow-lg","aria-hidden":!0,children:t.jsx(Z,{className:"h-4 w-4",strokeWidth:2.5})}),g&&t.jsx("div",{className:"absolute inset-0 bg-white/50 flex items-center justify-center","aria-hidden":!0,children:t.jsx("span",{className:"text-sm font-medium text-gray-600",children:"Updating…"})})]}),t.jsxs("div",{className:"p-4 bg-white",children:[t.jsx("p",{className:"font-semibold text-gray-900 truncate text-sm",children:e.name}),t.jsxs("p",{className:"text-xs text-gray-500 mt-0.5",children:[e.itemCount," ",(e.itemCount===1,"saved")]})]})]},e.id)})}),t.jsx("div",{className:"border-t border-gray-100 p-4 bg-gray-50/50",children:q?t.jsxs("div",{className:"flex gap-2",children:[t.jsx(te,{placeholder:"Wishlist name",value:W,onChange:e=>k(e.target.value),onKeyDown:e=>{e.key==="Enter"&&A(),e.key==="Escape"&&S(!1)},className:"flex-1 rounded-xl border-gray-200 bg-white",autoFocus:!0}),t.jsx(T,{size:"sm",onClick:A,disabled:!W.trim()||E,className:"rounded-xl shrink-0",children:E?"Creating…":"Create"}),t.jsx(T,{size:"sm",variant:"ghost",onClick:()=>{S(!1),k("")},className:"rounded-xl shrink-0",children:"Cancel"})]}):t.jsxs(T,{variant:"outline",className:"w-full rounded-xl h-11 border-gray-200 bg-white hover:bg-gray-50 hover:border-gray-300 font-medium",onClick:()=>S(!0),children:[t.jsx(se,{className:"mr-2 h-4 w-4"}),"Create new wishlist"]})})]})]})})}function he(s,a){const r=_(s),l=_(a),u=B(r,l),f=Math.abs(ae(r,l));r.setDate(r.getDate()-u*f);const m=+(B(r,l)===-u),o=u*(f-m);return o===0?0:o}function B(s,a){const r=s.getFullYear()-a.getFullYear()||s.getMonth()-a.getMonth()||s.getDate()-a.getDate()||s.getHours()-a.getHours()||s.getMinutes()-a.getMinutes()||s.getSeconds()-a.getSeconds()||s.getMilliseconds()-a.getMilliseconds();return r<0?-1:r>0?1:r}function ye(s){var u;if(!ge(s))return;const a=H(s)??s.draftData,r=a==null?void 0:a.plecMetadata,l=(u=s.draftData)==null?void 0:u.onboardingMetadata;return(r==null?void 0:r.partnerSince)||(r==null?void 0:r.onboardingDate)||(l==null?void 0:l.claimedAt)||((l==null?void 0:l.source)==="scraper"?void 0:s.createdAt)||void 0}function pe(s){if(!s)return;const a=R(s);return Number.isNaN(a.getTime())?void 0:ne(a,"MMMM yyyy")}function ke(s){var C,y;const a=[],r=H(s)??s.draftData,{createdAt:l,listingKind:u}=s,f=r==null?void 0:r.plecMetadata,m=r==null?void 0:r.serviceData;f!=null&&f.isFeatured&&a.push({type:"plec_featured",label:"Featured",priority:1,category:"plec",showOnCard:!0});const o=ye(s);if(o){const v=pe(o)??o;a.push({type:"hosting_since",label:`Hosting with PLEC since ${v}`,priority:1,category:"plec",showOnCard:!1,metadata:{sinceDate:o}})}if(l){const v=R(l);he(new Date,v)<=30&&a.push({type:"new",label:"New",priority:3,category:"status",showOnCard:!0})}return u==="service"&&(m==null?void 0:m.travelPolicy)==="travels_to_you"&&a.push({type:"travels_to_you",label:"Travels to You",priority:4,category:"feature",showOnCard:!1}),u==="service"&&((C=m==null?void 0:m.requirements)!=null&&C.insurance)&&(((y=m.requirements.licenses)==null?void 0:y.length)??0)>0&&a.push({type:"licensed_insured",label:"Licensed & Insured",priority:4,category:"feature",showOnCard:!1}),a.sort((v,N)=>v.priority-N.priority)}export{Se as P,De as S,ke as c,pe as f,ye as r};