"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[315,595],{2315:(e,t,r)=>{r.d(t,{findAvailableSlug:()=>n,gz:()=>c,sanitizeSlug:()=>u});var o=r(5317),s=r(8915);let l=["admin","api","app","auth","blog","dashboard","docs","help","home","login","logout","mail","news","profile","register","root","settings","signup","support","terms","privacy","about","contact","ftp","www","email","test","demo","example","sample","null","undefined","true","false","admin-panel","eventlyapp","evently","event","events","gallery","galleries","collection","collections"];async function a(e,t){let r=e.trim().toLowerCase();try{let e=(0,o.P)((0,o.rJ)(s.db,"users"),(0,o._M)("settings.slug","==",r)),l=await (0,o.GG)(e);if(l.empty||t&&l.docs[0].id===t)return!1;return!0}catch(e){if((null==e?void 0:e.code)==="permission-denied")return console.warn("checkSlugExists: permission-denied when querying slugs – assuming slug is available."),!1;return console.error("checkSlugExists: unexpected error:",e),!1}}function i(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:5,r=e.trim().toLowerCase().replace(/[^a-z0-9]/g,""),o=[];for(let e=1;e<=t;e++)o.push("".concat(r).concat(e));return["".concat(r,"-events"),"".concat(r,"-gallery"),"".concat(r,"-photos"),"the-".concat(r),"".concat(r,"-official")].forEach((e,r)=>{r30?{isValid:!1,error:"Profile slug must be no more than 30 characters long"}:/^[a-z0-9_-]+$/.test(t)?t.startsWith("-")||t.startsWith("_")||t.endsWith("-")||t.endsWith("_")?{isValid:!1,error:"Profile slug cannot start or end with hyphens or underscores"}:t.includes("--")||t.includes("__")||t.includes("-_")||t.includes("_-")?{isValid:!1,error:"Profile slug cannot contain consecutive special characters"}:l.includes(t)?{isValid:!1,error:"This profile slug is reserved and cannot be used"}:{isValid:!0}:{isValid:!1,error:"Profile slug can only contain letters, numbers, hyphens, and underscores"}}(e);if(!r.isValid)return console.log("=== validateSlug: Format validation failed:",r.error),{isValid:!1,error:r.error,suggestions:i(e)};let o=e.trim().toLowerCase();try{if(await a(o,t))return console.log("=== validateSlug: Slug is taken"),{isValid:!1,error:"This profile slug is already taken by another user",suggestions:i(o)};return console.log("=== validateSlug: Slug is available"),{isValid:!0}}catch(e){return console.error("=== validateSlug: Error during validation:",e),{isValid:!0}}}function u(e){return e.trim().toLowerCase().replace(/\s+/g,"-").replace(/[^a-z0-9-_]/g,"").replace(/[-_]+/g,"-").replace(/^[-_]+|[-_]+$/g,"").substring(0,30)}},2595:(e,t,r)=>{r.d(t,{GM:()=>b,Re:()=>m,oy:()=>f,VM:()=>u,h_:()=>v,initializeUserData:()=>d,eQ:()=>p,DO:()=>g,Yh:()=>c,uT:()=>y,V6:()=>h});var o=r(5317),s=r(7505),l=r(8915),a=r(2315);let i=async e=>{try{let t=(0,o.H9)(l.db,"users",e),r=await (0,o.x7)(t);if(!r.exists())return!0;return 2>(r.data().schemaVersion||1)}catch(e){return console.error("Error checking migration status:",e),!0}},n=async(e,t,r)=>{try{let c=(0,o.H9)(l.db,"users",e),u=await (0,o.x7)(c),d=[],g=u.exists()&&u.data()||{},f=!1,p=(null==g?void 0:g.schemaVersion)||1;if(p>=2)return{success:!0,migrated:!1};if(p<2){var s,i,n;if(!g.profile){let o=r||(null==t?void 0:t.split("@")[0])||"User",s=(0,a.sanitizeSlug)(o),l=await (0,a.findAvailableSlug)(s,e);g.profile={profileImage:g.profileImage||"",username:g.username||o,aboutText:g.aboutText||"Welcome to my link page! I'm ".concat(o," and I'm excited to share my favorite links with you. Follow me on my journey!"),usernameColor:g.usernameColor||"#2d2d2d",aboutColor:g.aboutColor||"#6b7280",verificationColor:g.verificationColor||"#9d4edd",mainFontFamily:g.mainFontFamily||"Inter",backgroundImage:g.backgroundImage||"",backgroundOverlayColor:g.backgroundOverlayColor||"#9d4edd",bottomBackgroundColor:g.bottomBackgroundColor||"#f9fafb",footerText:g.footerText||"linklyapp.xyz/".concat(l),footerTextColor:g.footerTextColor||"#6b7280",buttonBackgroundColor:g.buttonBackgroundColor||"#FFFFFF",buttonTextColor:g.buttonTextColor||"#2d2d2d"},d.push("Added profile structure"),f=!0}if(g.settings)g.settings.plans||(g.settings.plans=[{id:"free",name:"Free Plan",description:"Perfect for getting started",price:0,features:["linklyapp.xyz subdomain","Basic analytics","Unlimited links","Profile customization"]},{id:"starter",name:"Starter Plan",description:"0-10,000 monthly visitors",price:15,features:["Everything in Free","Remove Linkly branding","Custom domain support","Advanced analytics","Priority support"]},{id:"pro",name:"Pro Plan",description:"10,000+ monthly visitors",price:30,features:["Everything in Starter","Unlimited monthly visitors","Advanced customization","Team collaboration","API access","White-label solution"]}],d.push("Added plans to existing settings"),f=!0),g.settings.subscription&&g.settings.subscription.planName||(g.settings.subscription={planId:"free",planName:"Free Plan",planDescription:"Perfect for getting started with your link page",status:"active",startDate:(null===(i=g.settings.subscription)||void 0===i?void 0:i.startDate)||(0,o.O5)(),endDate:null,features:["linklyapp.xyz subdomain","Basic analytics","Unlimited links","Profile customization"]},d.push("Updated subscription structure"),f=!0);else{let l=(null===(s=g.profile)||void 0===s?void 0:s.username)||r||(null==t?void 0:t.split("@")[0])||"User",i=(0,a.sanitizeSlug)(l),n=await (0,a.findAvailableSlug)(i,e);g.settings={connectedDomain:"linklyapp.xyz/".concat(n),slug:n,activePlan:"free",subscription:{planId:"free",planName:"Free Plan",planDescription:"Perfect for getting started with your link page",status:"active",startDate:(0,o.O5)(),endDate:null,features:["linklyapp.xyz subdomain","Basic analytics","Unlimited links","Profile customization"]},plans:[{id:"free",name:"Free Plan",description:"Perfect for getting started",price:0,features:["linklyapp.xyz subdomain","Basic analytics","Unlimited links","Profile customization"]},{id:"starter",name:"Starter Plan",description:"0-10,000 monthly visitors",price:15,features:["Everything in Free","Remove Linkly branding","Custom domain support","Advanced analytics","Priority support"]},{id:"pro",name:"Pro Plan",description:"10,000+ monthly visitors",price:30,features:["Everything in Starter","Unlimited monthly visitors","Advanced customization","Team collaboration","API access","White-label solution"]}]},d.push("Added settings structure with updated plans"),f=!0}if(g.settings&&!g.settings.slug){let o=(null===(n=g.profile)||void 0===n?void 0:n.username)||r||(null==t?void 0:t.split("@")[0])||"User",s=(0,a.sanitizeSlug)(o),l=await (0,a.findAvailableSlug)(s,e);g.settings.slug=l,g.settings.connectedDomain="linklyapp.xyz/".concat(l),g.profile&&(g.profile.footerText="linklyapp.xyz/".concat(l)),d.push("Added slug to settings"),f=!0}for(let e of["profileImage","username","aboutText","usernameColor","aboutColor","verificationColor","mainFontFamily","backgroundImage","backgroundOverlayColor","bottomBackgroundColor","footerText","footerTextColor","buttonBackgroundColor","buttonTextColor"])void 0!==g[e]&&(delete g[e],d.push("Cleaned up old field: ".concat(e)),f=!0)}return f&&(g.schemaVersion=2,g.updatedAt=(0,o.O5)(),g.createdAt||(g.createdAt=(0,o.O5)(),d.push("Added createdAt timestamp")),await (0,o.BN)(c,g,{merge:!0}),d.push("Updated schema version")),{success:!0,migrated:f,changes:d}}catch(e){return console.error("Error migrating user:",e),{success:!1,migrated:!1,error:e}}},c=async(e,t)=>{try{console.log("=== saveUserProfile: Starting save process"),console.log("User ID:",e),console.log("Profile data:",t),console.log("Profile image:",t.profileImage),console.log("Background image:",t.backgroundImage);let r=(0,o.H9)(l.db,"users",e),s=Object.fromEntries(Object.entries(t).filter(e=>{let[t,r]=e;return void 0!==r}));return console.log("=== saveUserProfile: Clean profile data:",s),await (0,o.BN)(r,{profile:s,updatedAt:(0,o.O5)()},{merge:!0}),console.log("=== saveUserProfile: Profile saved successfully"),{success:!0}}catch(e){return console.error("=== saveUserProfile: Error saving profile:",e),console.error("Error code:",e.code),console.error("Error message:",e.message),{success:!1,error:e}}},u=async e=>{try{console.log("Getting profile for user:",e);let t=(0,o.H9)(l.db,"users",e),r=await (0,o.x7)(t);if(r.exists()){let e=r.data();return console.log("Profile data retrieved:",e.profile),e.profile||null}return console.log("No profile found for user"),null}catch(e){return console.error("Error getting profile:",e),null}},d=async(e,t,r,s)=>{try{console.log("Initializing user data for:",e);let c=(0,o.H9)(l.db,"users",e),u=await (0,o.x7)(c);if(u.exists()){let s=u.data();if(console.log("User data already exists"),await i(e)){console.log("User needs migration");let s=await n(e,r,t);if(s.success){let e=await (0,o.x7)(c);if(e.exists()){let t=e.data();return{success:!0,profile:t.profile,settings:t.settings,migrated:!0,changes:s.changes}}}return s}return{success:!0,profile:s.profile,settings:s.settings,migrated:!1}}console.log("Creating new user data");let d={username:t||(null==r?void 0:r.split("@")[0])||"User",aboutText:"",profileImage:"",backgroundImage:"",footerText:"eventlyapp.xyz",usernameColor:"#2D2D44",aboutColor:"#6b7280",verificationColor:"#8B5CF6",mainFontFamily:"Inter",backgroundOverlayColor:"rgba(0, 0, 0, 0.3)",bottomBackgroundColor:"#ffffff",footerTextColor:"#9ca3af",buttonBackgroundColor:"#ffffff",buttonTextColor:"#2D2D44"},g=s;if(!g){let e=(0,a.sanitizeSlug)(t||(null==r?void 0:r.split("@")[0])||"user");g=await (0,a.findAvailableSlug)(e)}let f={slug:g,connectedDomain:"",activePlan:"free",currentPlan:"free",subscription:{planId:"free",planName:"Free Plan",planDescription:"Perfect for getting started with your event galleries",status:"active",startDate:new Date,endDate:null,features:["Unlimited galleries","Basic analytics","EventlyApp watermark"]},plans:[{id:"free",name:"Free",price:0,features:["Unlimited galleries","Basic analytics","EventlyApp watermark"]},{id:"starter",name:"Starter",price:15,features:["All free features","Remove watermark","Custom domains","Better support"]},{id:"pro",name:"Pro",price:30,features:["All starter features","White label option","Priority support","Advanced analytics"]}],stripeCustomerId:null,stripeSubscriptionId:null};return await (0,o.BN)(c,{profile:d,settings:f,createdAt:(0,o.O5)(),updatedAt:(0,o.O5)()}),console.log("New user data created successfully with slug:",g),{success:!0,profile:d,settings:f,migrated:!1}}catch(e){return console.error("Error initializing user data:",e),console.error("Error code:",e.code),console.error("Error message:",e.message),{success:!1,error:e}}},g=async(e,t)=>{try{console.log("=== saveUserGalleries: Starting save process ==="),console.log("User ID:",e),console.log("Number of galleries to save:",t.length),console.log("Galleries data:",t);let r=(0,o.P)((0,o.rJ)(l.db,"galleries"),(0,o._M)("userId","==",e)),s=await (0,o.GG)(r);console.log("=== saveUserGalleries: Found existing galleries:",s.size);let a=new Map;for(let r of(s.forEach(e=>{let t=e.data();a.set(t.id,e.id),console.log("=== saveUserGalleries: Existing gallery:",t.id,"->",e.id)}),t))try{console.log("=== saveUserGalleries: Processing gallery:",r.id);let t=a.get(r.id),s=Object.fromEntries(Object.entries(r).filter(e=>{let[t,r]=e;return void 0!==r}));if(s.userId=e,s.updatedAt=(0,o.O5)(),t){console.log("=== saveUserGalleries: Updating existing gallery:",t);let e=(0,o.H9)(l.db,"galleries",t);await (0,o.mZ)(e,s),a.delete(r.id)}else{console.log("=== saveUserGalleries: Creating new gallery"),s.createdAt=(0,o.O5)();let e=(0,o.H9)((0,o.rJ)(l.db,"galleries"));await (0,o.BN)(e,s)}console.log("=== saveUserGalleries: Successfully processed gallery:",r.id)}catch(e){throw console.error("=== saveUserGalleries: Error processing gallery ".concat(r.id,":"),e),e}for(let[e,t]of a)try{console.log("=== saveUserGalleries: Deleting removed gallery:",e);let r=(0,o.H9)(l.db,"galleries",t);await (0,o.kd)(r)}catch(t){console.error("=== saveUserGalleries: Error deleting gallery ".concat(e,":"),t)}return console.log("=== saveUserGalleries: All galleries saved successfully ==="),{success:!0}}catch(e){return console.error("=== saveUserGalleries: Error saving galleries:",e),{success:!1,error:e}}},f=async e=>{try{console.log("Getting galleries for user:",e);let t=(0,o.P)((0,o.rJ)(l.db,"galleries"),(0,o._M)("userId","==",e)),r=await (0,o.GG)(t);console.log("Found",r.size,"galleries");let s=r.docs.map(e=>{let t=e.data();return{id:t.id||e.id,...t}});return console.log("Returning galleries:",s),s}catch(e){return console.error("Error getting galleries:",e),[]}},p=async(e,t)=>{try{console.log("Saving collections for user:",e),console.log("Number of collections:",t.length);let r=(0,o.P)((0,o.rJ)(l.db,"collections"),(0,o._M)("userId","==",e)),s=await (0,o.GG)(r),a=new Map;for(let r of(s.forEach(e=>{a.set(e.data().id,e.id)}),t))try{let t=a.get(r.id);if(t){let s=(0,o.H9)(l.db,"collections",t);await (0,o.mZ)(s,{...r,userId:e,updatedAt:(0,o.O5)()}),a.delete(r.id)}else{let t=(0,o.H9)((0,o.rJ)(l.db,"collections"));await (0,o.BN)(t,{...r,userId:e,createdAt:(0,o.O5)(),updatedAt:(0,o.O5)()})}}catch(e){console.error("Error processing collection ".concat(r.id,":"),e)}for(let[e,t]of a)try{let e=(0,o.H9)(l.db,"collections",t);await (0,o.kd)(e)}catch(t){console.error("Error deleting collection ".concat(e,":"),t)}return console.log("Collections saved successfully"),{success:!0}}catch(e){return console.error("Error saving collections:",e),{success:!1,error:e}}},m=async e=>{try{console.log("Getting collections for user:",e);let t=(0,o.P)((0,o.rJ)(l.db,"collections"),(0,o._M)("userId","==",e)),r=await (0,o.GG)(t);console.log("Found",r.size,"collections");let s=r.docs.map(e=>{let t=e.data();return{id:t.id||e.id,...t}});return console.log("Returning collections:",s),s}catch(e){return console.error("Error getting collections:",e),[]}},y=async(e,t)=>{try{console.log("Saving settings for user:",e);let r=(0,o.H9)(l.db,"users",e);return await (0,o.BN)(r,{settings:t,updatedAt:(0,o.O5)()},{merge:!0}),console.log("Settings saved successfully"),{success:!0}}catch(e){return console.error("Error saving settings:",e),{success:!1,error:e}}},v=async e=>{try{let t=(0,o.H9)(l.db,"users",e),r=await (0,o.x7)(t);if(r.exists())return r.data().settings||null;return null}catch(e){return console.error("Error getting settings:",e),null}},b=async e=>{try{console.log("Getting analytics for user:",e);let t=(0,o.P)((0,o.rJ)(l.db,"analytics"),(0,o._M)("userId","==",e),(0,o._M)("type","==","gallery_view")),r=await (0,o.GG)(t),s=r.size,a=(0,o.P)((0,o.rJ)(l.db,"analytics"),(0,o._M)("userId","==",e),(0,o._M)("type","==","view")),i=(await (0,o.GG)(a)).size,n=new Map;r.forEach(e=>{let t=e.data().galleryId;t&&n.set(t,(n.get(t)||0)+1)});let c=(await f(e)).map(e=>({id:e.id,text:e.title,clicks:n.get(e.id)||0})).filter(e=>e.clicks>0).sort((e,t)=>t.clicks-e.clicks).slice(0,5);return console.log("Analytics data:",{totalGalleryViews:s,totalPageViews:i,topGalleries:c}),{totalClicks:s,totalViews:i,topLinks:c}}catch(e){return console.error("Error getting analytics:",e),{totalClicks:0,totalViews:0,topLinks:[]}}},h=async(e,t,r)=>{try{let o=t.name.split(".").pop(),a="".concat(Date.now(),".").concat(o),i=(0,s.KR)(l.IG,"users/".concat(e,"/").concat(r,"/").concat(a)),n=await (0,s.D)(i,t);return await (0,s.qk)(n.ref)}catch(e){return console.error("Error uploading image:",e),null}}}}]);