"use strict";const h=require("react"),D=()=>window.__notificationSDK?.client,_=({preferences:t,onPreferencesChange:f,onError:e})=>{const n=D(),[c,i]=h.useState(!1),[s,l]=h.useState(null),p=h.useRef(),S=h.useRef({}),j=h.useCallback((v,y)=>{try{l(null);const r={...t},o=v.split(".");let b=r;for(let a=0;a<o.length-1;a++)b[o[a]]===void 0&&(b[o[a]]={}),b=b[o[a]];b[o[o.length-1]]=y,f(r),S.current[v]=y,p.current&&clearTimeout(p.current),i(!0),p.current=setTimeout(async()=>{try{if(!n)throw new Error("Notification client not available");const a=window.__notificationSDK?.config;if(!a)throw new Error("SDK configuration not available");const{subscriberId:u,tenantId:g,environmentId:A}=a;if(!u||!g||!A)throw new Error("SubscriberId, TenantId or EnvironmentId not available in SDK configuration.");if(v.startsWith("deliverySchedule.")){const d={isEnabled:r.deliverySchedule.enabled||!1};r.deliverySchedule.weeklySchedule&&(d.weeklySchedule=r.deliverySchedule.weeklySchedule),await n.preferences.updateSchedule(g,u,d,A)}else{const d={emailEnabled:r.channels.email,pushEnabled:r.channels.push,inAppEnabled:r.channels.inApp,smsEnabled:r.channels.sms};r.subscriptions.length>0&&(d.categories={},r.subscriptions.forEach(m=>{d.categories[m.workflowId]={emailEnabled:m.channels.email,pushEnabled:m.channels.push,inAppEnabled:m.channels.inApp,smsEnabled:m.channels.sms}})),await n.preferences.update(g,u,d)}S.current={},l(null)}catch(a){const u=a instanceof Error?a:new Error("Failed to save preferences");process.env.NODE_ENV,l(u),e?.(u)}finally{i(!1)}},500)}catch(r){const o=r instanceof Error?r:new Error("Failed to update preference");l(o),e?.(o),i(!1)}},[t,f,n,e]);return h.useEffect(()=>()=>{p.current&&clearTimeout(p.current)},[]),{updatePreference:j,isSaving:c,error:s}},E=t=>!(!t||typeof t!="object"||["notificationId","channel","renderedContent","read","archived"].filter(n=>!(n in t)).length>0||typeof t.channel!="string"||typeof t.read!="boolean"||typeof t.archived!="boolean"||typeof t.renderedContent!="object"||t.renderedContent===null),N=(t,f,e)=>({type:"custom",label:t.label,icon:void 0,handler:async()=>{try{if(t.url){const n=t.target||"_self";window.open(t.url,n)}t.markAsReadOnClick}catch(n){throw n}}}),w=()=>{const t=(e,n)=>{if(!e||typeof e!="object")return{subject:"",body:""};let c="",i="";return e.push?(c=e.push.title||"",i=e.push.body||""):e.email?(c=e.email.subject||"",i=e.email.html||e.email.text||""):e.in_app?(c=e.in_app.title||"",i=e.in_app.message||""):e.sms?(c="SMS",i=e.sms.message||""):n==="in_app"?(c=e.title||"",i=e.message||""):(c=e.title||e.subject||"",i=e.message||e.body||e.text||""),{subject:c,body:i}},f=(e,n,c)=>{if(!e)return[];const i=[];if(Array.isArray(e))return e.forEach(s=>{i.push(N(s,n||"",c))}),i;if(e.primary){const s=e.primary;i.push({type:"custom",label:s.label||"Primary Action",icon:void 0,handler:async l=>{s.url&&window.open(s.url,"_self")}})}if(e.secondary){const s=e.secondary;i.push({type:"custom",label:s.label||"Secondary Action",icon:void 0,handler:async l=>{s.url&&window.open(s.url,"_self")}})}return i};return{toWidgetNotification(e){const{subject:n,body:c}=t(e.renderedContent,e.channel);return{id:e.notificationId,subject:n||"Notification",body:c||"",isRead:e.read,isArchived:e.archived,timestamp:e.createdAt?new Date(e.createdAt):new Date,tags:[],actions:f(e.renderedContent?.actions||e.actions),metadata:{channel:e.channel,renderedContent:e.renderedContent}}},toWidgetNotificationFromWebSocket(e){if(!E(e)){const i=e?.renderedContent&&typeof e.renderedContent=="object"?e.renderedContent:{},{subject:s,body:l}=t(i,e?.channel);return{id:e?.notificationId||`notification-${Date.now()}`,subject:s||"Notification",body:l||"",isRead:e?.read||!1,isArchived:e?.archived||!1,timestamp:e?.createdAt?new Date(e.createdAt):new Date,tags:[],actions:[],metadata:{channel:e?.channel||"in_app",renderedContent:i}}}const{subject:n,body:c}=t(e.renderedContent,e.channel);return{id:e.notificationId,subject:n||"Notification",body:c||"",isRead:e.read||!1,isArchived:e.archived||!1,timestamp:e.createdAt?new Date(e.createdAt):new Date,tags:[],actions:f(e.renderedContent?.actions||e.actions,e.notificationId),metadata:{channel:e.channel,renderedContent:e.renderedContent}}},validateWebSocketPayload:E}};exports.createNotificationMapper=w;exports.useLivePreferences=_;
//# sourceMappingURL=hooks-C6Z38jT_.cjs.map
