1import{u as K,b as M,j as e}from"./query-Q_ThwKGI.js";import{r as o}from"./react-vendor-wefok28f.js";import{I as b}from"./input-Cu6Tiemi.js";import{L as c}from"./label-Ch0F7L4t.js";import{B as d}from"./button-DrepnwkX.js";import{B as H}from"./badge-u_30_p7s.js";import{S as E,a as D,b as F,c as z,d as I}from"./select-DECm7gW5.js";import{u as J}from"./index-C4Kq7kkR.js";import{u as W}from"./AuthProvider-BSQZ2YtM.js";import{s as x}from"./client-DjLwxtrO.js";import{u as X}from"./UserPreferencesContext-CmtdahHI.js";import{g as ee,a as se,b as re}from"./currencyUtils-D4WwR8db.js";import{a as te}from"./useSubscription-svlizlcK.js";import{P as ae}from"./PlanFeatureChecklist-BdN1LsxW.js";import{u as $}from"./useTranslation-DNoH3foE.js";import{aN as U,aO as ie,Z as ne,ai as R,D as oe,t as le}from"./icons-By8asZST.js";import{S as ce}from"./SubscriptionBanner-C8Wswn3W.js";import"./index-DC33WZD-.js";import"./index-DviHZN-M.js";import"./index-CryppvJw.js";import"./index-CSO0Tzy2.js";import"./supabase-COHOFxoQ.js";const de=()=>{const{user:t}=W(),{toast:i}=J(),L=K(),{openUpgrade:B}=te(),{preferences:w,setCurrency:O,setLanguage:V,setTimezone:A,isLoading:v}=X(),{t:s}=$(["settings","common"]),[N,y]=o.useState(!1),[p,h]=o.useState(!1),[Q,P]=o.useState(!1),[S,u]=o.useState(!1),[l,C]=o.useState({password:"",confirmPassword:""}),[g,_]=o.useState({full_name:"",phone:""}),{data:n,isLoading:Y,error:j,refetch:k}=M({queryKey:["profile"],queryFn:async()=>{if(console.log("Fetching profile for user ID:",t==null?void 0:t.id),!(t!=null&&t.id))throw new Error("User ID is not available");const{data:r,error:a}=await x.from("profiles").select("*").eq("id",t.id).single();if(a){if(console.error("Error fetching profile by ID:",a),t.email){console.log("Trying to fetch profile by email:",t.email);const{data:f,error:m}=await x.from("profiles").select("*").eq("email",t.email).single();if(m)throw console.error("Error fetching profile by email:",m),m;return console.log("Retrieved profile by email:",f),f}throw a}return console.log("Retrieved profile by ID:",r),r},enabled:!!t});o.useEffect(()=>{n&&(console.log("Setting form data from profile:",n),_({full_name:n.full_name||"",phone:n.phone||""}))},[n]),o.useEffect(()=>{j&&(console.error("Error in profile query:",j),i({title:"Error",description:"Failed to load profile data. Please refresh the page.",variant:"destructive"}))},[j,i]),o.useEffect(()=>{if(!(t!=null&&t.id))return;const r=x.channel("profile_changes").on("postgres_changes",{event:"UPDATE",schema:"public",table:"profiles",filter:`id=eq.${t.id}`},a=>{console.log("Profile updated via subscription:",a.new),L.setQueryData(["profile"],a.new),i({title:"Profile Updated",description:"Your profile has been updated successfully."})}).subscribe();return()=>{x.removeChannel(r)}},[t==null?void 0:t.id,L,i]);const q=r=>{const{name:a,value:f}=r.target;_(m=>({...m,[a]:f}))},T=r=>{const{name:a,value:f}=r.target;C(m=>({...m,[a]:f}))},Z=async r=>{r.preventDefault(),h(!0);try{if(console.log("Submitting profile update:",g),!(t!=null&&t.id))throw new Error("User ID is not available");const{error:a}=await x.from("profiles").update(g).eq("id",t.id);if(a)throw console.error("Error updating profile:",a),a;i({title:"Success",description:"Your profile has been updated successfully."}),y(!1),k()}catch(a){console.error("Error updating profile:",a),i({title:"Error",description:"Failed to update profile. Please try again.",variant:"destructive"})}finally{h(!1)}},G=async r=>{if(r.preventDefault(),l.password!==l.confirmPassword){i({title:"Error",description:"Passwords do not match",variant:"destructive"});return}if(l.password.length<6){i({title:"Error",description:"Password must be at least 6 characters",variant:"destructive"});return}h(!0);try{const{error:a}=await x.auth.updateUser({password:l.password});if(a)throw a;i({title:"Success",description:"Your password has been updated successfully."}),P(!1),C({password:"",confirmPassword:""})}catch(a){console.error("Error updating password:",a),i({title:"Error",description:a.message||"Failed to update password. Please try again.",variant:"destructive"})}finally{h(!1)}};return t?Y?e.jsx("div",{className:"space-y-6",children:e.jsx("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:e.jsx("p",{className:"text-muted-foreground",children:s("common:loading")})})}):j&&!n?e.jsx("div",{className:"space-y-6",children:e.jsxs("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:[e.jsx("p",{className:"text-muted-foreground",children:s("common:error")}),e.jsx(d,{onClick:()=>k(),variant:"outline",size:"sm",className:"mt-2 rounded-lg text-xs font-medium h-8 px-3",children:s("common:tryAgain")})]})}):e.jsxs("div",{className:"space-y-6",children:[e.jsx("div",{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:s("settings:profile.title")}),e.jsx("p",{className:"text-sm text-muted-foreground",children:s("settings:profile.subtitle")})]}),n&&e.jsx(H,{variant:n.subscription_plan==="PRO"?"default":"secondary",className:"text-sm flex items-center space-x-1",children:n.subscription_plan==="PRO"?e.jsxs(e.Fragment,{children:[e.jsx(U,{className:"h-3.5 w-3.5 text-yellow-500 mr-1"}),e.jsx("span",{children:s("settings:subscription.proPlan")})]}):e.jsxs(e.Fragment,{children:[e.jsx(ie,{className:"h-3.5 w-3.5 text-gray-400 mr-1"}),e.jsx("span",{children:s("settings:subscription.freePlan")})]})})]})}),n&&n.subscription_plan!=="PRO"&&e.jsxs("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(U,{className:"h-5 w-5 text-primary"}),e.jsx("h3",{className:"text-lg font-semibold",children:s("settings:subscription.upgradeToPro")})]}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:s("settings:subscription.unlockFeatures")}),e.jsx(ae,{compact:!0}),e.jsx("div",{className:"flex justify-end mt-4",children:e.jsxs(d,{size:"sm",onClick:()=>B(),children:[e.jsx(ne,{className:"h-4 w-4 mr-2"}),s("settings:subscription.upgradeNow")]})})]}),e.jsxs("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx(c,{className:"text-xs font-medium text-muted-foreground",children:s("settings:profile.email")}),e.jsx("p",{className:"text-sm mt-1",children:t==null?void 0:t.email})]}),e.jsxs("form",{onSubmit:Z,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"full_name",className:"text-sm",children:s("settings:profile.fullName")}
1),e.jsx(b,{id:"full_name",name:"full_name",value:g.full_name||"",onChange:q,disabled:!N,className:"text-sm"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"phone",className:"text-sm",children:s("settings:profile.phone")}),e.jsx(b,{id:"phone",name:"phone",value:g.phone||"",onChange:q,disabled:!N,className:"text-sm",placeholder:"+1 (555) 123-4567"})]}),e.jsx("div",{className:"flex justify-end gap-2 pt-4",children:N?e.jsxs(e.Fragment,{children:[e.jsx(d,{type:"button",variant:"outline",size:"sm",onClick:()=>y(!1),disabled:p,children:s("common:cancel")}),e.jsx(d,{type:"submit",size:"sm",disabled:p,children:s(p?"settings:profile.saving":"settings:profile.saveChanges")})]}):e.jsx(d,{type:"button",size:"sm",onClick:()=>y(!0),children:s("settings:profile.editProfile")})})]})]}),e.jsxs("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[e.jsx(R,{className:"h-5 w-5 text-muted-foreground"}),e.jsx("h3",{className:"text-lg font-medium",children:s("settings:preferences.title")})]}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:s("settings:preferences.subtitle")}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(c,{htmlFor:"currency",className:"text-sm flex items-center gap-1",children:[e.jsx(oe,{className:"h-3.5 w-3.5"}),s("settings:preferences.currency")]}),e.jsxs(E,{value:w.currency,onValueChange:async r=>{u(!0);try{await O(r),i({title:s("common:success"),description:`${s("settings:preferences.currency")}: ${r}`})}catch{i({title:s("common:error"),description:s("common:error"),variant:"destructive"})}finally{u(!1)}},disabled:v||S,children:[e.jsx(D,{id:"currency",className:"text-sm",children:e.jsx(F,{placeholder:s("common:select")})}),e.jsx(z,{children:ee().map(r=>e.jsxs(I,{value:r.value,children:[r.symbol," - ",r.label]},r.value))})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s("settings:preferences.currencyDescription")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(c,{htmlFor:"language",className:"text-sm flex items-center gap-1",children:[e.jsx(R,{className:"h-3.5 w-3.5"}),s("settings:preferences.language")]}),e.jsxs(E,{value:w.language,onValueChange:async r=>{u(!0);try{await V(r),i({title:s("common:success"),description:`${s("settings:preferences.language")}: ${r==="pt-BR"?"Português":"English"}`})}catch{i({title:s("common:error"),description:s("common:error"),variant:"destructive"})}finally{u(!1)}},disabled:v||S,children:[e.jsx(D,{id:"language",className:"text-sm",children:e.jsx(F,{placeholder:s("common:select")})}),e.jsx(z,{children:se().map(r=>e.jsx(I,{value:r.value,children:r.label},r.value))})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s("settings:preferences.languageDescription")})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(c,{htmlFor:"timezone",className:"text-sm flex items-center gap-1",children:[e.jsx(le,{className:"h-3.5 w-3.5"}),s("settings:preferences.timezone")]}),e.jsxs(E,{value:w.timezone,onValueChange:async r=>{u(!0);try{await A(r),i({title:s("common:success"),description:`${s("settings:preferences.timezone")}: ${r}`})}catch{i({title:s("common:error"),description:s("common:error"),variant:"destructive"})}finally{u(!1)}},disabled:v||S,children:[e.jsx(D,{id:"timezone",className:"text-sm",children:e.jsx(F,{placeholder:s("common:select")})}),e.jsx(z,{children:re().map(r=>e.jsxs(I,{value:r.value,children:[r.label," (UTC",r.offset,")"]},r.value))})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s("settings:preferences.timezoneDescription")})]})]})]}),e.jsxs("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:[e.jsx("h3",{className:"text-lg font-medium mb-4",children:s("settings:password.title")}),Q?e.jsxs("form",{onSubmit:G,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"password",className:"text-sm",children:s("settings:password.newPassword")}),e.jsx(b,{id:"password",name:"password",type:"password",value:l.password,onChange:T,className:"text-sm",required:!0,minLength:6})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"confirmPassword",className:"text-sm",children:s("settings:password.confirmPassword")}),e.jsx(b,{id:"confirmPassword",name:"confirmPassword",type:"password",value:l.confirmPassword,onChange:T,className:"text-sm",required:!0,minLength:6})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(d,{type:"button",variant:"outline",size:"sm",onClick:()=>{P(!1),C({password:"",confirmPassword:""})},disabled:p,children:s("common:cancel")}),e.jsx(d,{type:"submit",size:"sm",disabled:p||!l.password||!l.confirmPassword,children:s(p?"settings:password.updating":"settings:password.changePassword")})]})]}):e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium",children:s("settings:password.title")}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:s("settings:password.subtitle")})]}),e.jsx(d,{type:"button",size:"sm",variant:"outline",onClick:()=>P(!0),children:s("settings:password.changePassword")})]})]})]}):e.jsx("div",{className:"space-y-6",children:e.jsx("div",{className:"rounded-2xl border border-border/50 bg-card shadow-sm p-6",children:e.jsx("p",{className:"text-muted-foreground",children:s("common:loading")})})})},_e=()=>{const{t}=$("settings");return e.jsxs("div",{className:"space-y-8 animate-fade-in",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold tracking-tight",children:t("title")}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:t("subtitle")})]}),e.jsx(ce,{}),e.jsx("div",{className:"max-w-2xl",children:e.jsx(de,{})})]})};export{_e as default};
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.