1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[949],{7786:function(e,t,a){a.d(t,{H:function(){return m},a:function(){return f}});var i=a(7437),r=a(2265),o=a(5735),n=a(9506),s=a(6463),l=a(6763),c=a(9842);let d=(0,r.createContext)(void 0),u="[email protected]";function m(e){let{children:t}=e,[a,m]=(0,r.useState)(null),[f,g]=(0,r.useState)(null),[p,h]=(0,r.useState)(!0),[w,y]=(0,r.useState)(null),v=(0,s.useRouter)(),{toast:b}=(0,l.pm)(),A=(0,r.useRef)(null),k=(0,r.useCallback)(async e=>{var t,a,i;let r=(0,c.JU)(n.db,"users",e.uid);try{let a=await (0,c.QT)(r);if(a.exists()){let t=a.data(),i=!1,o={};if(e.email===u&&!0!==t.isAdmin&&(console.log("Admin user ".concat(e.email," (UID: ").concat(e.uid,") found in Firestore, but isAdmin is not true. Attempting to update.")),t.isAdmin=!0,o.isAdmin=!0,i=!0),void 0===t.isMember&&(t.isMember=!1,o.isMember=!1,i=!0),void 0===t.loggedAscents&&(t.loggedAscents={},o.loggedAscents={},i=!0),void 0===t.baggedMunros&&(t.baggedMunros=[],o.baggedMunros=[],i=!0),i){o.updatedAt=(0,c.Bt)();try{await (0,c.r7)(r,o),b({title:"Profile Synced",description:"User ".concat(e.email,"'s profile data has been synced/updated."),duration:3e3,variant:"default"}),console.log("User profile for UID ".concat(e.uid," updated in Firestore with consistent data."))}catch(a){console.error("Failed to update/sync profile for ".concat(e.email," (UID: ").concat(e.uid,") in Firestore:"),a);let t="Could not persist profile updates for ".concat(e.email,".");a instanceof c.WA&&"permission-denied"===a.code?t+=" Firestore Permission Denied. Check rules for 'users' collection.":a.message&&(t+=" Error: ".concat(a.message)),b({title:"Profile Sync Failed",description:t,variant:"warning",duration:7e3})}}g(t),console.log("App user profile for UID ".concat(e.uid," loaded from Firestore. isAdmin: ").concat(t.isAdmin,", displayName: ").concat(t.displayName))}else{let a=e.email===u;console.warn("No Firestore profile found for user ".concat(e.uid,". Creating one with isAdmin: ").concat(a,"."));let i=e.displayName||"",o=i.split(" "),n=o[0]||"",s=o.slice(1).join(" ")||"",l="";l=n&&s?"".concat(n," ").concat(s).trim():n?n.trim():i?i.trim():e.email?e.email.split("@")[0]:"User";let d={uid:e.uid,email:e.email||void 0,firstName:n,lastName:s,username:(null===(t=e.email)||void 0===t?void 0:t.split("@")[0])||"user".concat(e.uid.substring(0,5)),displayName:l,isAdmin:a,isMember:!1,createdAt:(0,c.Bt)(),updatedAt:(0,c.Bt)(),bio:"",avatarUrl:e.photoURL||null,backgroundUrl:null,loggedAscents:{},baggedMunros:[]};try{await (0,c.pl)(r,d),g(d),console.log("New Firestore profile created for UID ".concat(e.uid,". Name: ").concat(d.displayName,", Admin: ").concat(d.isAdmin,".")),a&&b({title:"Admin Registered",description:"User ".concat(e.email," has been registered as an admin in the app's data."),duration:4e3})}catch(a){console.error("Failed to create Firestore profile for UID ".concat(e.uid,":"),a);let t="Could not create your user profile in the database.";a instanceof c.WA&&"permission-denied"===a.code?t+=" Firestore Permission Denied. Check rules for 'users' collection (create).":a.message&&(t+=" Error: ".concat(a.message)),b({title:"Profile Creation Failed",description:t,variant:"destructive",duration:7e3}),g({...d,bio:"Error creating full profile."})}}}catch(s){console.error("Error fetching/creating user profile from Firestore:",s);let t="Could not load or create your detailed profile.",r="Profile Error",o=(null===(a=s.message)||void 0===a?void 0:a.toLowerCase())||"",n=o.includes("offline")||o.includes("network error")||o.includes("unavailable")||o.includes("could not reach cloud firestore backend")||o.includes("the operation could not be completed")||"unavailable"===s.code;"permission-denied"===s.code?(r="Permission Denied",t="Could not load or create profile: Permission denied. Check Firestore security rules for 'users' collection."):n?(r="Network Issue",t="Failed to load your profile due to a network problem with Firestore. Please check your internet connection."):s.message&&(t="Could not load or create your detailed profile: ".concat(s.message)),b({title:r,de
1scription:t,variant:"destructive"}),g({uid:e.uid,email:e.email||void 0,displayName:e.displayName||(null===(i=e.email)||void 0===i?void 0:i.split("@")[0])||"User",isAdmin:e.email===u,isMember:!1,loggedAscents:{},baggedMunros:[],bio:"Profile data could not be fully loaded."})}},[b]);(0,r.useEffect)(()=>{h(!0);let e=(0,o.Aj)(n.I8,async e=>{if(e){await e.reload();let a=n.I8.currentUser;if(m(a),a&&a.emailVerified)await k(a);else if(a){var t;console.log("User ".concat(a.uid," logged in but email not verified. Setting minimal profile.")),g({uid:a.uid,email:a.email||void 0,displayName:a.displayName||(null===(t=a.email)||void 0===t?void 0:t.split("@")[0])||"User",isAdmin:a.email===u,isMember:!1,loggedAscents:{},baggedMunros:[]})}else g(null)}else console.log("No user logged in (onAuthStateChanged)."),m(null),g(null);h(!1)});return()=>e()},[k]);let E=(0,r.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];h(!0),y(null);try{await (0,o.w7)(n.I8),e?b({title:"Logged Out Due to Inactivity",description:"You have been logged out after 24 hours of inactivity.",duration:7e3,variant:"default"}):b({title:"Logged Out",description:"You have been successfully logged out."}),v.push("/")}catch(t){console.error("Logout error:",t);let e=t.code||t.message||"An unknown error occurred during logout.";y(e),b({title:"Logout Failed",description:e,variant:"destructive"})}finally{h(!1)}},[v,b]);(0,r.useEffect)(()=>{if(!a)return;let e=()=>{A.current&&clearTimeout(A.current),A.current=setTimeout(()=>{console.log("User has been inactive for 24 hours. Logging out."),E(!0)},864e5)},t=["mousemove","mousedown","keypress","touchstart","scroll"];return t.forEach(t=>window.addEventListener(t,e)),e(),()=>{A.current&&clearTimeout(A.current),t.forEach(t=>window.removeEventListener(t,e))}},[a,E]);let T=async(e,t,a,i,r)=>{h(!0),y(null);let s=!1;try{let l=(await (0,o.Xb)(n.I8,e,t)).user;try{await (0,o.w$)(l),s=!0}catch(e){console.error("Error sending initial verification email:",e),b({title:"Verification Email Issue",description:"Account created, but failed to send verification email: ".concat(e.message,". Please also check your spam folder. You can try logging in to trigger a resend."),variant:"warning",duration:1e4})}let d="".concat(a," ").concat(i).trim();await (0,o.ck)(l,{displayName:d});let m=(0,c.JU)(n.db,"users",l.uid);if(!(await (0,c.QT)(m)).exists()){let e=l.email===u,t={uid:l.uid,email:l.email||void 0,firstName:a,lastName:i,username:r,displayName:d,createdAt:(0,c.Bt)(),updatedAt:(0,c.Bt)(),bio:"",avatarUrl:null,backgroundUrl:null,isAdmin:e,isMember:!1,loggedAscents:{},baggedMunros:[]};try{await (0,c.pl)(m,t),e&&b({title:"Admin Registered",description:"User ".concat(l.email," has been registered as an admin in the app's data."),duration:4e3})}catch(t){console.error("Error creating Firestore user profile during sign up (explicit call):",t);let e="Could not save your profile details to the database.";t instanceof c.WA&&"permission-denied"===t.code?e="Profile creation failed: Permission denied. Check Firestore rules for 'users' collection (create).":t.message&&(e="Profile creation failed: ".concat(t.message)),b({title:"Profile Save Warning",description:e,variant:"warning",duration:8e3})}}return s&&b({title:"Registration Successful",description:"Please check your email (and spam folder) to verify your account before logging in.",duration:1e4}),v.push("/login"),l}catch(t){console.error("Sign up error:",t);let e="An unknown error occurred during registration.";if(t.code)switch(t.code){case"auth/email-already-in-use":e="This email address is already in use by another account.";break;case"auth/invalid-email":e="The email address is not valid.";break;case"auth/operation-not-allowed":e="Email/password accounts are not enabled.";break;case"auth/weak-password":e="The password is too weak.";break;default:e=t.message}else t.message&&(e=t.message);return y(e),b({title:"Registration Failed",description:e,variant:"destructive"}),null}finally{h(!1)}},I=async(e,t)=>{h(!0),y(null);try{let i=(await (0,o.e5)(n.I8,e,t)).user;if(await i.reload(),(i=n.I8.currentUser).emailVerified)return(null==a?void 0:a.uid)===i.uid&&(null==a?void 0:a.emailVerified)||m(i),f&&f.uid===i.uid||k(i),b({title:"Login Successful",description:"Welcome back!"}),v.push("/dashboard"),i;{let e="A new verification email has been sent to ".concat(i.email,". Please check your inbox (and spam folder) and verify your account before logging in again.");try{await (0,o.w$)(i)}catch(a){let t=a.message;"auth/too-many-requests"===a.code?(console.warn("Verification email send limit reached (auth/too-many-requests):",a.message),t="Too many verification email requests have been sent. Please wait a few minutes before trying to log in again, or check your spam folder for previous emails.",e="Your email ".concat(i.email," is not verified. ").
1concat(t)):(console.warn("Error resending verification email (but user was informed):",a),e="Your email ".concat(i.email," is not verified. We tried to send a new verification email, but encountered an issue: ").concat(t))}b({title:"Email Not Verified",description:e,variant:"destructive",duration:1e4}),await (0,o.w7)(n.I8);let t="Email not verified. ".concat(e.toLowerCase().includes("too many")||e.toLowerCase().includes("limit reached")?"Too many requests. Check your email/spam or wait.":"Please verify then log in.");return y(t),null}}catch(a){let e="An unknown error occurred during login.",t=!1;if(a.code)switch(a.code){case"auth/invalid-email":e="The email address is not valid.",t=!0;break;case"auth/user-disabled":e="This user account has been disabled.",t=!0;break;case"auth/user-not-found":e="No user found with this email, or incorrect password.",t=!0;break;case"auth/wrong-password":e="Incorrect password or email.",t=!0;break;case"auth/invalid-credential":e="Incorrect email or password.",t=!0;break;default:e=a.message||"An unexpected error occurred."}else a.message&&(e=a.message);return t?console.warn("Login attempt failed (handled): ".concat(a.code||"Unknown code"," - ").concat(e)):console.error("Login error (unhandled or unexpected):",a),y(e),null}finally{h(!1)}},U=(0,r.useCallback)(async()=>{await E(!1)},[E]),F=async e=>{if(!a)throw b({title:"Not Authenticated",description:"You must be logged in to change your email.",variant:"destructive"}),Error("User not authenticated for email update.");h(!0),y(null);try{await (0,o.s)(a,e);let t=(0,c.JU)(n.db,"users",a.uid);await (0,c.r7)(t,{email:e,updatedAt:(0,c.Bt)()}),g(t=>t?{...t,email:e}:null),b({title:"Email Update Initiated",description:"Your request to change email to ".concat(e," is processing. Please check both your OLD and NEW email inboxes for verification steps. You may need to re-login after verification."),duration:12e3})}catch(o){console.error("Hook: Update email error:",o);let t="An unknown error occurred while updating your email.",a="Email Update Failed",i="destructive",r=!0;if("auth/email-already-in-use"===o.code?t="This email address is already in use by another account.":"auth/invalid-email"===o.code?t="The new email address is not valid.":"auth/requires-recent-login"===o.code?t="This is a sensitive operation. For security, please log out and log back in again before changing your email address.":"auth/operation-not-allowed"===o.code?o.message&&o.message.toLowerCase().includes("please verify the new email")?(a="Verification Pending",t="A verification for the new email (".concat(e,") is likely already pending. Please check your inbox for ").concat(e," and click the verification link. If you can't find it, or wish to use a different new email, please wait a few minutes."),i="warning",r=!1):t="This operation is not allowed at this time. This might be due to security settings or an unusual account state. (Error: ".concat(o.message||o.code,")"):o instanceof c.WA&&"permission-denied"===o.code?(a="Permission Denied",t="Failed to update email in profile: Permission denied. Check Firestore rules for 'users' collection (update). Email in Firebase Auth may have changed, but not in app profile.",r=!1):o.message&&(t=o.message),y(t),b({title:a,description:t,variant:i,duration:1e4}),r)throw o}finally{h(!1)}},P=async e=>{h(!0),y(null);try{await (0,o.LS)(n.I8,e),b({title:"Password Reset Email Sent",description:"If an account exists for ".concat(e,", a password reset link has been sent. Please check your inbox (and spam folder)."),duration:1e4})}catch(a){console.error("Password reset error:",a);let t="An unknown error occurred.";if(a.code)switch(a.code){case"auth/invalid-email":t="The email address is not valid.";break;case"auth/user-not-found":console.warn("Password reset attempt for non-existent user: ".concat(e));break;case"auth/missing-email":t="Please enter an email address.";break;case"auth/too-many-requests":t="Too many requests. Please try again later.";break;default:t=a.message}else a.message&&(t=a.message);throw"auth/user-not-found"!==a.code&&(y(t),b({title:"Password Reset Failed",description:t,variant:"destructive"})),a}finally{h(!1)}},S=async(e,t)=>{if(!a)throw b({title:"Not Authenticated",description:"You must be logged in to change your password.",variant:"destructive"}),Error("User not authenticated for password update.");h(!0),y(null);try{if(!a.email)throw b({title:"Error",description:"Current user email is not available, cannot re-authenticate.",variant:"destructive"}),Error("User email not available for re-authentication.");let i=o.w9.credential(a.email,e);await (0,o.aF)(a,i),await (0,o.gQ)(a,t),b({title:"Password Updated",description:"Your password has been changed successfully."})}catch(a){console.error("Hook: Update password error:",a);let e="An unknown error occurred while updating your password.",t="Password Update Failed";if(a.code)switch(a.code){case"auth/wrong-password":e="The current password you entered is incorrect.",t="Incorrect Current Password";break;case"auth/weak-password":e="The new password is too weak. Please choose a stronger password.";break;case"auth/requires-recent-login":e="This is a sensitive operation. For security, please log out and log back in again before changing your password.";break;default:e=a.message||"An error occurred (code: ".concat(a.code,").")}else a.message&&(e=a.message);throw y(e),b({title:t,description:e,variant:"destructive",duration:7e3}),a}finally{h(!1)}},C=(0,r.useCallback)(e=>{g(t=>t?{...t,...e}:a?{uid:a.uid,...e}:null)},[a]),D=async e=>{if(!a||!a.email)throw b({title:"Not Authenticated",description:"You must be logged in to delete your account.",variant:"destructive"}
1),Error("User not authenticated for account deletion.");h(!0),y(null);try{let t=o.w9.credential(a.email,e);await (0,o.aF)(a,t);let i=(0,c.JU)(n.db,"users",a.uid);await (0,c.oe)(i),console.log("Firestore document for UID ".concat(a.uid," deleted.")),await (0,o.h8)(a),console.log("Firebase Auth user for UID ".concat(a.uid," deleted.")),b({title:"Account Deleted",description:"Your account and all associated data have been permanently removed."}),await U()}catch(t){console.error("Hook: Delete account error:",t);let e="An unknown error occurred while deleting your account.";if(t instanceof c.WA)e="Failed to delete your profile data: ".concat(t.message,". Your account was not deleted.");else if(t.code)switch(t.code){case"auth/wrong-password":e="The password you entered is incorrect. Account not deleted.";break;case"auth/requires-recent-login":e="For security, please log out and log back in before deleting your account.";break;default:e="An error occurred: ".concat(t.message||t.code,".")}else t.message&&(e=t.message);throw y(e),b({title:"Account Deletion Failed",description:e,variant:"destructive",duration:8e3}),Error(e)}finally{h(!1)}};return(0,i.jsx)(d.Provider,{value:{currentUser:a,appUserProfile:f,isLoading:p,error:w,signUp:T,logIn:I,logOut:U,updateUserEmail:F,sendPasswordReset:P,updateAuthContextProfile:C,updateUserPassword:S,deleteUserAccount:D},children:t})}function f(){let e=(0,r.useContext)(d);if(void 0===e)throw Error("useAuth must be used within an AuthProvider");return e}},6763:function(e,t,a){a.d(t,{pm:function(){return m}});var i=a(2265);let r=0,o=new Map,n=e=>{if(o.has(e))return;let t=setTimeout(()=>{o.delete(e),d({type:"REMOVE_TOAST",toastId:e})},1e6);o.set(e,t)},s=(e,t)=>{switch(t.type){case"ADD_TOAST":return{...e,toasts:[t.toast,...e.toasts].slice(0,1)};case"UPDATE_TOAST":return{...e,toasts:e.toasts.map(e=>e.id===t.toast.id?{...e,...t.toast}:e)};case"DISMISS_TOAST":{let{toastId:a}=t;return a?n(a):e.toasts.forEach(e=>{n(e.id)}),{...e,toasts:e.toasts.map(e=>e.id===a||void 0===a?{...e,open:!1}:e)}}case"REMOVE_TOAST":if(void 0===t.toastId)return{...e,toasts:[]};return{...e,toasts:e.toasts.filter(e=>e.id!==t.toastId)}}},l=[],c={toasts:[]};function d(e){c=s(c,e),l.forEach(e=>{e(c)})}function u(e){let{...t}=e,a=(r=(r+1)%Number.MAX_SAFE_INTEGER).toString(),i=()=>d({type:"DISMISS_TOAST",toastId:a});return d({type:"ADD_TOAST",toast:{...t,id:a,open:!0,onOpenChange:e=>{e||i()}}}),{id:a,dismiss:i,update:e=>d({type:"UPDATE_TOAST",toast:{...e,id:a}})}}function m(){let[e,t]=i.useState(c);return i.useEffect(()=>(l.push(t),()=>{let e=l.indexOf(t);e>-1&&l.splice(e,1)}),[e]),{...e,toast:u,dismiss:e=>d({type:"DISMISS_TOAST",toastId:e})}}},9506:function(e,t,a){let i;a.d(t,{I8:function(){return l},db:function(){return c},tO:function(){return d}});var r=a(5236),o=a(5735),n=a(9842),s=a(9854);i=(0,r.C6)().length?(0,r.Mq)():(0,r.ZF)({apiKey:"AIzaSyAmkDHAfoIUYiCQ3JiS1o_TcvBE1eeprqI",authDomain:"munro-mapper-rrvpn.firebaseapp.com",projectId:"munro-mapper-rrvpn",storageBucket:"munro-mapper-rrvpn.firebasestorage.app",messagingSenderId:"1012801247568",appId:"1:1012801247568:web:8d4db22805dadf3daeb455"});let l=(0,o.v0)(i),c=(0,n.ad)(i),d=(0,s.cF)(i)},9354:function(e,t,a){a.d(t,{cn:function(){return o}});var i=a(4839),r=a(6164);function o(){for(var e=arguments.length,t=Array(e),a=0;a<e;a++)t[a]=arguments[a];return(0,r.m6)((0,i.W)(t))}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.