1import{v as q,w as T,i as x,u as $,a3 as B,r as m,j as e,c as C,I as D,B as M,k as p}from"./index-DTL302P6.js";import{S as Q}from"./sidebar-O3-mXb5J.js";import{C as H}from"./CustomerSidebar-pLhiKl1A.js";import{C as S,a as w,b as I,d as L}from"./card-Bn2w8S0a.js";import{S as R}from"./switch-D_zjmv9v.js";import{L as O}from"./label-D672ps04.js";import{A as h,b as v}from"./alert-juPBH6EM.js";import{u as V}from"./useCustomerAuth-BPgz8Jrh.js";import{u as G}from"./useMutation-hORDcmsA.js";import{B as z}from"./bell-Dw8ipi73.js";import{S as J}from"./save-B7IQRtTg.js";import"./separator-BXcg3puw.js";import"./index-B07Nqcis.js";import"./skeleton-CFvFMfzl.js";import"./auth-CNI9qFaZ.js";import"./PortalLanguageToggle-nGkdsYbz.js";import"./useUnreadCount-CmcO4JsF.js";import"./badge-C58b7yU7.js";import"./map-pin-Dwxh9WPE.js";import"./ticket-B1MANAx2.js";import"./chart-column-BWYOVxkC.js";import"./download-CD9rcI65.js";import"./message-square-BrwXl32Z.js";import"./file-text-Dd-GFJlC.js";import"./package-N-RlFvO8.js";import"./bike-CLQnIyjX.js";const K=[{id:"tickets",i18nKey:"portal.customer.notifications.groups.tickets",fallback:"Service tickets",events:[{key:"ticket_created",group:"tickets",defaultEnabled:!0},{key:"ticket_status_change",group:"tickets",defaultEnabled:!0},{key:"ticket_comment",group:"tickets",defaultEnabled:!0},{key:"ticket_assigned",group:"tickets",defaultEnabled:!1},{key:"ticket_closed",group:"tickets",defaultEnabled:!0},{key:"ticket_sla_breach",group:"tickets",defaultEnabled:!1}]},{id:"messages",i18nKey:"portal.customer.notifications.groups.messages",fallback:"Meldinger",events:[{key:"message_reply",group:"messages",defaultEnabled:!0},{key:"conversation_started",group:"messages",defaultEnabled:!0}]},{id:"locations",i18nKey:"portal.customer.notifications.groups.locations",fallback:"Lokasjoner & drift",events:[{key:"location_change",group:"locations",defaultEnabled:!1},{key:"location_added",group:"locations",defaultEnabled:!1},{key:"ops_log_daily",group:"locations",defaultEnabled:!1}]},{id:"documents",i18nKey:"portal.customer.notifications.groups.documents",fallback:"Dokumenter",events:[{key:"document_shared",group:"documents",defaultEnabled:!1}]},{id:"users",i18nKey:"portal.customer.notifications.groups.users",fallback:"Brukere & tilgang",events:[{key:"user_invited",group:"users",defaultEnabled:!1},{key:"user_accepted",group:"users",defaultEnabled:!1},{key:"login_new_device",group:"users",defaultEnabled:!1}]},{id:"system",i18nKey:"portal.customer.notifications.groups.system",fallback:"System",events:[{key:"announcement",group:"system",defaultEnabled:!1},{key:"maintenance",group:"system",defaultEnabled:!1}]}],W=K.flatMap(s=>s.events);function j(){return Object.fromEntries(W.map(s=>[s.key,s.defaultEnabled]))}function X(s){const g=q(),u=!!s,l=T({queryKey:["customer-notification-prefs",s==null?void 0:s.toLowerCase()],enabled:u,queryFn:async()=>{if(!s)return null;const{data:a,error:o}=await x.rpc("get_customer_user_id_for_email",{_email:s});if(o)throw o;if(!a)return{customerUserId:null,prefs:j(),emailOverride:""};const{data:c,error:d}=await x.from("customer_notification_preferences").select("id, customer_user_id, prefs, email_override").eq("customer_user_id",a).maybeSingle();if(d)throw d;const n=c;return{customerUserId:a,prefs:{...j(),...(n==null?void 0:n.prefs)??{}},emailOverride:(n==null?void 0:n.email_override)??""}}}),i=G({mutationFn:async a=>{const{error:o}=await x.from("customer_notification_preferences").upsert({customer_user_id:a.customerUserId,prefs:a.prefs,email_override:a.emailOverride},{onConflict:"customer_user_id"});if(o)throw o},onSuccess:()=>{g.invalidateQueries({queryKey:["customer-notification-prefs",s==null?void 0:s.toLowerCase()]})}});return{...l,save:i}}function Ne(){var E;const{t:s}=$(),{customerName:g}=V(),{session:u}=B(),l=((E=u==null?void 0:u.user)==null?void 0:E.email)??null,{data:i,isLoading:a,isError:o,error:c,save:d}=X(l),[n,b]=m.useState(j()),[_,N]=m.useState(""),[A,f]=m.useState(!1);m.useEffect(()=>{i&&(b(i.prefs),N(i.emailOverride??""),f(!1))},[i]);const k=(i==null?void 0:i.customerUserId)??null,F=(r,t)=>{b(y=>({...y,[r]:t})),f(!0)},U=async()=>
1{if(!k){p.error("Fant ingen kundebruker for denne eposten.");return}const r=_.trim();if(r&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(r)){p.error("Ugyldig epostadresse i 'Send til'.");return}try{await d.mutateAsync({customerUserId:k,prefs:n,emailOverride:r||null}),p.success("Notifikasjonsinnstillinger lagret"),f(!1)}catch(t){p.error(`Kunne ikke lagre: ${(t==null?void 0:t.message)??t}`)}},P=m.useMemo(()=>K,[]);return e.jsx(Q,{children:e.jsxs("div",{className:"min-h-screen flex w-full bg-background",children:[e.jsx(H,{customerName:g}),e.jsxs("main",{className:"flex-1 p-6 space-y-6 max-w-3xl",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-medium text-foreground flex items-center gap-2",children:[e.jsx(z,{className:"w-6 h-6"}),s("portal.customer.notifications.title","Notifikasjoner")]}),e.jsx("p",{className:"text-muted-foreground mt-1",children:s("portal.customer.notifications.description","Velg hvilke hendelser du vil motta epost-varsel om. Innstillingene gjelder kun deg.")})]}),l&&e.jsx(h,{children:e.jsxs(v,{children:[s("portal.customer.notifications.emailInfo","Innlogget som"),": ",e.jsx("strong",{children:l})]})}),e.jsx(h,{children:e.jsx(v,{className:"text-sm",children:s("portal.customer.notifications.disclaimer","Sending av notifikasjoner aktiveres trinnvis. Foreløpig lagres preferansene dine, og brukes etter hvert som hver hendelse kobles på.")})}),a&&e.jsx("div",{className:"flex justify-center py-12",children:e.jsx(C,{className:"w-6 h-6 animate-spin text-muted-foreground"})}),o&&e.jsx(h,{variant:"destructive",children:e.jsx(v,{children:String((c==null?void 0:c.message)??c??"Ukjent feil")})}),!a&&!o&&e.jsxs(e.Fragment,{children:[P.map(r=>e.jsxs(S,{children:[e.jsx(w,{children:e.jsx(I,{className:"text-lg font-medium",children:s(r.i18nKey,r.fallback)})}),e.jsx(L,{className:"space-y-4",children:r.events.map(t=>e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx(O,{htmlFor:`evt-${t.key}`,className:"text-sm",children:s(`portal.customer.notifications.events.${t.key}.label`,t.key)}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s(`portal.customer.notifications.events.${t.key}.desc`,"")})]}),e.jsx(R,{id:`evt-${t.key}`,checked:!!n[t.key],onCheckedChange:y=>F(t.key,y)})]},t.key))})]},r.id)),e.jsxs(S,{children:[e.jsx(w,{children:e.jsx(I,{className:"text-lg font-medium",children:s("portal.customer.notifications.deliveryTitle","Leveringsadresse")})}),e.jsxs(L,{className:"space-y-2",children:[e.jsx(O,{htmlFor:"email-override",className:"text-sm",children:s("portal.customer.notifications.emailOverrideLabel","Send notifikasjoner til en annen epost (valgfritt)")}),e.jsx(D,{id:"email-override",type:"email",placeholder:l??"[email protected]",value:_,onChange:r=>{N(r.target.value),f(!0)}}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s("portal.customer.notifications.emailOverrideHelp","La være tom for å bruke epost-adressen du er innlogget med.")})]})]}),e.jsx("div",{className:"flex justify-end",children:e.jsxs(M,{onClick:U,disabled:!A||d.isPending||!k,className:"gap-2",children:[d.isPending?e.jsx(C,{className:"w-4 h-4 animate-spin"}):e.jsx(J,{className:"w-4 h-4"}),s("portal.customer.notifications.save","Lagre innstillinger")]})})]})]})]})})}export{Ne 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.