"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.InAppChannel=void 0;const db_1=require("@b/db"),BaseChannel_1=require("./BaseChannel"),Websocket_1=require("@b/handler/Websocket");class InAppChannel extends BaseChannel_1.BaseChannel{constructor(){super("IN_APP")}async send(e,t){try{this.validate(e);const{userId:i,type:a,data:n}=e,s=(null==n?void 0:n.title)||"Notification",r=(null==n?void 0:n.message)||"",o=(null==n?void 0:n.link)||null,d=(null==n?void 0:n.relatedId)||null,l=(null==n?void 0:n.actions)||null,c=await db_1.models.notification.create({userId:i,type:a,title:s,message:r,link:o,relatedId:d,actions:l?JSON.stringify(l):null,read:!1,idempotencyKey:e.idempotencyKey,channels:JSON.stringify(["IN_APP"]),priority:e.priority||"NORMAL",details:e.metadata?JSON.stringify(e.metadata):null},t?{transaction:t}:void 0);this.log("Notification created in database",{id:c.id,userId:i,type:a});try{await this.sendViaWebSocket(i,{id:c.id,type:c.type,title:s,message:r,link:o,actions:l,createdAt:c.createdAt});this.log("Notification sent via WebSocket",{id:c.id,userId:i})}catch(e){this.logError("WebSocket delivery failed",e)}await this.trackDelivery(c.id,{status:"DELIVERED",messageId:`in-app-${c.id}`});return{success:!0,externalId:c.id,messageId:`in-app-${c.id}`}}catch(e){this.logError("Failed to send in-app notification",e);return{success:!1,error:e.message}}}async sendViaWebSocket(e,t){try{Websocket_1.messageBroker.sendToClientOnRoute(e,"notification",{...t,timestamp:(new Date).toISOString()})}catch(e){throw e}}validate(e){var t,i;super.validate(e);if(!(null===(t=e.data)||void 0===t?void 0:t.message)&&!(null===(i=e.data)||void 0===i?void 0:i.title))throw new Error("In-app notification requires at least title or message")}}exports.InAppChannel=InAppChannel;