import{a as L,b as m}from"./vendor-query-CKQAY9LX.js";import{v as w,s as n}from"./index-DbhEjV5W.js";const F=()=>{const{toast:l}=w(),o=L();return m({mutationFn:async({userId:r,data:e})=>{const{error:d}=await n.from("profiles").update(e).eq("id",r);if(d)throw d},onSuccess:(r,{userId:e})=>{o.invalidateQueries({queryKey:["profile",e]}),o.invalidateQueries({queryKey:["profiles"]}),o.invalidateQueries({queryKey:["own-profile"]}),o.invalidateQueries({queryKey:["admin-users"]})},onError:r=>{l({title:"Error",description:r.message||"Failed to update profile",variant:"destructive"})}})},N=()=>{const{toast:l}=w();return m({mutationFn:async({currentPassword:o,newPassword:r})=>{const{data:{user:e}}=await n.auth.getUser();if(!(e!=null&&e.email))throw new Error("User email not found");const{error:d}=await n.auth.signInWithPassword({email:e.email,password:o});if(d)throw new Error("Current password is incorrect");const{error:c}=await n.auth.updateUser({password:r});if(c)throw c},onSuccess:()=>{l({title:"Success",description:"Password updated successfully"})},onError:o=>{var d,c;const r=((d=o.message)==null?void 0:d.toLowerCase().includes("weak"))||((c=o.message)==null?void 0:c.toLowerCase().includes("easy to guess"))||o.code==="weak_password";let e=o.message||"Failed to update password";r&&(e="This password has been found in data breaches and is not secure. Please choose a different password that hasn't been compromised."),l({title:"Error",description:e,variant:"destructive"})}})};let h=!1;const U=l=>{var e;const o=(e=l.split(".").pop())==null?void 0:e.toLowerCase();return{jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",pdf:"application/pdf",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document"}[o||""]||"application/octet-stream"},R=async(l,o)=>{try{const{data:r,error:e}=await n.storage.from(l).download(o);return e||!r?(console.log(`❌ File validation failed for ${o}:`,e==null?void 0:e.message),!1):l==="avatars"&&(console.log(`🔍 Validating uploaded file MIME type: ${r.type}`),!r.type.startsWith("image/"))?(console.log(`❌ Invalid MIME type detected: ${r.type} (expected image/*)`),!1):!0}catch(r){return console.log(`❌ File validation error for ${o}:`,r),!1}},O=()=>{const{toast:l}=w();return m({mutationFn:async({file:o,bucket:r,path:e,options:d})=>{var v;const c=navigator.userAgent||"",I=/Android/i.test(c),C=/Chrome/i.test(c),T=I&&C;if(console.log(`🌍 [ENV] UserAgent: ${c}`),console.log(`🌍 [ENV] Origin: ${window.location.origin}`),console.log("🌍 [ENV] Supabase domain: wqfvydcajnbtsqpjaikl.supabase.co"),console.log(`🌍 [ENV] Android Chrome: ${T}`),"connection"in navigator&&navigator.connection){const i=navigator.connection;console.log(`🌍 [CONN] Type: ${i.effectiveType}, RTT: ${i.rtt}ms, SaveData: ${i.saveData}`)}const p=U(o.name),E=o.type&&o.type!=="application/octet-stream"?o.type:p;console.log(`📤 [UPLOAD START] File: ${o.name}, Bucket: ${r}, Size: ${o.size}, Type: ${o.type}, Final MIME: ${E}`),console.log(`📤 [UPLOAD START] Path: ${e}, Detected MIME: ${p}`);const y=r==="avatars",A=r==="resumes";console.log(`🔍 [VALIDATION] Should validate this upload: ${y} (bucket: ${r})`),console.log("⬆️ [ATTEMPT 1] Starting initial upload...");let{data:f,error:s}=A?await n.storage.from(r).upload(e,o):await n.storage.from(r).upload(e,o,{cacheControl:"3600",upsert:!0,contentType:E});if(console.log(`⬆️ [ATTEMPT 1] Result - Success: ${!s}, Error: ${(s==null?void 0:s.message)||"none"}, Data: ${JSON.stringify(f)}`),s){console.log("❌ [ATTEMPT 1] Upload failed with error:",s),console.log(`❌ [ERROR DETAILS] Name: ${s.name}, Message: ${s.message}`),s.originalError&&console.log("❌ [ERROR DETAILS] Original error:",s.originalError);const i=s.name==="StorageUnknownError"||s.originalError instanceof TypeError;if(T&&A&&i){console.log("🔄 [FALLBACK] Attempting signed upload for Android Chrome...");try{const{data:g,error:a}=await n.storage.from("resumes").createSignedUploadUrl(e);if(a)throw console.log("❌ [FALLBACK] Failed to create signed URL:",a),a;console.log("✅ [FALLBACK] Signed URL created, uploading...");const{data:t,error:u}=await n.storage.from("resumes").uploadToSignedUrl(e,g.token,o);if(u)throw console.log("❌ [FALLBACK] Signed upload failed:",u),u;console.log("✅ [FALLBACK] Signed upload successful"),f={id:(t==null?void 0:t.path)||e,path:(t==null?void 0:t.path)||e,fullPath:(t==null?void 0:t.fullPath)||e},s=null}catch(g){if(console.log("❌ [FALLBACK] Complete failure:",g),!h){h=!0,console.log("🔍 [DIAGNOSTIC] Running one-time probe...");try{const{data:{user:a}}=await n.auth.getUser();if(a){const t=`resumes/diagnostics/${a.id}/${Date.now()}.txt`,u=new Blob(["diagnostic"],{type:"text/plain"}),{error:$}=await n.storage.from("resumes").upload(t,u);$?console.log("❌ [DIAGNOSTIC] Probe upload failed:",$):(console.log("✅ [DIAGNOSTIC] Probe upload succeeded, cleaning up..."),await n.storage.from("resumes").remove([t]))}}catch(a){console.log("❌ [DIAGNOSTIC] Probe error:",a)}}throw g}}else throw s}let S=!1;if(y&&!s){console.log(`🔍 [VALIDATION] Starting file validation for ${r}...`);const i=await R(r,e);console.log(`🔍 [VALIDATION] Validation result: ${i}`),i||(S=!0)}if(S){console.log("🔄 [RETRY] Validation failed, preparing retry..."),console.log("🔄 [RETRY] Refreshing auth session...");const{data:i,error:g}=await n.auth.refreshSession();console.log(`🔄 [RETRY] Session refresh result - Success: ${!g}, Has session: ${!!i.session}`),console.log(`⬆️ [ATTEMPT 2] Starting retry upload with MIME: ${p}...`);const a=await n.storage.from(r).upload(e,o,{cacheControl:"3600",upsert:!0,contentType:p});if(console.log(`⬆️ [ATTEMPT 2] Result - Success: ${!a.error}, Error: ${((v=a.error)==null?void 0:v.message)||"none"}`),a.error)throw console.log("❌ [ATTEMPT 2] Retry failed:",a.error),a.error;console.log("🔍 [VALIDATION] Validating retry upload...");const t=await R(r,e);if(console.log(`🔍 [VALIDATION] Retry validation result: ${t}`),!t)throw console.log("❌ [VALIDATION] Retry validation failed"),new Error("File upload validation failed after retry");f=a.data,console.log("✅ [ATTEMPT 2] Retry upload successful")}else s||console.log("✅ [ATTEMPT 1] Initial upload successful");if(r==="avatars"){console.log("📍 [RETURN] Getting public URL for avatars bucket...");const{data:{publicUrl:i}}=n.storage.from(r).getPublicUrl(e);return console.log(`✅ [SUCCESS] Upload complete. Public URL: ${i}`),{url:i,path:e}}else return console.log(`✅ [SUCCESS] Upload complete. Storage path: ${e}`),{url:e,path:e}},onError:(o,r)=>{var e;console.error("❌ Upload failed:",o),(e=r.options)!=null&&e.suppressErrorToast||l({title:"Error",description:o.message||"Failed to upload file",variant:"destructive"})}})};export{N as a,O as b,F as u};