import{j as e}from"./query-PJ0ZdjbV.js";import{a as f}from"./vendor-CWhr2lg4.js";import{a2 as z,a5 as Z,U as je,a as ze,aC as Ue,aD as Ns,aE as ws,aF as ks,aG as Ss,aH as Cs,aI as _s,aJ as Ts,aK as As,B as P,L as ee,al as Ke,ar as Ps,I as B,ap as _,at as ie,aL as Es,z as Ds,aM as Be,am as He,an as Ve,ao as Ge,aN as Ms,aO as Is,aa as Xe,ab as $e,ac as Ye,ad as Je,ae as Qe,q as M,s as V,t as G,v as X,w as $,x as W,af as Ze,aP as Ls,aQ as ye,aR as qs,P as le,ag as es,ah as ss,ai as as,aj as ne,ak as Fs,y as ve,aS as Rs,Q as Ne,V as we,W as ke,X as ts,Y as Se,Z as Ce,_ as _e,$ as Te,a0 as Ae,aT as Os,aU as Ws,r as ls,aV as zs,aW as Us,aX as Ks,aY as Bs,aZ as Hs,a_ as Vs,av as Gs,ax as Xs,ay as $s,az as Ys,a$ as Js,aA as Qs,o as Zs,a4 as O,b0 as ea,b1 as sa,b2 as aa,b3 as ta,b4 as la,b5 as na,b as De,b6 as ia,au as Me,b7 as ra,b8 as ca,b9 as oa,ba as da,bb as xa,bc as Y,a7 as pa,bd as J}from"./index-BSPIaiZU.js";import{P as ha}from"./page-header-D8tSLbK0.js";import{B as ns}from"./banknote-r5Yp3Ac5.js";import{M as is}from"./mail-Bu7DITgC.js";import{S as rs}from"./smartphone-CjO1nI6k.js";import{M as cs}from"./message-square-CrQ4L-TW.js";import{M as os}from"./message-circle-DRPiqW4z.js";import{T as ds}from"./trending-up-Cognoqa9.js";import{B as D}from"./badge-C34iUAiA.js";import{S as F}from"./skeleton-CfUgaIb_.js";import{E as H}from"./empty-state-lAjp7uO1.js";import{B as ma}from"./bell-off-WDg6Md43.js";import{C as oe}from"./circle-alert-9pjtMKqA.js";import{C as ua,u as ga,t as ba,a as ae,o as fa,b as ja,s as Q,e as ya}from"./date-range-picker-BB0TgAn8.js";import{C as de}from"./circle-check-Dgh1V3VA.js";import{S as va}from"./status-badge-DPaiL_rL.js";import{R as Na}from"./rich-text-editor-DrppaLvL.js";import{Z as se}from"./zap-DzbXsdut.js";import{F as fe}from"./file-text-BeBoTfpI.js";import{E as xs}from"./ellipsis-DaNBIBaV.js";import{P as ps}from"./pencil-Dl5fXQYl.js";import{E as hs}from"./eye-DlIcvqP-.js";import{E as wa}from"./eye-off-D-jqPqD8.js";import{G as ka}from"./globe-BTv0oSL0.js";import{S as ms}from"./settings-2-BfbRXbug.js";import{u as Sa}from"./accounts.api-DU956vCW.js";import{A as Ca}from"./async-select-BUbwdJEZ.js";import{L as _a}from"./line-chart-DHSrVad-.js";import{C as me,B as Ta}from"./bar-chart-Ddv4-XT3.js";import{P as Aa}from"./pie-chart-DX6eKRHL.js";import{f as us}from"./format-Bw_hER4B.js";import{p as gs}from"./parseISO-CsaAlwIQ.js";import{S as Pa}from"./send-hExJQKXK.js";import{C as Ea}from"./circle-x-C0sF-Oht.js";import{D as Da}from"./data-table-D_-xOEqd.js";import"./ui-aO-W1bxx.js";import"./with-selector-CmGJjsAK.js";import"./index-Dh4i1-J3.js";import"./list-ordered-Dn0jhaqK.js";import"./link-2-zJB3uZzB.js";import"./undo-2-CyjnlLm8.js";import"./value-BGBiTkF3.js";import"./chevron-left-DLFf1YD2.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const bs=z("ChartNoAxesColumn",[["line",{x1:"18",x2:"18",y1:"20",y2:"10",key:"1xfpm4"}],["line",{x1:"12",x2:"12",y1:"20",y2:"4",key:"be30l9"}],["line",{x1:"6",x2:"6",y1:"20",y2:"14",key:"1r4le6"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ma=z("ChevronsDownUp",[["path",{d:"m7 20 5-5 5 5",key:"13a0gw"}],["path",{d:"m7 4 5 5 5-5",key:"1kwcof"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ia=z("FileCheck2",[["path",{d:"M4 22h14a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v4",key:"1pf5j1"}],["path",{d:"M14 2v4a2 2 0 0 0 2 2h4",key:"tnqrlb"}],["path",{d:"m3 15 2 2 4-4",key:"1lhrkk"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const La=z("Hash",[["line",{x1:"4",x2:"20",y1:"9",y2:"9",key:"4lhtct"}],["line",{x1:"4",x2:"20",y1:"15",y2:"15",key:"vyu0kd"}],["line",{x1:"10",x2:"8",y1:"3",y2:"21",key:"1ggp8o"}],["line",{x1:"16",x2:"14",y1:"3",y2:"21",key:"weycgp"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const qa=z("Key",[["path",{d:"m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4",key:"g0fldk"}],["path",{d:"m21 2-9.6 9.6",key:"1j0ho8"}],["circle",{cx:"7.5",cy:"15.5",r:"5.5",key:"yqb3hr"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const fs=z("MonitorSpeaker",[["path",{d:"M5.5 20H8",key:"1k40s5"}],["path",{d:"M17 9h.01",key:"1j24nn"}],["rect",{width:"10",height:"16",x:"12",y:"4",rx:"2",key:"ixliua"}],["path",{d:"M8 6H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h4",key:"1mp6e1"}],["circle",{cx:"17",cy:"15",r:"1",key:"tqvash"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Fa=z("Radio",[["path",{d:"M4.9 19.1C1 15.2 1 8.8 4.9 4.9",key:"1vaf9d"}],["path",{d:"M7.8 16.2c-2.3-2.3-2.3-6.1 0-8.5",key:"u1ii0m"}],["circle",{cx:"12",cy:"12",r:"2",key:"1c9p78"}],["path",{d:"M16.2 7.8c2.3 2.3 2.3 6.1 0 8.5",key:"1j5fej"}],["path",{d:"M19.1 4.9C23 8.8 23 15.1 19.1 19",key:"10b0cb"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ra=z("Server",[["rect",{width:"20",height:"8",x:"2",y:"2",rx:"2",ry:"2",key:"ngkwjq"}],["rect",{width:"20",height:"8",x:"2",y:"14",rx:"2",ry:"2",key:"iecqi9"}],["line",{x1:"6",x2:"6.01",y1:"6",y2:"6",key:"16zg32"}],["line",{x1:"6",x2:"6.01",y1:"18",y2:"18",key:"nzw8ys"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ie=z("TestTubeDiagonal",[["path",{d:"M21 7 6.82 21.18a2.83 2.83 0 0 1-3.99-.01a2.83 2.83 0 0 1 0-4L17 3",key:"1ub6xw"}],["path",{d:"m16 2 6 6",key:"1gw87d"}],["path",{d:"M12 16H4",key:"1cjfip"}]]),Le={leave:{label:"Leave Management",icon:Ue},attendance:{label:"Attendance",icon:ze},payroll:{label:"Payroll",icon:ns},core_hr:{label:"Core HR",icon:je},accounts:{label:"Account & Security",icon:Z},system:{label:"System",icon:Z}};function Oa(s){const a=new Map;for(const t of s){const c=a.get(t.module)??[];c.push({code:t.key,label:t.label,description:t.description,defaultChannels:t.default_channels,transactional:t.transactional}),a.set(t.module,c)}return Array.from(a.entries()).map(([t,c])=>{var r,d;return{id:t,label:((r=Le[t])==null?void 0:r.label)??t,icon:((d=Le[t])==null?void 0:d.icon)??Z,events:c}})}function qe(s){const a={};for(const t of s)a[t.key]={in_app:t.default_channels.includes("in_app"),email:t.default_channels.includes("email"),push:t.default_channels.includes("push"),sms:t.default_channels.includes("sms"),whatsapp:t.default_channels.includes("whatsapp")};return a}const Pe=[{id:"leave",label:"Leave Management",icon:Ue,events:[{code:"leave.applied",label:"Leave Application Submitted",description:"When a leave request is submitted by you or your team",defaultChannels:["in_app","email"]},{code:"leave.approved",label:"Leave Approved",description:"When your leave request is approved by your manager",defaultChannels:["in_app","email","push"]},{code:"leave.rejected",label:"Leave Declined",description:"When your leave request is declined",defaultChannels:["in_app","email"]},{code:"leave.cancelled",label:"Leave Cancelled",description:"When a leave application is withdrawn",defaultChannels:["in_app"]},{code:"leave.balance_reminder",label:"Leave Balance Reminder",description:"Annual reminder to use or plan remaining leave balance",defaultChannels:["in_app","email"]}]},{id:"attendance",label:"Attendance",icon:ze,events:[{code:"attendance.absent",label:"Absent Alert",description:"When you are marked absent for a working day",defaultChannels:["in_app","email","push"]},{code:"attendance.late",label:"Late Arrival Alert",description:"When a late check-in is recorded against your profile",defaultChannels:["in_app"]},{code:"attendance.regularize_approved",label:"Regularization Approved",description:"When your attendance regularization request is approved",defaultChannels:["in_app","email"]},{code:"attendance.overtime",label:"Overtime Logged",description:"When overtime hours are logged and pending approval",defaultChannels:["in_app"]}]},{id:"payroll",label:"Payroll",icon:ns,events:[{code:"payroll.slip_generated",label:"Payslip Ready",description:"When your monthly payslip has been generated and is ready to view",defaultChannels:["in_app","email","push"]},{code:"payroll.processed",label:"Payroll Run Completed",description:"When the monthly payroll has been processed by HR",defaultChannels:["in_app"]},{code:"payroll.revision",label:"Salary Revised",description:"When your CTC or salary components are updated",defaultChannels:["in_app","email"]}]},{id:"performance",label:"Performance",icon:ds,events:[{code:"performance.review_due",label:"Review Due",description:"When a performance review cycle requires your action",defaultChannels:["in_app","email","push"]},{code:"performance.feedback_received",label:"Feedback Received",description:"When a peer or manager submits feedback for you",defaultChannels:["in_app","push"]},{code:"performance.goal_assigned",label:"Goal Assigned",description:"When a new KRA or goal is assigned to you",defaultChannels:["in_app","email"]}]},{id:"recruitment",label:"Recruitment",icon:je,events:[{code:"recruitment.application_update",label:"Application Status Update",description:"When a candidate application status changes in your pipeline",defaultChannels:["in_app","email"]},{code:"recruitment.interview_scheduled",label:"Interview Scheduled",description:"When an interview panel invitation is created for you",defaultChannels:["in_app","email","push"]}]},{id:"system",label:"System & Announcements",icon:Z,events:[{code:"system.announcement",label:"Company Announcements",description:"Important company-wide notices from HR or leadership",defaultChannels:["in_app","email"]},{code:"system.task_assigned",label:"Task Assigned",description:"When an action item or workflow task is assigned to you",defaultChannels:["in_app","push"]},{code:"system.document_shared",label:"Document Shared",description:"When a document or file is shared with you",defaultChannels:["in_app","email"]}]}],R=[{id:"in_app",label:"In-App",icon:fs,description:"Bell notifications inside the platform",color:"text-blue-600",bgColor:"bg-blue-50",borderColor:"border-blue-200"},{id:"email",label:"Email",icon:is,description:"Emails to your registered address",color:"text-violet-600",bgColor:"bg-violet-50",borderColor:"border-violet-200"},{id:"push",label:"Push",icon:rs,description:"Mobile & browser push alerts",color:"text-orange-500",bgColor:"bg-orange-50",borderColor:"border-orange-200"},{id:"sms",label:"SMS",icon:cs,description:"Text messages for critical alerts",color:"text-green-600",bgColor:"bg-green-50",borderColor:"border-green-200"},{id:"whatsapp",label:"WhatsApp",icon:os,description:"WhatsApp Business messaging",color:"text-emerald-600",bgColor:"bg-emerald-50",borderColor:"border-emerald-200"}];function te(){return{in_app:!1,email:!1,push:!1,sms:!1,whatsapp:!1}}function Fe(s,a){return s[a]??te()}const K=[{type:"in_app",label:"In-App",icon:fs,color:"text-blue-600",bgColor:"bg-blue-50",borderColor:"border-blue-200",description:"Real-time in-app bell notifications via WebSocket / SSE",providers:[{id:"builtin",name:"Built-in (Database)",description:"Default — stores in Notification table, always available. No configuration required.",fields:[]},{id:"pusher",name:"Pusher",description:"Real-time delivery via Pusher Channels",fields:[{key:"app_id",label:"App ID",type:"text",placeholder:"123456",required:!0},{key:"key",label:"Key",type:"text",placeholder:"your-pusher-key",required:!0},{key:"secret",label:"Secret",type:"password",placeholder:"••••••••",required:!0},{key:"cluster",label:"Cluster",type:"select",options:[{value:"ap2",label:"ap2 (Mumbai)"},{value:"ap1",label:"ap1 (Singapore)"},{value:"us2",label:"us2 (US East)"},{value:"eu",label:"eu (Ireland)"}]}]}]},{type:"email",label:"Email",icon:is,color:"text-violet-600",bgColor:"bg-violet-50",borderColor:"border-violet-200",description:"Transactional & digest email delivery via third-party providers",providers:[{id:"smtp",name:"SMTP (Generic / SES / Gmail)",description:"Standard SMTP relay — any mail server, incl. Amazon SES SMTP and Gmail",fields:[{key:"host",label:"SMTP Host",type:"text",placeholder:"email-smtp.ap-south-1.amazonaws.com",required:!0,hint:"SES: email-smtp.<region>.amazonaws.com · Gmail: smtp.gmail.com"},{key:"port",label:"Port",type:"number",placeholder:"587",required:!0,hint:"587 (TLS) or 465 (SSL)"},{key:"username",label:"Username",type:"text",placeholder:"SES SMTP username / mailbox",required:!0},{key:"password",label:"Password",type:"password",placeholder:"SES SMTP password / app password",required:!0,hint:"For SES use the SMTP credentials (not the raw API key)"},{key:"encryption",label:"Encryption",type:"select",options:[{value:"tls",label:"TLS / STARTTLS (recommended)"},{value:"ssl",label:"SSL"},{value:"none",label:"None"}]},{key:"from_name",label:"From Name",type:"text",placeholder:"PeoplePin"},{key:"from_email",label:"From Email",type:"text",placeholder:"noreply@company.com",required:!0,hint:"Must be a verified sender (SES) or your mailbox"}]},{id:"sendgrid",name:"SendGrid",description:"Twilio SendGrid — scalable email delivery with analytics",fields:[{key:"api_key",label:"API Key",type:"password",placeholder:"SG.xxxxxxxx",required:!0},{key:"from_email",label:"From Email",type:"text",placeholder:"noreply@company.com",required:!0},{key:"from_name",label:"From Name",type:"text",placeholder:"PeoplePin"},{key:"sandbox_mode",label:"Sandbox Mode",type:"select",options:[{value:"false",label:"Disabled (Production)"},{value:"true",label:"Enabled (Testing)"}]}]},{id:"ses",name:"Amazon SES",description:"AWS Simple Email Service — high-volume, low-cost delivery",fields:[{key:"aws_access_key_id",label:"AWS Access Key ID",type:"text",placeholder:"AKIA…",required:!0},{key:"aws_secret_access_key",label:"AWS Secret Access Key",type:"password",placeholder:"••••••••",required:!0},{key:"region",label:"AWS Region",type:"select",required:!0,options:[{value:"ap-south-1",label:"ap-south-1 (Mumbai)"},{value:"us-east-1",label:"us-east-1 (N. Virginia)"},{value:"eu-west-1",label:"eu-west-1 (Ireland)"},{value:"ap-southeast-1",label:"ap-southeast-1 (Singapore)"}]},{key:"from_email",label:"Verified From Email",type:"text",placeholder:"noreply@company.com",required:!0},{key:"configuration_set",label:"Configuration Set",type:"text",placeholder:"Optional",hint:"For tracking opens/clicks"}]},{id:"mailgun",name:"Mailgun",description:"Sinch Mailgun — developer-friendly email API",fields:[{key:"api_key",label:"API Key",type:"password",placeholder:"key-xxxxxxxx",required:!0},{key:"domain",label:"Domain",type:"text",placeholder:"mg.company.com",required:!0},{key:"from_email",label:"From Email",type:"text",placeholder:"noreply@company.com",required:!0},{key:"region",label:"Region",type:"select",options:[{value:"us",label:"US"},{value:"eu",label:"EU"}]}]}]},{type:"push",label:"Push Notifications",icon:rs,color:"text-orange-500",bgColor:"bg-orange-50",borderColor:"border-orange-200",description:"Browser & mobile push notifications via Firebase or OneSignal",providers:[{id:"fcm",name:"Firebase Cloud Messaging",description:"Google FCM — Android, iOS, and Web push notifications",fields:[{key:"project_id",label:"Firebase Project ID",type:"text",placeholder:"my-project-123",required:!0,hint:"Auto-filled from the uploaded JSON if left blank"},{key:"service_account_json",label:"Service Account JSON Key",type:"file",accept:".json,application/json",required:!0,hint:"Server key: Firebase Console → Project Settings → Service Accounts → Generate new private key"},{key:"android_google_services_json",label:"Android — google-services.json",type:"file",accept:".json,application/json",hint:"Client config for the Android app (served via the push-config API)"},{key:"ios_google_service_plist",label:"iOS — GoogleService-Info.plist",type:"file",accept:".plist,text/xml,application/xml",hint:"Client config for the iOS app (served via the push-config API)"},{key:"web_firebase_config",label:"Web — Firebase Config (JSON)",type:"textarea",placeholder:'{ "apiKey": "...", "authDomain": "...", "projectId": "...", "messagingSenderId": "...", "appId": "..." }',hint:"The firebaseConfig object for web push"},{key:"web_vapid_key",label:"Web — VAPID Key",type:"text",placeholder:"B? public VAPID key",hint:"Web Push certificate key pair (Cloud Messaging → Web configuration)"},{key:"default_icon",label:"Default Notification Icon URL",type:"text",placeholder:"https://cdn.company.com/icon-192.png"},{key:"default_click_action",label:"Default Click Action URL",type:"text",placeholder:"https://app.company.com"}]},{id:"onesignal",name:"OneSignal",description:"OneSignal — multi-platform push with audience segmentation",fields:[{key:"app_id",label:"OneSignal App ID",type:"text",placeholder:"xxxxxxxx-xxxx-xxxx-xxxx",required:!0},{key:"rest_api_key",label:"REST API Key",type:"password",placeholder:"••••••••",required:!0}]}]},{type:"sms",label:"SMS",icon:cs,color:"text-green-600",bgColor:"bg-green-50",borderColor:"border-green-200",description:"Transactional SMS delivery via any HTTP provider, Twilio, MSG91, or AWS SNS",providers:[{id:"generic_http_sms",name:"Generic HTTP (any provider)",description:"Works with any SMS provider that has an HTTP send API — configure the URL & fields",fields:[{key:"url",label:"Send Endpoint URL",type:"text",required:!0,placeholder:"https://api.provider.com/send?apikey={apikey}&to={mobile}&text={text}",hint:"Use placeholders: {mobile} {text} {sender} {apikey}. They are substituted at send time."},{key:"method",label:"HTTP Method",type:"select",options:[{value:"GET",label:"GET"},{value:"POST",label:"POST"}]},{key:"body_format",label:"POST Body Format",type:"select",options:[{value:"form",label:"Form-encoded (application/x-www-form-urlencoded)"},{value:"json",label:"JSON (application/json)"},{value:"query",label:"Query string (params on URL)"}],hint:"Only used when method is POST"},{key:"params",label:"Params / Body (JSON)",type:"textarea",placeholder:'{ "authkey": "{apikey}", "mobiles": "{mobile}", "message": "{text}", "sender": "{sender}" }',hint:"Key→value pairs sent with the request. Values may use {placeholders}. Leave empty if everything is in the URL."},{key:"apikey",label:"API Key",type:"password",placeholder:"••••••••",hint:"Referenced by {apikey} in the URL/params"},{key:"sender",label:"Sender ID",type:"text",placeholder:"OQUIS",hint:"Referenced by {sender}"},{key:"success_contains",label:"Success Indicator (optional)",type:"text",placeholder:'e.g. success  or  "status":"ok"',hint:"If set, the response body must contain this text for the send to count as delivered"}]},{id:"twilio_sms",name:"Twilio SMS",description:"Twilio — global programmable SMS with delivery tracking",fields:[{key:"account_sid",label:"Account SID",type:"text",placeholder:"ACxxxxxxxx",required:!0},{key:"auth_token",label:"Auth Token",type:"password",placeholder:"••••••••",required:!0},{key:"from_number",label:"From Number / Sender ID",type:"text",placeholder:"+1415XXXYYYY or OQUIS",required:!0},{key:"messaging_service_sid",label:"Messaging Service SID",type:"text",placeholder:"Optional — for copilot features"}]},{id:"msg91",name:"MSG91",description:"MSG91 — India-focused transactional & promotional SMS",fields:[{key:"auth_key",label:"Auth Key",type:"password",placeholder:"••••••••",required:!0},{key:"sender_id",label:"Sender ID (6 chars)",type:"text",placeholder:"OQUISH",required:!0},{key:"route",label:"Route",type:"select",required:!0,options:[{value:"4",label:"Transactional"},{value:"1",label:"Promotional"}]},{key:"template_id",label:"DLT Template ID",type:"text",placeholder:"For DLT-registered templates"}]},{id:"sns",name:"Amazon SNS",description:"AWS Simple Notification Service — SMS via AWS global network",fields:[{key:"aws_access_key_id",label:"AWS Access Key ID",type:"text",placeholder:"AKIA…",required:!0},{key:"aws_secret_access_key",label:"AWS Secret Access Key",type:"password",placeholder:"••••••••",required:!0},{key:"region",label:"AWS Region",type:"select",required:!0,options:[{value:"ap-south-1",label:"ap-south-1 (Mumbai)"},{value:"us-east-1",label:"us-east-1 (N. Virginia)"},{value:"ap-southeast-1",label:"ap-southeast-1 (Singapore)"}]},{key:"sender_id",label:"Sender ID",type:"text",placeholder:"OQUIS"},{key:"sms_type",label:"SMS Type",type:"select",options:[{value:"Transactional",label:"Transactional"},{value:"Promotional",label:"Promotional"}]}]}]},{type:"whatsapp",label:"WhatsApp",icon:os,color:"text-emerald-600",bgColor:"bg-emerald-50",borderColor:"border-emerald-200",description:"WhatsApp Business API messaging via Meta, Twilio, or Doubletick",providers:[{id:"meta_whatsapp",name:"Meta Cloud API",description:"Official Meta WhatsApp Cloud API — direct integration",fields:[{key:"phone_number_id",label:"Phone Number ID",type:"text",placeholder:"10XXXXXXXXX",required:!0},{key:"access_token",label:"Permanent Access Token",type:"password",placeholder:"EAAG…",required:!0},{key:"waba_id",label:"WhatsApp Business Account ID",type:"text",placeholder:"10XXXXXXXXX",required:!0},{key:"api_version",label:"Graph API Version",type:"text",placeholder:"v21.0",hint:"Meta Graph API version for the messages endpoint"},{key:"verify_token",label:"Webhook Verify Token",type:"text",placeholder:"my-verify-token",hint:"Only needed if you receive inbound webhooks"}]},{id:"twilio_whatsapp",name:"Twilio WhatsApp",description:"Twilio — WhatsApp messaging via Twilio Messaging Service",fields:[{key:"account_sid",label:"Account SID",type:"text",placeholder:"ACxxxxxxxx",required:!0},{key:"auth_token",label:"Auth Token",type:"password",placeholder:"••••••••",required:!0},{key:"from_number",label:"WhatsApp Number",type:"text",placeholder:"whatsapp:+14155238886",required:!0},{key:"messaging_service_sid",label:"Messaging Service SID",type:"text",placeholder:"Optional"}]},{id:"doubletick",name:"DoubleTick",description:"DoubleTick — WhatsApp Business API for Indian enterprises",fields:[{key:"api_key",label:"API Key",type:"password",placeholder:"••••••••",required:!0},{key:"device_id",label:"Device ID",type:"text",placeholder:"dt-xxxxxx",required:!0},{key:"from_number",label:"Registered WhatsApp Number",type:"text",placeholder:"+91 98765 43210",required:!0}]}]}],Re=["accounts","audit","notifications","leave","attendance","payroll","performance","recruitment","system"],Wa=[{value:"in_app",label:"In-App"},{value:"email",label:"Email"},{value:"push",label:"Push"},{value:"sms",label:"SMS"},{value:"whatsapp",label:"WhatsApp"}],js={queued:{label:"Queued",variant:"secondary",color:"#64748b"},sending:{label:"Sending",variant:"warning",color:"#f59e0b"},delivered:{label:"Delivered",variant:"success",color:"#10b981"},failed:{label:"Failed",variant:"destructive",color:"#ef4444"},bounced:{label:"Bounced",variant:"secondary",color:"#f97316"}},za="/firebase-messaging-sw.js",ue="pp_push_token";function Ua(){const[s,a]=f.useState("default"),[t,c]=f.useState("");f.useEffect(()=>{let x=!0;return(async()=>{const b=await Ns().catch(()=>!1);if(x){if(!b||!("serviceWorker"in navigator)||!("Notification"in window)){a("unsupported");return}a(Notification.permission)}})(),()=>{x=!1}},[]);const r=f.useCallback(async()=>{c(""),a("registering");try{const{firebaseConfig:x,vapidKey:b}=await ws();if(!(x!=null&&x.projectId)||!b)throw new Error("Web push is not configured by an admin yet (missing Firebase web config or VAPID key).");ks().length===0&&Ss(x);const T=new URLSearchParams({apiKey:x.apiKey??"",authDomain:x.authDomain??"",projectId:x.projectId??"",messagingSenderId:x.messagingSenderId??"",appId:x.appId??""}).toString(),S=await navigator.serviceWorker.register(`${za}?${T}`),p=await Notification.requestPermission();if(p!=="granted")return a(p),!1;const h=Cs(),v=await _s(h,{vapidKey:b,serviceWorkerRegistration:S});if(!v)throw new Error("Failed to obtain a device token from Firebase.");return await Ts(v,"web"),localStorage.setItem(ue,v),a("granted"),!0}catch(x){return c(x instanceof Error?x.message:String(x)),a("error"),!1}},[]),d=f.useCallback(async()=>{const x=localStorage.getItem(ue);x&&(await As(x).catch(()=>{}),localStorage.removeItem(ue)),a("default")},[]);return{status:s,error:t,enable:r,disable:d}}function Ka(){const{status:s,error:a,enable:t,disable:c}=Ua();if(s==="unsupported")return e.jsxs("div",{className:"flex items-center gap-3 rounded-lg border border-slate-200 bg-slate-50 px-4 py-3",children:[e.jsx(ma,{className:"h-4 w-4 text-slate-400 shrink-0"}),e.jsx("p",{className:"text-[12px] text-slate-500",children:"This browser doesn't support push notifications."})]});const r=s==="granted",d=s==="registering";return e.jsxs("div",{className:"rounded-lg border border-slate-200 bg-white px-4 py-3",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx("div",{className:`flex h-9 w-9 shrink-0 items-center justify-center rounded-lg ${r?"bg-orange-50 text-orange-500":"bg-slate-100 text-slate-400"}`,children:e.jsx(Z,{className:"h-4 w-4"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-800",children:"Push on this device"}),e.jsx("p",{className:"text-[11px] text-slate-400 leading-snug",children:r?"This browser is registered for push notifications.":s==="denied"?"Blocked — enable notifications for this site in your browser settings.":"Get alerts even when PeoplePin isn’t open in a tab."})]})]}),e.jsx("div",{className:"shrink-0",children:r?e.jsx(P,{variant:"outline",size:"sm",onClick:c,children:"Disable"}):e.jsx(P,{size:"sm",onClick:t,disabled:d||s==="denied",children:d?e.jsxs(e.Fragment,{children:[e.jsx(ee,{className:"h-3.5 w-3.5 mr-1.5 animate-spin"})," Enabling…"]}):"Enable"})})]}),s==="error"&&a&&e.jsxs("div",{className:"mt-2 flex items-start gap-1.5 text-[11px] text-red-500",children:[e.jsx(oe,{className:"h-3.5 w-3.5 shrink-0 mt-0.5"}),e.jsx("span",{children:a})]})]})}function Ba({channel:s,enabledCount:a,totalCount:t,onToggleAll:c}){const r=a===t,d=a>0&&!r,x=s.icon;return e.jsxs("div",{className:_("flex items-center gap-3 rounded-xl border p-4 bg-white",s.borderColor),children:[e.jsx("div",{className:_("flex h-9 w-9 shrink-0 items-center justify-center rounded-lg",s.bgColor,s.color),children:e.jsx(x,{className:"h-4 w-4"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-800",children:s.label}),e.jsx("p",{className:"text-[11px] text-slate-400 leading-snug",children:s.description})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[d&&e.jsxs("span",{className:"text-[11px] text-slate-400",children:[a,"/",t]}),e.jsx(ie,{checked:r||d,onCheckedChange:b=>c(s.id,b),"aria-label":`Toggle all ${s.label} notifications`})]})]})}function Ha({event:s,prefs:a,onToggle:t}){return e.jsxs("div",{className:"group grid grid-cols-[1fr_repeat(5,52px)] items-center gap-2 rounded-lg px-4 py-3 transition-colors hover:bg-slate-50/60",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-[13px] font-medium text-slate-700 leading-snug",children:s.label}),s.transactional&&e.jsx(D,{variant:"secondary",className:"text-[10px] px-1.5 py-0",children:"Always on"})]}),e.jsx("p",{className:"mt-0.5 text-[11px] text-slate-400 leading-snug",children:s.description})]}),R.map(c=>e.jsx("div",{className:"flex justify-center",children:e.jsx(ie,{checked:s.transactional?!0:a[c.id]??!1,disabled:s.transactional,onCheckedChange:()=>t(s.code,c.id),"aria-label":`${s.label} via ${c.label}`})},c.id))]})}function Va({category:s,prefs:a,onToggle:t,isOpen:c,onToggleOpen:r,searchQuery:d}){const x=s.icon,b=f.useMemo(()=>{if(!d)return s.events;const p=d.toLowerCase();return s.events.filter(h=>h.label.toLowerCase().includes(p)||h.code.toLowerCase().includes(p))},[s.events,d]);if(d&&b.length===0)return null;const T=b.length*R.length,S=b.reduce((p,h)=>{const v=Fe(a,h.code);return p+R.filter(u=>v[u.id]).length},0);return e.jsxs("div",{className:"rounded-xl border border-slate-200 bg-white overflow-hidden",children:[e.jsxs("button",{onClick:r,className:"flex w-full items-center gap-3 px-4 py-3.5 hover:bg-slate-50 transition-colors",children:[e.jsx("div",{className:"flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-slate-100 text-slate-500",children:e.jsx(x,{className:"h-4 w-4"})}),e.jsxs("div",{className:"flex-1 text-left min-w-0",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-800",children:s.label}),e.jsxs("p",{className:"text-[11px] text-slate-400",children:[b.length," events · ",S," of ",T," channels enabled"]})]}),c?e.jsx(Es,{className:"h-4 w-4 text-slate-400 shrink-0"}):e.jsx(Ds,{className:"h-4 w-4 text-slate-400 shrink-0"})]}),c&&e.jsxs(e.Fragment,{children:[e.jsx(Be,{children:e.jsxs("div",{className:"grid grid-cols-[1fr_repeat(5,52px)] items-center gap-2 border-t border-slate-100 bg-slate-50/60 px-4 py-2",children:[e.jsx("p",{className:"text-[11px] font-medium text-slate-400 uppercase tracking-wide",children:"Event"}),R.map(p=>{const h=p.icon;return e.jsxs(He,{children:[e.jsx(Ve,{asChild:!0,children:e.jsxs("div",{className:"flex flex-col items-center gap-0.5 cursor-default",children:[e.jsx(h,{className:_("h-3.5 w-3.5",p.color)}),e.jsx("span",{className:"text-[10px] text-slate-400",children:p.label})]})}),e.jsx(Ge,{side:"top",className:"text-[11px]",children:p.description})]},p.id)})]})}),e.jsx("div",{className:"divide-y divide-slate-100/70",children:b.map(p=>e.jsx(Ha,{event:p,prefs:Fe(a,p.code),onToggle:t},p.code))})]})]})}function Ga({categories:s,prefs:a,dirty:t,savedAt:c,isSaving:r,isLoading:d,isError:x,onToggle:b,onToggleAll:T,onSave:S,onDiscard:p,onRetry:h}){const[v,u]=f.useState(""),[i,o]=f.useState(()=>{var w;return new Set([((w=s[0])==null?void 0:w.id)??""])}),C=s.flatMap(w=>w.events),y=C.length,l=i.size===s.length;function N(w){o(g=>{const q=new Set(g);return q.has(w)?q.delete(w):q.add(w),q})}function k(){o(new Set(s.map(w=>w.id)))}function E(){o(new Set)}return d?e.jsxs("div",{className:"space-y-5",children:[e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:Array.from({length:5}).map((w,g)=>e.jsx(F,{variant:"rectangular",className:"h-20 rounded-lg"},g))}),e.jsx(F,{className:"h-px w-full"}),Array.from({length:3}).map((w,g)=>e.jsx(F,{variant:"rectangular",className:"h-16 rounded-lg"},g))]}):x?e.jsx(H,{icon:oe,title:"Failed to load preferences",description:"Something went wrong fetching your notification preferences.",action:e.jsx(P,{size:"sm",onClick:h,children:"Retry"})}):e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"rounded-lg border border-slate-200 bg-slate-50/60 px-4 py-3",children:[e.jsx("p",{className:"text-[13px] font-medium text-slate-700",children:"Your personal notification preferences"}),e.jsxs("p",{className:"mt-0.5 text-[12px] text-slate-500 leading-relaxed",children:["Choose which channels ",e.jsx("strong",{children:"you"})," want to receive each notification on. This only affects your own account. ",e.jsx("em",{children:"Who"})," gets notified and the default channels are set by an admin under ",e.jsx("strong",{children:"Alert Triggers"}),". Events marked ",e.jsx("strong",{children:"Always on"})," ","(invites, password & payslip alerts) can't be turned off."]})]}),e.jsx(Ka,{}),e.jsxs("div",{children:[e.jsx("p",{className:"mb-3 text-[12px] font-semibold uppercase tracking-wider text-slate-400",children:"Channels"}),e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:R.map(w=>{const g=C.filter(q=>{var U;return((U=a[q.code])==null?void 0:U[w.id])??!1}).length;return e.jsx(Ba,{channel:w,enabledCount:g,totalCount:y,onToggleAll:T},w.id)})})]}),e.jsx(Ke,{}),e.jsxs("div",{children:[e.jsxs("div",{className:"mb-3 flex items-center justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"flex items-center gap-3 flex-1 min-w-0",children:[e.jsx("p",{className:"text-[12px] font-semibold uppercase tracking-wider text-slate-400 shrink-0",children:"Event Preferences"}),e.jsxs("div",{className:"relative max-w-xs flex-1",children:[e.jsx(Ps,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-slate-400"}),e.jsx(B,{value:v,onChange:w=>u(w.target.value),placeholder:"Search events...",className:"h-8 pl-8 text-[12px]"})]})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsx("button",{onClick:l?E:k,className:"flex items-center gap-1 text-[11px] text-slate-500 hover:text-slate-700 transition-colors",children:l?e.jsxs(e.Fragment,{children:[e.jsx(Ma,{className:"h-3.5 w-3.5"})," Collapse all"]}):e.jsxs(e.Fragment,{children:[e.jsx(ua,{className:"h-3.5 w-3.5"})," Expand all"]})}),c&&!t&&e.jsxs("span",{className:"flex items-center gap-1.5 text-[12px] text-green-600",children:[e.jsx(de,{className:"h-3.5 w-3.5"}),"Saved"]}),t&&e.jsx("span",{className:"text-[12px] text-amber-600 font-medium",children:"Unsaved changes"}),e.jsxs(D,{variant:"secondary",className:"text-[11px]",children:[y," events"]})]})]}),e.jsx("div",{className:"space-y-3",children:s.map(w=>e.jsx(Va,{category:w,prefs:a,onToggle:b,isOpen:i.has(w.id),onToggleOpen:()=>N(w.id),searchQuery:v},w.id))})]}),t&&e.jsx("div",{className:"sticky bottom-0 border-t border-amber-100 bg-amber-50/90 px-6 py-3 backdrop-blur-sm rounded-b-lg",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-[13px] text-amber-700",children:"You have unsaved preference changes."}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(P,{variant:"outline",size:"sm",onClick:p,children:"Discard"}),e.jsx(P,{size:"sm",onClick:S,disabled:r,children:r?e.jsxs(e.Fragment,{children:[e.jsx(ee,{className:"h-3.5 w-3.5 animate-spin"})," Saving…"]}):"Save Changes"})]})]})})]})}const Xa=Pe.flatMap(s=>s.events.map(a=>({...a,categoryLabel:s.label})));function Ee(s){return Xa.find(a=>a.code===s)??null}const $a=fa({module:Q().min(1,"Module is required"),event_code:Q().min(1,"Event code is required").regex(/^[a-z0-9_.]+$/,"Lowercase letters, dots, and underscores only"),channel:ya(["in_app","email","push","sms","whatsapp"]),subject:Q().min(1,"Subject is required"),html_body:Q().optional(),text_body:Q().min(1,"Text body is required"),description:Q().optional(),is_active:ja()});function Ya({eventCode:s}){const a=f.useMemo(()=>Ee(s),[s]);return a?e.jsxs("div",{className:"rounded-lg border border-amber-200/60 bg-amber-50/40 p-3 space-y-2.5",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"flex h-6 w-6 items-center justify-center rounded-md bg-amber-100 text-amber-600",children:e.jsx(se,{className:"h-3 w-3"})}),e.jsx("p",{className:"text-[12px] font-semibold text-slate-700",children:"Automatic Trigger"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-[11px] text-slate-500",children:"Event"}),e.jsx("span",{className:"text-[11px] font-medium text-slate-700",children:a.label})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-[11px] text-slate-500",children:"Module"}),e.jsx("span",{className:"text-[11px] font-medium text-slate-700",children:a.categoryLabel})]}),e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsx("span",{className:"text-[11px] text-slate-500 shrink-0",children:"Trigger"}),e.jsx("span",{className:"text-[11px] text-slate-600 text-right",children:a.description})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-[11px] text-slate-500",children:"Default Channels"}),e.jsx("div",{className:"flex items-center gap-1",children:a.defaultChannels.map(t=>{const c=R.find(r=>r.id===t);return c?e.jsx(D,{variant:"secondary",className:"text-[9px] px-1.5 py-0",children:c.label},t):null})})]})]}),e.jsx("p",{className:"text-[10px] text-amber-600/80 leading-relaxed",children:"This template will be dispatched automatically when the above event fires, respecting each recipient's channel preferences."})]}):e.jsx("div",{className:"rounded-lg border border-dashed border-slate-200 bg-slate-50/50 p-3",children:e.jsxs("div",{className:"flex items-center gap-2 text-slate-400",children:[e.jsx(Ls,{className:"h-3.5 w-3.5 shrink-0"}),e.jsxs("p",{className:"text-[11px]",children:["Enter a valid event code to see trigger rules. Use the catalogue codes like",e.jsx("span",{className:"font-mono ml-1",children:"leave.applied"}),"."]})]})})}function Ja({eventCode:s}){const{data:a=[]}=ye(),t=a.find(r=>r.key===s);if(!t||t.variables.length===0)return null;async function c(r){try{await navigator.clipboard.writeText(`{{${r}}}`)}catch{}}return e.jsxs("div",{className:"rounded-lg border border-slate-200 bg-slate-50/60 p-3 space-y-2",children:[e.jsxs("p",{className:"text-[11px] font-semibold text-slate-600",children:["Variables for ",e.jsx("span",{className:"font-mono",children:t.key}),e.jsx("span",{className:"font-normal text-slate-400",children:" — click to copy"})]}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:t.variables.map(r=>e.jsx("button",{type:"button",title:r.description+(r.example?` (e.g. ${r.example})`:""),onClick:()=>c(r.name),className:"text-[11px] font-mono bg-white border border-slate-200 rounded px-1.5 py-0.5 text-slate-600 hover:border-primary/40 hover:text-primary transition-colors",children:`{{${r.name}}}`},r.name))})]})}function Qa({value:s,onChange:a,error:t}){return e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(M,{children:"Event Code"}),e.jsxs(V,{value:s||"_custom",onValueChange:c=>a(c==="_custom"?"":c),children:[e.jsx(G,{className:"h-8 text-[13px] font-mono",children:e.jsx(X,{placeholder:"Select or type event code..."})}),e.jsxs($,{className:"max-h-64",children:[e.jsx(W,{value:"_custom",className:"text-slate-400 italic text-[12px]",children:"Custom event code..."}),Pe.map(c=>e.jsxs("div",{children:[e.jsx("div",{className:"px-2 py-1.5 text-[10px] font-semibold text-slate-400 uppercase tracking-wider",children:c.label}),c.events.map(r=>e.jsxs(W,{value:r.code,className:"text-[12px]",children:[e.jsx("span",{className:"font-mono text-slate-600",children:r.code}),e.jsxs("span",{className:"ml-2 text-slate-400",children:["— ",r.label]})]},r.code))]},c.id))]})]}),s&&!Ee(s)&&e.jsx(B,{value:s,onChange:c=>a(c.target.value),placeholder:"e.g. custom.event.code",className:"h-8 text-[13px] font-mono mt-1.5"}),t&&e.jsx("p",{className:"text-[11px] text-red-500",children:t})]})}function Za({template:s,open:a,onClose:t}){const c=!!s,r=Ms(),d=Is(),x=r.isPending||d.isPending,b=ga({resolver:ba($a),defaultValues:{module:(s==null?void 0:s.module)??"",event_code:(s==null?void 0:s.event_code)??"",channel:(s==null?void 0:s.channel)??"in_app",subject:(s==null?void 0:s.subject)??"",html_body:(s==null?void 0:s.html_body)??"",text_body:(s==null?void 0:s.text_body)??"",description:(s==null?void 0:s.description)??"",is_active:(s==null?void 0:s.is_active)??!0}}),{register:T,handleSubmit:S,control:p,reset:h,setValue:v,formState:{errors:u}}=b,i=b.watch("channel"),o=b.watch("event_code");f.useEffect(()=>{if(Ee(o)){const l=Pe.find(N=>N.events.some(k=>k.code===o));if(l){const N=Re.find(k=>k===l.id);N&&v("module",N)}}},[o,v]),f.useEffect(()=>{h({module:(s==null?void 0:s.module)??"",event_code:(s==null?void 0:s.event_code)??"",channel:(s==null?void 0:s.channel)??"in_app",subject:(s==null?void 0:s.subject)??"",html_body:(s==null?void 0:s.html_body)??"",text_body:(s==null?void 0:s.text_body)??"",description:(s==null?void 0:s.description)??"",is_active:(s==null?void 0:s.is_active)??!0})},[s,h]);function C(y){const l={...y,html_body:y.channel==="email"?y.html_body??"":""};c&&s?d.mutate({id:s.id,...l},{onSuccess:t}):r.mutate(l,{onSuccess:t})}return e.jsx(Xe,{open:a,onOpenChange:y=>!y&&t(),children:e.jsxs($e,{size:"lg",children:[e.jsxs(Ye,{children:[e.jsx(Je,{children:c?"Edit Template":"Create Template"}),e.jsx(Qe,{children:c?"Update the notification template content and settings.":"Define a new notification template for a specific event and channel."})]}),e.jsxs("form",{onSubmit:S(C),className:"flex flex-col flex-1 overflow-hidden",children:[e.jsxs("div",{className:"flex-1 overflow-y-auto px-6 py-5 space-y-5",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(M,{children:"Module"}),e.jsx(ae,{name:"module",control:p,render:({field:y})=>e.jsxs(V,{value:y.value,onValueChange:y.onChange,children:[e.jsx(G,{className:"h-8 text-[13px]",children:e.jsx(X,{placeholder:"Select module..."})}),e.jsx($,{children:Re.map(l=>e.jsx(W,{value:l,className:"capitalize",children:l},l))})]})}),u.module&&e.jsx("p",{className:"text-[11px] text-red-500",children:u.module.message})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(M,{children:"Channel"}),e.jsx(ae,{name:"channel",control:p,render:({field:y})=>e.jsxs(V,{value:y.value,onValueChange:l=>y.onChange(l),children:[e.jsx(G,{className:"h-8 text-[13px]",children:e.jsx(X,{})}),e.jsx($,{children:Wa.map(l=>e.jsx(W,{value:l.value,children:l.label},l.value))})]})})]})]}),e.jsx(ae,{name:"event_code",control:p,render:({field:y})=>{var l;return e.jsx(Qa,{value:y.value,onChange:y.onChange,error:(l=u.event_code)==null?void 0:l.message})}}),e.jsx(Ya,{eventCode:o}),e.jsx(Ja,{eventCode:o}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(M,{children:"Subject"}),e.jsx(B,{...T("subject"),placeholder:"e.g. Leave Request Submitted",className:"h-8 text-[13px]"}),u.subject&&e.jsx("p",{className:"text-[11px] text-red-500",children:u.subject.message})]}),e.jsx(Ke,{}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(M,{children:i==="email"?"Email Body":"Body Content"}),e.jsx(ae,{name:i==="email"?"html_body":"text_body",control:p,render:({field:y})=>e.jsx(Na,{value:y.value??"",onChange:y.onChange,placeholder:i==="email"?"Design your email notification content...":"Write your notification message..."},i)}),e.jsxs("p",{className:"text-[10px] text-slate-400",children:["Use the toolbar to format text. Template variables like ","{{user.first_name}}"," will be rendered at send time."]}),i!=="email"&&u.text_body&&e.jsx("p",{className:"text-[11px] text-red-500",children:u.text_body.message})]}),i==="email"&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(M,{children:["Plain Text Fallback",e.jsx("span",{className:"font-normal text-slate-400 ml-1",children:"(for email clients without HTML support)"})]}),e.jsx("textarea",{...T("text_body"),rows:3,placeholder:"Hi {{user.first_name}}, your leave request has been submitted.",className:_("flex w-full rounded-md border border-input bg-white px-3 py-2 text-[12px] font-mono","text-foreground placeholder:text-slate-400 focus-visible:outline-none","focus-visible:ring-2 focus-visible:ring-primary/20 focus-visible:border-primary/40","transition-colors resize-none")}),u.text_body&&e.jsx("p",{className:"text-[11px] text-red-500",children:u.text_body.message})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs(M,{children:["Description ",e.jsx("span",{className:"font-normal text-slate-400",children:"(optional)"})]}),e.jsx(B,{...T("description"),placeholder:"Brief description of when this template is used",className:"h-8 text-[13px]"})]}),e.jsxs("div",{className:"flex items-center justify-between rounded-lg border border-slate-200 px-3 py-2.5",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[13px] font-medium text-slate-700",children:"Active"}),e.jsx("p",{className:"text-[11px] text-slate-400",children:"Enable this template for automatic dispatch"})]}),e.jsx(ae,{name:"is_active",control:p,render:({field:y})=>e.jsx(ie,{checked:y.value,onCheckedChange:y.onChange})})]})]}),e.jsxs(Ze,{children:[e.jsx(P,{type:"button",variant:"outline",size:"sm",onClick:t,children:"Cancel"}),e.jsx(P,{type:"submit",size:"sm",disabled:x,children:x?"Saving...":c?"Save Changes":"Create Template"})]})]})]})})}function et({tpl:s,onEdit:a,onDelete:t}){const[c,r]=f.useState(!1),d=K.find(x=>x.type===s.channel);return e.jsxs("div",{className:"rounded-xl border border-slate-200 bg-white overflow-hidden",children:[e.jsxs("div",{className:"p-4 space-y-2.5",children:[e.jsxs("div",{className:"flex items-start gap-2.5",children:[e.jsx("div",{className:_("flex h-9 w-9 shrink-0 items-center justify-center rounded-lg mt-0.5",(d==null?void 0:d.bgColor)??"bg-slate-100",(d==null?void 0:d.color)??"text-slate-500"),children:d&&e.jsx(d.icon,{className:"h-4 w-4"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-800 truncate",children:s.subject||"(no subject)"}),e.jsx(va,{status:s.is_active?"active":"inactive"})]}),e.jsx("p",{className:"mt-0.5 text-[11px] text-slate-400 font-mono",children:s.event_code})]}),e.jsxs(es,{children:[e.jsx(ss,{asChild:!0,children:e.jsx("button",{className:"flex h-7 w-7 items-center justify-center rounded-lg text-slate-400 hover:bg-slate-100 hover:text-slate-600 transition-colors shrink-0",children:e.jsx(xs,{className:"h-4 w-4"})})}),e.jsxs(as,{align:"end",className:"w-40",children:[e.jsxs(ne,{className:"gap-2 text-[13px]",onClick:()=>a(s),children:[e.jsx(ps,{className:"h-3.5 w-3.5"})," Edit"]}),e.jsxs(ne,{className:"gap-2 text-[13px]",onClick:()=>r(x=>!x),children:[e.jsx(hs,{className:"h-3.5 w-3.5"})," ",c?"Hide Preview":"Preview"]}),e.jsx(Fs,{}),e.jsxs(ne,{className:"gap-2 text-[13px] text-red-600 focus:text-red-600",onClick:()=>t(s),children:[e.jsx(ve,{className:"h-3.5 w-3.5"})," Delete"]})]})]})]}),s.description&&e.jsx("p",{className:"text-[12px] text-slate-500 leading-relaxed",children:s.description}),e.jsxs("div",{className:"flex items-center gap-2 pt-0.5 flex-wrap",children:[e.jsx(D,{variant:"outline",className:"text-[10px] text-slate-500",children:s.module}),e.jsx(D,{variant:"outline",className:_("text-[10px]",(d==null?void 0:d.color)??"text-slate-500"),children:(d==null?void 0:d.label)??s.channel}),s.channel==="email"&&s.html_body&&e.jsx(D,{variant:"outline",className:"text-[10px] text-violet-500",children:"HTML"})]})]}),e.jsx("div",{className:"border-t border-slate-100 px-4 py-2 bg-slate-50/50",children:e.jsxs("span",{className:"text-[10px] text-slate-400",children:["Updated ",new Date(s.updated_at).toLocaleDateString()]})}),c&&e.jsxs("div",{className:"border-t border-slate-100 p-4 bg-slate-50/30 space-y-2",children:[s.channel==="email"&&s.html_body?e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] font-medium text-slate-400 uppercase tracking-wide mb-1.5",children:"HTML Body"}),e.jsx("div",{className:"rounded-lg border border-slate-200 bg-white p-3 text-[11px] text-slate-600 overflow-auto max-h-48",children:e.jsxs("pre",{className:"whitespace-pre-wrap font-mono leading-relaxed",children:[s.html_body.slice(0,500),s.html_body.length>500?"…":""]})})]}):null,e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] font-medium text-slate-400 uppercase tracking-wide mb-1.5",children:"Text Body"}),s.text_body?e.jsx("div",{className:"rounded-lg border border-slate-200 bg-white p-3",children:e.jsx("pre",{className:"text-[11px] text-slate-600 whitespace-pre-wrap font-mono leading-relaxed",children:s.text_body})}):e.jsx("p",{className:"text-[11px] text-slate-400 italic",children:"No text body defined."})]})]})]})}function st({template:s,open:a,onClose:t}){const c=Rs();function r(){s&&c.mutate(s.id,{onSuccess:t})}return e.jsx(Ne,{open:a,onOpenChange:d=>!d&&t(),children:e.jsxs(we,{children:[e.jsx(ke,{children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx(ts,{variant:"destructive"}),e.jsxs("div",{children:[e.jsx(Se,{children:"Delete Template"}),e.jsxs(Ce,{children:["Are you sure you want to delete the template “",s==null?void 0:s.subject,"”? This action cannot be undone."]})]})]})}),e.jsxs(_e,{children:[e.jsx(Te,{onClick:t,children:"Cancel"}),e.jsx(Ae,{variant:"destructive",onClick:r,disabled:c.isPending,children:c.isPending?"Deleting...":"Delete"})]})]})})}function at(){var y;const{data:s=[],isLoading:a,isError:t,refetch:c}=qs(),[r,d]=f.useState("in_app"),[x,b]=f.useState("all"),[T,S]=f.useState(!1),[p,h]=f.useState(null),[v,u]=f.useState(null),i=K.reduce((l,N)=>(l[N.type]=s.filter(k=>k.channel===N.type),l),{in_app:[],email:[],push:[],sms:[],whatsapp:[]}),o=Array.from(new Set(s.map(l=>l.module))).sort(),C=i[r].filter(l=>x==="all"||l.module===x);return a?e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"flex items-center gap-3",children:Array.from({length:5}).map((l,N)=>e.jsx(F,{variant:"rectangular",className:"h-8 w-20 rounded-full"},N))}),e.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-4",children:Array.from({length:4}).map((l,N)=>e.jsx(F,{variant:"rectangular",className:"h-28 rounded-lg"},N))})]}):t?e.jsx(H,{icon:fe,title:"Failed to load templates",description:"Something went wrong fetching your notification templates.",action:e.jsx(P,{size:"sm",onClick:()=>c(),children:"Retry"})}):e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[14px] font-semibold text-slate-800",children:"Template Gallery"}),e.jsx("p",{className:"text-[12px] text-slate-400 mt-0.5 leading-relaxed",children:"Notification templates per channel. Each template defines the message format for a specific event and delivery channel."})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsxs(D,{variant:"secondary",className:"text-[11px]",children:[s.length," templates"]}),e.jsxs(P,{size:"sm",onClick:()=>S(!0),children:[e.jsx(le,{className:"h-3.5 w-3.5 mr-1"}),"Create Template"]})]})]}),e.jsx("div",{className:"flex items-center gap-2 flex-wrap",children:K.map(l=>{const N=l.icon,k=i[l.type].length,E=r===l.type;return e.jsxs("button",{onClick:()=>d(l.type),className:_("flex items-center gap-2 rounded-lg border px-3.5 py-2 text-[12px] font-medium transition-all",E?_("bg-white shadow-sm",l.borderColor,l.color):"border-transparent bg-slate-100 text-slate-500 hover:bg-slate-200"),children:[e.jsx("div",{className:_("flex h-5 w-5 items-center justify-center rounded",E?_(l.bgColor,l.color):""),children:e.jsx(N,{className:"h-3 w-3"})}),l.label,e.jsx("span",{className:_("rounded-full px-1.5 py-0.5 text-[10px] font-semibold",E?_(l.bgColor,l.color):"bg-slate-200 text-slate-500"),children:k})]},l.type)})}),o.length>1&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-[11px] text-slate-400",children:"Filter by module:"}),e.jsxs("div",{className:"flex gap-1 flex-wrap",children:[e.jsx("button",{onClick:()=>b("all"),className:_("px-2.5 py-1 rounded-md text-[11px] font-medium transition-colors",x==="all"?"bg-slate-800 text-white":"bg-slate-100 text-slate-500 hover:bg-slate-200"),children:"All"}),o.map(l=>e.jsx("button",{onClick:()=>b(l),className:_("px-2.5 py-1 rounded-md text-[11px] font-medium transition-colors",x===l?"bg-slate-800 text-white":"bg-slate-100 text-slate-500 hover:bg-slate-200"),children:l},l))]})]}),C.length===0?e.jsx(H,{icon:fe,title:`No templates for ${((y=K.find(l=>l.type===r))==null?void 0:y.label)??r}`,description:'Click "Create Template" to add a template for this channel.'}):e.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3",children:C.map(l=>e.jsx(et,{tpl:l,onEdit:h,onDelete:u},l.id))}),e.jsx(Za,{template:T?null:p,open:T||!!p,onClose:()=>{S(!1),h(null)}}),e.jsx(st,{template:v,open:!!v,onClose:()=>u(null)})]})}const ge="••••••••";function tt({field:s,value:a,onChange:t}){var T,S,p;const[c,r]=f.useState(!1),d=f.useRef(null),[x,b]=f.useState("");if(s.type==="file"){const h=!!a,v=a===ge;async function u(i){var y;const o=(y=i.target.files)==null?void 0:y[0];if(!o)return;const C=await o.text();b(o.name),t(C)}return e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("input",{ref:d,type:"file",accept:s.accept,onChange:u,className:"hidden"}),h?e.jsxs("div",{className:"flex items-center justify-between rounded-md border border-slate-200 bg-slate-50 px-3 py-2",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx(Ia,{className:"h-4 w-4 text-green-600 shrink-0"}),e.jsx("span",{className:"text-[12px] text-slate-600 truncate",children:x||(v?"File uploaded (stored securely)":"File loaded")})]}),e.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[e.jsx(P,{type:"button",variant:"ghost",size:"sm",className:"h-7 text-[11px]",onClick:()=>{var i;return(i=d.current)==null?void 0:i.click()},children:"Replace"}),e.jsx("button",{type:"button",onClick:()=>{b(""),t("")},className:"text-slate-400 hover:text-red-500","aria-label":"Remove file",children:e.jsx(Os,{className:"h-3.5 w-3.5"})})]})]}):e.jsxs(P,{type:"button",variant:"outline",size:"sm",className:"w-full h-9 text-[12px]",onClick:()=>{var i;return(i=d.current)==null?void 0:i.click()},children:[e.jsx(Ws,{className:"h-3.5 w-3.5 mr-1.5"})," Upload ",(s.accept||"file").split(",")[0]," file"]})]})}return s.type==="textarea"?e.jsx(ls,{value:a===ge?"":a,placeholder:a===ge?"Stored — paste again to replace":s.placeholder,onChange:h=>t(h.target.value),rows:5,className:"text-[12px] font-mono"}):s.type==="select"?e.jsxs(V,{value:a||((S=(T=s.options)==null?void 0:T[0])==null?void 0:S.value)||"",onValueChange:t,children:[e.jsx(G,{className:"w-full h-9 text-[12px]",children:e.jsx(X,{})}),e.jsx($,{children:(p=s.options)==null?void 0:p.map(h=>e.jsx(W,{value:h.value,className:"text-[12px]",children:h.label},h.value))})]}):s.type==="password"?e.jsxs("div",{className:"relative",children:[e.jsx(B,{type:c?"text":"password",value:a,placeholder:s.placeholder,onChange:h=>t(h.target.value),className:"pr-9 text-[12px]"}),e.jsx("button",{type:"button",onClick:()=>r(h=>!h),className:"absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 transition-colors",tabIndex:-1,children:c?e.jsx(wa,{className:"h-3.5 w-3.5"}):e.jsx(hs,{className:"h-3.5 w-3.5"})})]}):e.jsx(B,{type:s.type==="number"?"number":"text",value:a,placeholder:s.placeholder,onChange:h=>t(h.target.value),className:"text-[12px]"})}function lt({provider:s,onToggle:a,onRemove:t,onSave:c,onTest:r,isSaving:d,isTesting:x}){var w;const[b,T]=f.useState(!1),[S,p]=f.useState(s.credentials),[h,v]=f.useState("");f.useEffect(()=>{p(s.credentials)},[s.credentials]);const u=K.find(g=>g.type===s.channel),i=u==null?void 0:u.providers.find(g=>g.id===s.provider_code);if(!u||!i)return null;const o=u.icon,C=i.fields.filter(g=>S[g.key]&&S[g.key]!=="••••••••").length,y=i.fields.filter(g=>S[g.key]==="••••••••").length,l=i.fields.filter(g=>g.required).length,N=i.fields.filter(g=>g.required&&S[g.key]&&S[g.key]!=="••••••••").length,k=i.fields.filter(g=>g.required&&S[g.key]==="••••••••").length,E=JSON.stringify(S)!==JSON.stringify(s.credentials);return e.jsxs("div",{className:_("rounded-xl border bg-white overflow-hidden transition-shadow",s.is_enabled?u.borderColor:"border-slate-200",b?"shadow-sm":""),children:[e.jsxs("div",{className:"flex items-center gap-3 px-5 py-4",children:[e.jsx("div",{className:_("flex h-10 w-10 shrink-0 items-center justify-center rounded-xl",s.is_enabled?_(u.bgColor,u.color):"bg-slate-100 text-slate-400"),children:e.jsx(o,{className:"h-4.5 w-4.5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("p",{className:_("text-[13px] font-semibold",s.is_enabled?"text-slate-800":"text-slate-500"),children:u.label}),e.jsx(D,{variant:"outline",className:"text-[10px] py-0 font-mono",children:i.name}),s.is_verified?e.jsxs(D,{className:"text-[10px] py-0 bg-emerald-50 text-emerald-700 border-emerald-200",children:[e.jsx(Zs,{className:"h-2.5 w-2.5 mr-0.5"}),"Verified"]}):N+k===l&&l>0?e.jsx(D,{variant:"secondary",className:"text-[10px] py-0",children:"Ready"}):e.jsxs(D,{variant:"secondary",className:"text-[10px] py-0 text-amber-600 bg-amber-50 border-amber-200",children:[N+k,"/",l," required"]})]}),e.jsxs("p",{className:"text-[11px] text-slate-400 truncate mt-0.5",children:[i.description,s.last_tested_at&&e.jsxs("span",{className:"ml-2 text-slate-300",children:["· Tested ",new Date(s.last_tested_at).toLocaleDateString()]})]})]}),e.jsxs("div",{className:"flex items-center gap-1.5 shrink-0",children:[e.jsx("button",{onClick:()=>T(g=>!g),className:_("flex h-7 w-7 items-center justify-center rounded-lg transition-colors",b?"bg-slate-100 text-slate-700":"text-slate-400 hover:bg-slate-100 hover:text-slate-600"),title:"Configure",children:e.jsx(ms,{className:"h-3.5 w-3.5"})}),e.jsx("button",{onClick:t,className:"flex h-7 w-7 items-center justify-center rounded-lg text-slate-400 hover:bg-red-50 hover:text-red-500 transition-colors",title:"Remove channel",children:e.jsx(ve,{className:"h-3.5 w-3.5"})}),e.jsx(ie,{checked:s.is_enabled,onCheckedChange:a})]})]}),b&&e.jsxs("div",{className:"border-t border-slate-100 bg-slate-50/50",children:[e.jsxs("div",{className:"flex items-center gap-2 px-5 py-2.5 border-b border-slate-100 bg-slate-50 flex-wrap",children:[e.jsx(Ra,{className:"h-3 w-3 text-slate-400"}),e.jsxs("span",{className:"text-[11px] font-medium text-slate-500",children:["Provider: ",i.name]}),e.jsx("span",{className:"text-[11px] text-slate-300",children:"·"}),e.jsxs("span",{className:"text-[11px] text-slate-400",children:[C+y,"/",i.fields.length," fields configured"]}),((w=s.test_result)==null?void 0:w.message)&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-[11px] text-slate-300",children:"·"}),e.jsx("span",{className:_("text-[11px]",s.test_result.success?"text-emerald-600":"text-red-500"),children:s.test_result.message})]})]}),e.jsxs("div",{className:"px-5 py-4 space-y-3.5",children:[i.fields.map(g=>e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-1.5 mb-1.5",children:[g.type==="password"?e.jsx(qa,{className:"h-3 w-3 text-slate-400"}):e.jsx(La,{className:"h-3 w-3 text-slate-400"}),e.jsxs("label",{className:"text-[11px] font-medium text-slate-600",children:[g.label,g.required&&e.jsx("span",{className:"text-red-400 ml-0.5",children:"*"})]})]}),e.jsx(tt,{field:g,value:S[g.key]??"",onChange:q=>p(U=>({...U,[g.key]:q}))}),g.hint&&e.jsx("p",{className:"mt-1 text-[10px] text-slate-400",children:g.hint})]},g.key)),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-1.5 mb-1.5",children:[e.jsx(Ie,{className:"h-3 w-3 text-slate-400"}),e.jsx("label",{className:"text-[11px] font-medium text-slate-600",children:"Test Recipient"})]}),e.jsx(B,{type:"text",value:h,placeholder:s.channel==="email"?"test@example.com":s.channel==="sms"||s.channel==="whatsapp"?"+91 98765 43210":"device-token",onChange:g=>v(g.target.value),className:"text-[12px]"}),e.jsx("p",{className:"mt-1 text-[10px] text-slate-400",children:"Enter a recipient address to send a test notification"})]}),e.jsxs("div",{className:"flex items-center justify-between pt-2 border-t border-slate-100",children:[E?e.jsx("span",{className:"text-[11px] text-amber-600 font-medium",children:"Unsaved changes"}):e.jsx("span",{}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(P,{variant:"outline",size:"sm",className:"h-7 text-[11px]",disabled:x||!h.trim(),onClick:()=>r(h.trim()),children:[x?e.jsx(ee,{className:"h-3 w-3 animate-spin"}):e.jsx(Ie,{className:"h-3 w-3"}),x?"Testing…":"Test Connection"]}),e.jsxs(P,{size:"sm",className:"h-7 text-[11px]",disabled:d||!E,onClick:()=>c(S),children:[d?e.jsx(ee,{className:"h-3 w-3 animate-spin"}):e.jsx(de,{className:"h-3 w-3"}),d?"Saving…":"Save"]})]})]})]})]})]})}function nt(){const{data:s=[],isLoading:a,isError:t,refetch:c}=zs(),r=Us(),d=Ks(),x=Bs(),b=Hs(),T=Vs(),[S,p]=f.useState(!1),[h,v]=f.useState("push"),[u,i]=f.useState(""),[o,C]=f.useState(null),[y,l]=f.useState(null),[N,k]=f.useState(null),E=s.map(m=>m.channel),w=K.filter(m=>!E.includes(m.type));function g(){var A;const m=w[0];m&&(v(m.type),i(((A=m.providers[0])==null?void 0:A.id)??"")),p(!0)}function q(){const m=K.find(I=>I.type===h);if(!m)return;const A=m.providers.find(I=>I.id===u)??m.providers[0];A&&r.mutate({channel:h,provider_code:A.id,display_name:`${m.label} — ${A.name}`,is_enabled:!1,credentials:{},config:{},priority:0},{onSuccess:()=>{O.success(`${m.label} channel added with ${A.name} provider.`),p(!1)},onError:()=>O.error("Failed to add channel.")})}function U(m,A){l(m.id),d.mutate({id:m.id,credentials:A},{onSuccess:()=>{O.success("Provider credentials saved."),l(null)},onError:()=>{O.error("Failed to save credentials."),l(null)}})}function xe(m,A){k(m.id),b.mutate({id:m.id,recipient_address:A},{onSuccess:I=>{I.success?O.success(I.message||"Test successful!"):O.error(I.message||"Test failed."),k(null)},onError:()=>{O.error("Test connection failed."),k(null)}})}function pe(m){T.mutate(m.id,{onSuccess:A=>{O.success(`${m.display_name} ${A.is_enabled?"enabled":"disabled"}.`)},onError:()=>O.error("Failed to toggle provider.")})}function he(){o&&x.mutate(o.id,{onSuccess:()=>{O.success("Channel provider removed."),C(null)},onError:()=>O.error("Failed to remove channel.")})}const re=K.find(m=>m.type===h);return a?e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(F,{className:"h-6 w-52"}),e.jsx(F,{className:"h-4 w-96"})]}),Array.from({length:3}).map((m,A)=>e.jsx(F,{variant:"rectangular",className:"h-20 rounded-xl"},A))]}):t?e.jsx(H,{icon:se,title:"Failed to load channel settings",description:"Something went wrong fetching your channel providers. Please try again.",action:e.jsx(P,{size:"sm",onClick:()=>c(),children:"Retry"})}):e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[14px] font-semibold text-slate-800",children:"Channel Integrations"}),e.jsx("p",{className:"text-[12px] text-slate-400 mt-0.5 leading-relaxed max-w-lg",children:"Configure delivery providers for each notification channel. Each channel connects to an external service (SendGrid, Firebase, Twilio, etc.) for message delivery."})]}),e.jsxs(P,{size:"sm",disabled:w.length===0,onClick:g,children:[e.jsx(le,{className:"h-3.5 w-3.5"}),"Add Channel"]})]}),e.jsx("div",{className:"space-y-3",children:s.map(m=>e.jsx(lt,{provider:m,onToggle:()=>pe(m),onRemove:()=>C(m),onSave:A=>U(m,A),onTest:A=>xe(m,A),isSaving:y===m.id,isTesting:N===m.id},m.id))}),s.length===0&&e.jsx(H,{icon:se,title:"No channels configured",description:"Add a channel to start delivering notifications via email, push, SMS, or WhatsApp.",action:e.jsxs(P,{size:"sm",onClick:g,children:[e.jsx(le,{className:"h-3.5 w-3.5"}),"Add First Channel"]})}),e.jsx(Ne,{open:!!o,onOpenChange:m=>!m&&C(null),children:e.jsxs(we,{children:[e.jsx(ke,{children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx(ts,{variant:"destructive"}),e.jsxs("div",{children:[e.jsx(Se,{children:"Remove Channel Provider"}),e.jsxs(Ce,{children:["Are you sure you want to remove"," ",e.jsx("strong",{children:o==null?void 0:o.display_name}),"? This will delete all saved credentials and configuration."]})]})]})}),e.jsxs(_e,{children:[e.jsx(Te,{children:"Cancel"}),e.jsx(Ae,{variant:"destructive",onClick:he,disabled:x.isPending,children:x.isPending?e.jsxs(e.Fragment,{children:[e.jsx(ee,{className:"h-3.5 w-3.5 animate-spin"})," Removing…"]}):"Remove"})]})]})}),e.jsx(Gs,{open:S,onOpenChange:p,children:e.jsxs(Xs,{className:"max-w-md",children:[e.jsxs($s,{children:[e.jsx(Ys,{children:"Add Notification Channel"}),e.jsx(Js,{children:"Select a channel type and the delivery provider you want to integrate."})]}),e.jsxs("div",{className:"space-y-5 py-1",children:[e.jsxs("div",{children:[e.jsx("p",{className:"mb-2 text-[11px] font-medium uppercase tracking-wide text-slate-500",children:"Channel Type"}),e.jsx("div",{className:"grid grid-cols-5 gap-2",children:w.map(m=>{const A=m.icon,I=h===m.type;return e.jsxs("button",{onClick:()=>{var ce;v(m.type),i(((ce=m.providers[0])==null?void 0:ce.id)??"")},className:_("flex flex-col items-center gap-1.5 rounded-xl border p-2.5 transition-all",I?_("border-transparent shadow-sm",m.bgColor,m.color):"border-slate-200 bg-white text-slate-500 hover:border-slate-300"),children:[e.jsx(A,{className:"h-4 w-4"}),e.jsx("span",{className:"text-[10px] font-medium leading-none",children:m.label})]},m.type)})})]}),re&&e.jsxs("div",{children:[e.jsx("p",{className:"mb-2 text-[11px] font-medium uppercase tracking-wide text-slate-500",children:"Delivery Provider"}),e.jsx("div",{className:"space-y-2",children:re.providers.map(m=>{const A=u===m.id;return e.jsxs("button",{onClick:()=>i(m.id),className:_("flex items-start gap-3 w-full rounded-xl border p-3.5 text-left transition-all",A?"border-blue-300 bg-blue-50/50 shadow-sm":"border-slate-200 bg-white hover:border-slate-300"),children:[e.jsx("div",{className:_("flex h-8 w-8 shrink-0 items-center justify-center rounded-lg mt-0.5",A?"bg-blue-100 text-blue-600":"bg-slate-100 text-slate-400"),children:e.jsx(ka,{className:"h-3.5 w-3.5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:_("text-[12px] font-semibold",A?"text-blue-900":"text-slate-700"),children:m.name}),e.jsx("p",{className:"text-[11px] text-slate-400 mt-0.5 leading-snug",children:m.description}),e.jsxs("p",{className:"text-[10px] text-slate-300 mt-1",children:[m.fields.filter(I=>I.required).length," required fields · ",m.fields.length," total"]})]}),A&&e.jsx("div",{className:"shrink-0 mt-1",children:e.jsx(de,{className:"h-4 w-4 text-blue-500"})})]},m.id)})})]})]}),e.jsxs(Qs,{children:[e.jsx(P,{variant:"outline",size:"sm",onClick:()=>p(!1),children:"Cancel"}),e.jsx(P,{size:"sm",onClick:q,disabled:!u||r.isPending,children:r.isPending?e.jsxs(e.Fragment,{children:[e.jsx(ee,{className:"h-3.5 w-3.5 animate-spin"})," Adding…"]}):e.jsxs(e.Fragment,{children:[e.jsx(le,{className:"h-3.5 w-3.5"})," Add Channel"]})})]})]})})]})}const it=s=>{var a;return((a=R.find(t=>t.id===s))==null?void 0:a.label)??s},Oe={event_key:"",name:"",description:"",role_recipients:[],users:[],channels:[],is_active:!0};function rt(){const{hasPermission:s}=ea(),a=s("notifications","notification","update"),{data:t=[],isLoading:c}=ye(),{data:r=[],isLoading:d}=sa(),x=aa(),b=ta(),T=la(),[S,p]=f.useState(!1),[h,v]=f.useState(null),[u,i]=f.useState(null),[o,C]=f.useState(Oe),[y,l]=f.useState(""),N=f.useMemo(()=>Object.fromEntries(t.map(n=>[n.key,n])),[t]),k=o.event_key?N[o.event_key]:void 0,E=f.useMemo(()=>t.filter(n=>!n.transactional),[t]),{data:w}=Sa({search:y||void 0,page_size:20}),g=((w==null?void 0:w.results)??[]).map(n=>({value:n.id,label:`${n.first_name} ${n.last_name}`.trim()||n.email,hint:n.email}));function q(){v(null),C(Oe),p(!0)}function U(n){v(n),C({event_key:n.event_key,name:n.name,description:n.description,role_recipients:n.role_recipients??[],users:n.users??[],channels:n.channels??[],is_active:n.is_active}),p(!0)}function xe(n){const j=N[n];C(L=>({...L,event_key:n,name:L.name||(j==null?void 0:j.label)||"",role_recipients:L.role_recipients.filter(ys=>j==null?void 0:j.allowed_recipients.some(vs=>vs.key===ys)),channels:L.channels.length?L.channels:(j==null?void 0:j.default_channels)??[]}))}function pe(n){C(j=>({...j,role_recipients:j.role_recipients.includes(n)?j.role_recipients.filter(L=>L!==n):[...j.role_recipients,n]}))}function he(n){C(j=>({...j,channels:j.channels.includes(n)?j.channels.filter(L=>L!==n):[...j.channels,n]}))}function re(n){C(j=>j.users.includes(n)?j:{...j,users:[...j.users,n]})}function m(n){C(j=>({...j,users:j.users.filter(L=>L!==n)}))}const A=!!o.event_key&&o.channels.length>0&&(o.role_recipients.length>0||o.users.length>0);function I(){const n={event_key:o.event_key,name:o.name,description:o.description,role_recipients:o.role_recipients,users:o.users,channels:o.channels,is_active:o.is_active};h?b.mutate({id:h.id,...n},{onSuccess:()=>p(!1)}):x.mutate(n,{onSuccess:()=>p(!1)})}function ce(){u&&T.mutate(u.id,{onSuccess:()=>i(null)})}return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("h3",{className:"text-[15px] font-semibold text-slate-900 flex items-center gap-2",children:[e.jsx(se,{className:"h-4 w-4 text-primary"})," Alert Triggers"]}),e.jsxs("p",{className:"mt-0.5 text-[13px] text-slate-400",children:["Admin routing: when an event fires, ",e.jsx("strong",{children:"who"})," gets notified and on"," ",e.jsx("strong",{children:"which channels"}),". Individual users can then mute channels for themselves under ",e.jsx("strong",{children:"Preferences"}),"."]})]}),a&&e.jsxs(P,{size:"sm",onClick:q,disabled:c,children:[e.jsx(le,{className:"mr-1.5 h-3.5 w-3.5"})," Add Trigger"]})]}),d?e.jsx("div",{className:"text-[13px] text-slate-400 py-8 text-center",children:"Loading triggers…"}):r.length===0?e.jsxs("div",{className:"rounded-lg border border-dashed border-slate-200 py-12 text-center",children:[e.jsx(se,{className:"mx-auto h-8 w-8 text-slate-300"}),e.jsx("p",{className:"mt-2 text-[14px] font-medium text-slate-600",children:"No alert triggers yet"}),e.jsxs("p",{className:"text-[13px] text-slate-400",children:["Add a trigger, or run ",e.jsx("code",{className:"text-[12px]",children:"seed_alert_triggers"})," for defaults."]})]}):e.jsx("div",{className:"space-y-2",children:r.map(n=>e.jsxs("div",{className:"flex items-center justify-between rounded-lg border border-slate-200 bg-white px-4 py-3",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-medium text-slate-900 text-[14px]",children:n.name||n.event_label}),e.jsx("code",{className:"text-[11px] text-slate-400",children:n.event_key}),!n.is_active&&e.jsx(D,{variant:"secondary",children:"Off"})]}),e.jsxs("div",{className:"mt-1 flex flex-wrap items-center gap-1.5 text-[12px] text-slate-500",children:[e.jsx(je,{className:"h-3 w-3"}),n.role_recipients.map(j=>e.jsx(D,{variant:"outline",className:"text-[11px]",children:j},j)),n.users.length>0&&e.jsxs(D,{variant:"outline",className:"text-[11px]",children:["+",n.users.length," user(s)"]}),e.jsx(Fa,{className:"ml-2 h-3 w-3"}),n.channels.map(j=>e.jsx(D,{variant:"secondary",className:"text-[11px]",children:it(j)},j))]})]}),a&&e.jsxs(Ne,{open:(u==null?void 0:u.id)===n.id,onOpenChange:j=>!j&&i(null),children:[e.jsxs(es,{children:[e.jsx(ss,{asChild:!0,children:e.jsx(P,{variant:"ghost",size:"icon-sm",children:e.jsx(xs,{className:"h-4 w-4"})})}),e.jsxs(as,{align:"end",children:[e.jsxs(ne,{onClick:()=>U(n),children:[e.jsx(ps,{className:"mr-2 h-3.5 w-3.5"})," Edit"]}),e.jsx(na,{asChild:!0,children:e.jsxs(ne,{className:"text-red-600",onClick:()=>i(n),children:[e.jsx(ve,{className:"mr-2 h-3.5 w-3.5"})," Delete"]})})]})]}),e.jsxs(we,{children:[e.jsxs(ke,{children:[e.jsx(Se,{children:"Delete trigger?"}),e.jsxs(Ce,{children:["This stops notifications for ",e.jsx("strong",{children:u==null?void 0:u.event_key})," configured here."]})]}),e.jsxs(_e,{children:[e.jsx(Te,{children:"Cancel"}),e.jsx(Ae,{variant:"destructive",onClick:ce,children:"Delete"})]})]})]})]},n.id))}),e.jsx(Xe,{open:S,onOpenChange:p,children:e.jsxs($e,{size:"lg",children:[e.jsxs(Ye,{children:[e.jsx(Je,{children:h?"Edit Alert Trigger":"Add Alert Trigger"}),e.jsx(Qe,{children:"Choose an event, who to notify, and on which channels."})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto px-6 py-5 space-y-5",children:[e.jsxs("div",{children:[e.jsx(M,{children:"Event *"}),e.jsxs(V,{value:o.event_key,onValueChange:xe,disabled:!!h,children:[e.jsx(G,{className:"mt-1.5",children:e.jsx(X,{placeholder:"Select an event"})}),e.jsx($,{children:E.map(n=>e.jsxs(W,{value:n.key,children:[n.label," ",e.jsxs("span",{className:"text-slate-400",children:["(",n.module,")"]})]},n.key))})]}),(k==null?void 0:k.description)&&e.jsx("p",{className:"mt-1 text-[12px] text-slate-400",children:k.description})]}),k&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx(M,{children:"Name"}),e.jsx(B,{className:"mt-1.5",value:o.name,onChange:n=>C(j=>({...j,name:n.target.value})),placeholder:k.label})]}),k.variables.length>0&&e.jsxs("div",{className:"rounded-md bg-slate-50 border border-slate-200 p-3",children:[e.jsx("p",{className:"text-[12px] font-medium text-slate-600 mb-1.5",children:"Variables available to this event's templates"}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:k.variables.map(n=>e.jsx("code",{title:n.description,className:"text-[11px] bg-white border border-slate-200 rounded px-1.5 py-0.5 text-slate-600",children:`{{${n.name}}}`},n.name))})]}),e.jsxs("div",{children:[e.jsx(M,{children:"Recipients (role-relative) *"}),e.jsx("div",{className:"mt-1.5 space-y-1.5",children:k.allowed_recipients.map(n=>e.jsxs("label",{className:"flex items-center gap-2 text-[13px] text-slate-700",children:[e.jsx(De,{checked:o.role_recipients.includes(n.key),onCheckedChange:()=>pe(n.key)}),n.label]},n.key))})]}),e.jsxs("div",{children:[e.jsx(M,{children:"Specific users (optional)"}),e.jsx("div",{className:"mt-1.5",children:e.jsx(Ca,{value:null,onValueChange:re,onSearch:l,options:g,placeholder:"Add a user…",searchPlaceholder:"Search users…"})}),o.users.length>0&&e.jsx("div",{className:"mt-2 flex flex-wrap gap-1.5",children:o.users.map(n=>{const j=g.find(L=>L.value===n);return e.jsxs(D,{variant:"secondary",className:"cursor-pointer",onClick:()=>m(n),children:[(j==null?void 0:j.label)??n," ✕"]},n)})})]}),e.jsxs("div",{children:[e.jsx(M,{children:"Channels *"}),e.jsx("div",{className:"mt-1.5 grid grid-cols-2 gap-1.5",children:R.map(n=>e.jsxs("label",{className:"flex items-center gap-2 text-[13px] text-slate-700",children:[e.jsx(De,{checked:o.channels.includes(n.id),onCheckedChange:()=>he(n.id)}),n.label]},n.id))}),e.jsx("p",{className:"mt-1 text-[12px] text-slate-400",children:"A channel only sends if a template exists for this event + channel."})]}),e.jsxs("div",{children:[e.jsx(M,{children:"Notes"}),e.jsx(ls,{className:"mt-1.5",rows:2,value:o.description,onChange:n=>C(j=>({...j,description:n.target.value}))})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(M,{children:"Active"}),e.jsx(ie,{checked:o.is_active,onCheckedChange:n=>C(j=>({...j,is_active:n}))})]})]})]}),e.jsxs(Ze,{children:[e.jsx(P,{variant:"outline",onClick:()=>p(!1),children:"Cancel"}),e.jsx(P,{onClick:I,disabled:!A||x.isPending||b.isPending,children:h?"Save Changes":"Create Trigger"})]})]})})]})}const We=[{value:"7d",label:"Last 7 days"},{value:"30d",label:"Last 30 days"},{value:"90d",label:"Last 90 days"}];function be({label:s,value:a,icon:t,color:c,bgColor:r,subtitle:d}){return e.jsx("div",{className:"rounded-xl border border-slate-200 bg-white p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:_("flex h-10 w-10 shrink-0 items-center justify-center rounded-xl",r,c),children:e.jsx(t,{className:"h-4.5 w-4.5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-[11px] font-medium text-slate-400 uppercase tracking-wider",children:s}),e.jsx("p",{className:"text-[22px] font-bold text-slate-800 leading-tight tabular-nums",children:a}),d&&e.jsx("p",{className:"text-[11px] text-slate-400 mt-0.5",children:d})]})]})})}function ct(){const[s,a]=f.useState("30d"),{data:t,isLoading:c,isError:r,refetch:d}=ia(s);if(c)return e.jsxs("div",{className:"space-y-5",children:[e.jsx("div",{className:"flex items-center gap-2",children:We.map(i=>e.jsx(F,{className:"h-8 w-24 rounded-lg"},i.value))}),e.jsx("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-3",children:Array.from({length:4}).map((i,o)=>e.jsx(F,{variant:"rectangular",className:"h-24 rounded-xl"},o))}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-4",children:[e.jsx(F,{variant:"rectangular",className:"h-72 rounded-xl"}),e.jsx(F,{variant:"rectangular",className:"h-72 rounded-xl"})]})]});if(r)return e.jsx(H,{icon:oe,title:"Failed to load analytics",description:"Something went wrong fetching notification analytics data.",action:e.jsx(P,{size:"sm",onClick:()=>d(),children:"Retry"})});if(!t||t.totals.sent===0)return e.jsx(H,{icon:bs,title:"No analytics data yet",description:"Analytics will appear here once notifications start being delivered."});const{totals:x,by_channel:b,by_day:T,top_events:S}=t,p=Math.round(x.delivery_rate),h=T.map(i=>({date:us(gs(i.date),"dd MMM"),sent:i.sent,delivered:i.delivered})),v=b.map(i=>{const o=R.find(C=>C.id===i.channel);return{name:(o==null?void 0:o.label)??i.channel,value:i.sent}}),u=S.slice(0,8).map(i=>({event:i.notification__type.split(".").pop()??i.notification__type,count:i.count}));return e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[14px] font-semibold text-slate-800",children:"Delivery Analytics"}),e.jsx("p",{className:"text-[12px] text-slate-400 mt-0.5",children:"Notification delivery performance and channel breakdown."})]}),e.jsx("div",{className:"flex items-center gap-1.5 shrink-0",children:We.map(i=>e.jsx("button",{onClick:()=>a(i.value),className:_("px-3 py-1.5 rounded-lg text-[12px] font-medium transition-colors",s===i.value?"bg-slate-800 text-white":"bg-slate-100 text-slate-500 hover:bg-slate-200"),children:i.label},i.value))})]}),e.jsxs("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-3",children:[e.jsx(be,{label:"Sent",value:x.sent.toLocaleString(),icon:Pa,color:"text-blue-600",bgColor:"bg-blue-50"}),e.jsx(be,{label:"Delivered",value:x.delivered.toLocaleString(),icon:de,color:"text-emerald-600",bgColor:"bg-emerald-50"}),e.jsx(be,{label:"Failed",value:x.failed.toLocaleString(),icon:Ea,color:"text-red-500",bgColor:"bg-red-50"}),e.jsx("div",{className:"rounded-xl border border-slate-200 bg-white p-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-xl bg-violet-50 text-violet-600",children:e.jsx(ds,{className:"h-4.5 w-4.5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-[11px] font-medium text-slate-400 uppercase tracking-wider",children:"Delivery Rate"}),e.jsxs("p",{className:"text-[22px] font-bold text-slate-800 leading-tight tabular-nums",children:[p,"%"]}),e.jsx(Me,{value:p,variant:p>=90?"success":p>=70?"warning":"destructive",className:"mt-1.5 h-1.5"})]})]})})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"rounded-xl border border-slate-200 bg-white p-4",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-700 mb-3",children:"Daily Volume"}),e.jsx(_a,{data:h,xKey:"date",lines:[{dataKey:"sent",label:"Sent",color:me.primary},{dataKey:"delivered",label:"Delivered",color:me.success}],height:240,showLegend:!0})]}),e.jsxs("div",{className:"rounded-xl border border-slate-200 bg-white p-4",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-700 mb-3",children:"By Channel"}),v.length>0?e.jsx(Aa,{data:v,dataKey:"value",nameKey:"name",height:240,donut:!0,centerLabel:x.sent.toLocaleString(),showLegend:!0}):e.jsx("div",{className:"flex items-center justify-center h-60 text-[12px] text-slate-400",children:"No channel data available"})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"rounded-xl border border-slate-200 bg-white p-4",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-700 mb-3",children:"Top Events"}),u.length>0?e.jsx(Ta,{data:u,xKey:"event",bars:[{dataKey:"count",label:"Count",color:me.primary}],height:240}):e.jsx("div",{className:"flex items-center justify-center h-60 text-[12px] text-slate-400",children:"No event data available"})]}),e.jsxs("div",{className:"rounded-xl border border-slate-200 bg-white p-4",children:[e.jsx("p",{className:"text-[13px] font-semibold text-slate-700 mb-3",children:"Channel Breakdown"}),e.jsxs("div",{className:"space-y-3",children:[b.map(i=>{const o=R.find(l=>l.id===i.channel),C=i.sent>0?Math.round(i.delivered/i.sent*100):0,y=o==null?void 0:o.icon;return e.jsxs("div",{className:"flex items-center gap-3",children:[y&&e.jsx("div",{className:_("flex h-8 w-8 shrink-0 items-center justify-center rounded-lg",o==null?void 0:o.bgColor,o==null?void 0:o.color),children:e.jsx(y,{className:"h-3.5 w-3.5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsx("span",{className:"text-[12px] font-medium text-slate-700",children:(o==null?void 0:o.label)??i.channel}),e.jsxs("div",{className:"flex items-center gap-2 text-[11px] text-slate-400",children:[e.jsxs("span",{children:[i.sent," sent"]}),e.jsx("span",{children:"·"}),e.jsxs("span",{children:[i.delivered," delivered"]}),i.failed>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"·"}),e.jsxs("span",{className:"text-red-500",children:[i.failed," failed"]})]})]})]}),e.jsx(Me,{value:C,variant:C>=90?"success":C>=70?"warning":"destructive",className:"h-1.5"})]}),e.jsxs(D,{variant:"secondary",className:"text-[10px] shrink-0 tabular-nums",children:[C,"%"]})]},i.channel)}),b.length===0&&e.jsx("p",{className:"text-[12px] text-slate-400 text-center py-8",children:"No data"})]})]})]})]})}const ot=[{id:"notification",accessorKey:"notification_title",header:"Notification",size:260,cell:({row:s})=>{const a=s.original,t=R.find(c=>c.id===a.channel);return e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("div",{className:"min-w-0 flex-1",children:e.jsx("p",{className:"text-[13px] font-medium text-slate-700 truncate",children:a.notification_title||"Untitled"})}),t&&e.jsx(D,{variant:"outline",className:_("text-[10px] shrink-0",t.color),children:t.label})]})}},{id:"status",accessorKey:"status",header:"Status",size:110,cell:({row:s})=>{const a=js[s.original.status];return e.jsxs(D,{variant:"secondary",className:"text-[10px]",style:{color:a.color,backgroundColor:`${a.color}10`},children:[e.jsx("span",{className:"mr-1 inline-block h-1.5 w-1.5 rounded-full",style:{backgroundColor:a.color}}),a.label]})}},{id:"recipient",accessorKey:"recipient_address",header:"Recipient",size:200,cell:({row:s})=>e.jsx("span",{className:"text-[12px] font-mono text-slate-600 truncate block",children:s.original.recipient_address})},{id:"provider",accessorKey:"provider_display",header:"Provider",size:140,cell:({row:s})=>e.jsx("span",{className:"text-[12px] text-slate-600",children:s.original.provider_display})},{id:"attempt",accessorKey:"attempt",header:"Attempt",size:80,cell:({row:s})=>e.jsxs("span",{className:"text-[12px] text-slate-500 tabular-nums",children:["#",s.original.attempt]})},{id:"sent_at",accessorKey:"sent_at",header:"Sent At",size:140,cell:({row:s})=>e.jsx("span",{className:"text-[12px] text-slate-500 tabular-nums",children:s.original.sent_at?us(gs(s.original.sent_at),"dd MMM, HH:mm"):"—"})},{id:"error",accessorKey:"error_message",header:"Error",cell:({row:s})=>{const a=s.original.error_message;return a?e.jsx(Be,{children:e.jsxs(He,{children:[e.jsx(Ve,{asChild:!0,children:e.jsx("span",{className:"text-[12px] text-red-500 truncate block max-w-[200px] cursor-default",children:a})}),e.jsx(Ge,{side:"top",className:"max-w-sm text-[11px]",children:a})]})}):e.jsx("span",{className:"text-[12px] text-slate-300",children:"—"})}}];function dt(){const[s,a]=f.useState("all"),[t,c]=f.useState("all"),[r,d]=f.useState({pageIndex:0,pageSize:25}),x={...s!=="all"&&{channel:s},...t!=="all"&&{status:t},page:r.pageIndex+1},{data:b,isLoading:T,isError:S,refetch:p}=ra(x);if(S)return e.jsx(H,{icon:oe,title:"Failed to load delivery logs",description:"Something went wrong fetching notification delivery logs.",action:e.jsx(P,{size:"sm",onClick:()=>p(),children:"Retry"})});const h=e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(V,{value:s,onValueChange:v=>{a(v),d(u=>({...u,pageIndex:0}))},children:[e.jsx(G,{className:"h-8 w-[140px] text-[12px]",children:e.jsx(X,{placeholder:"Channel"})}),e.jsxs($,{children:[e.jsx(W,{value:"all",className:"text-[12px]",children:"All Channels"}),R.map(v=>e.jsx(W,{value:v.id,className:"text-[12px]",children:v.label},v.id))]})]}),e.jsxs(V,{value:t,onValueChange:v=>{c(v),d(u=>({...u,pageIndex:0}))},children:[e.jsx(G,{className:"h-8 w-[140px] text-[12px]",children:e.jsx(X,{placeholder:"Status"})}),e.jsxs($,{children:[e.jsx(W,{value:"all",className:"text-[12px]",children:"All Statuses"}),Object.entries(js).map(([v,u])=>e.jsx(W,{value:v,className:"text-[12px]",children:e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"inline-block h-1.5 w-1.5 rounded-full",style:{backgroundColor:u.color}}),u.label]})},v))]})]})]});return e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[14px] font-semibold text-slate-800",children:"Delivery Logs"}),e.jsx("p",{className:"text-[12px] text-slate-400 mt-0.5",children:"Complete delivery history for all notification channels with status tracking and error details."})]}),e.jsx(Da,{columns:ot,data:(b==null?void 0:b.results)??[],totalCount:(b==null?void 0:b.count)??0,pagination:r,onPaginationChange:d,isLoading:T,searchPlaceholder:"Search by recipient or error...",actions:h,emptyMessage:"No delivery logs",emptyDescription:"Logs will appear here once notifications are sent.",enablePagination:!0})]})}const xt=[];function tl(){const{data:s=xt,isLoading:a,isError:t,refetch:c}=ca(),{data:r=[],isLoading:d}=ye(),x=oa(),b=f.useMemo(()=>Oa(r),[r]),[T,S]=f.useState({}),[p,h]=f.useState(!1),[v,u]=f.useState(null);f.useEffect(()=>{if(r.length===0)return;const l=qe(r);for(const N of s){const k=l[N.event_code]??te();l[N.event_code]={...k,[N.channel]:N.is_enabled}}S(l),h(!1)},[s,r]);const i=f.useCallback((l,N)=>{S(k=>{const E=k[l]??te();return{...k,[l]:{...E,[N]:!E[N]}}}),h(!0)},[]),o=f.useCallback((l,N)=>{S(k=>{const E={...k};for(const w of b)for(const g of w.events)g.transactional||(E[g.code]={...E[g.code]??te(),[l]:N});return E}),h(!0)},[b]),C=()=>{const l=[];for(const[N,k]of Object.entries(T))for(const[E,w]of Object.entries(k))l.push({event_code:N,channel:E,is_enabled:w});x.mutate(l,{onSuccess:()=>{h(!1),u(new Date)}})},y=()=>{const l=qe(r);for(const N of s){const k=l[N.event_code]??te();l[N.event_code]={...k,[N.channel]:N.is_enabled}}S(l),h(!1)};return e.jsxs("div",{className:"space-y-5",children:[e.jsx(ha,{title:"Notification Preferences",description:"Control how, when, and where you receive notifications across all HCM modules."}),e.jsxs(da,{defaultValue:"preferences",className:"space-y-0",children:[e.jsxs(xa,{children:[e.jsxs(Y,{value:"preferences",children:[e.jsx(Z,{className:"h-3.5 w-3.5"}),"Preferences"]}),e.jsxs(Y,{value:"templates",children:[e.jsx(fe,{className:"h-3.5 w-3.5"}),"Template Gallery"]}),e.jsxs(Y,{value:"channel-settings",children:[e.jsx(ms,{className:"h-3.5 w-3.5"}),"Channel Settings"]}),e.jsxs(Y,{value:"triggers",children:[e.jsx(se,{className:"h-3.5 w-3.5"}),"Alert Triggers"]}),e.jsxs(Y,{value:"analytics",children:[e.jsx(bs,{className:"h-3.5 w-3.5"}),"Analytics"]}),e.jsxs(Y,{value:"delivery-logs",children:[e.jsx(pa,{className:"h-3.5 w-3.5"}),"Delivery Logs"]})]}),e.jsx(J,{value:"preferences",className:"space-y-5",children:e.jsx(Ga,{categories:b,prefs:T,dirty:p,savedAt:v,isSaving:x.isPending,isLoading:a||d,isError:t,onToggle:i,onToggleAll:o,onSave:C,onDiscard:y,onRetry:()=>c()})}),e.jsx(J,{value:"templates",children:e.jsx(at,{})}),e.jsx(J,{value:"channel-settings",children:e.jsx(nt,{})}),e.jsx(J,{value:"triggers",children:e.jsx(rt,{})}),e.jsx(J,{value:"analytics",children:e.jsx(ct,{})}),e.jsx(J,{value:"delivery-logs",children:e.jsx(dt,{})})]})]})}export{tl as NotificationPreferencesPage};
