function e(e=null){return(e?.byDay||[]).map(e=>({date:e.date||e.day,total:Number(e.total||0),accepted:Number(e.queued||0),failed:Number(e.failed||0),recipients:Number(e.recipients||0)}))}function t(e=null){return[...e?.byDomain||[]].sort((e,t)=>Number(t.total||0)-Number(e.total||0)).map(e=>({domain:e.domain||`unknown`,total:Number(e.total||0),accepted:Number(e.queued||0),failed:Number(e.failed||0),recipients:Number(e.recipients||0)}))}function n(e=null){let t=e?.engagement||{};return{trackedDelivered:Number(t.trackedDelivered||0),totalOpens:Number(t.totalOpens||0),uniqueOpens:Number(t.uniqueOpens||0),proxyOpens:Number(t.proxyOpens||0),totalClicks:Number(t.totalClicks||0),uniqueClicks:Number(t.uniqueClicks||0),scannerEvents:Number(t.scannerEvents||0),openRate:Number(t.openRate||0),clickRate:Number(t.clickRate||0),clickToOpenRate:Number(t.clickToOpenRate||0)}}function r(e=null){return(e?.engagementByDay||[]).map(e=>({date:e.day||e.date,opens:Number(e.opens||0),uniqueOpens:Number(e.uniqueOpens||0),clicks:Number(e.clicks||0),uniqueClicks:Number(e.uniqueClicks||0),scannerEvents:Number(e.scannerEvents||0)}))}function i(e=null){return(e?.topLinks||[]).map(e=>({fingerprint:e.fingerprint||``,target:e.target||e.targetOrigin||``,targetOrigin:e.targetOrigin||``,clicks:Number(e.clicks||0),uniqueClicks:Number(e.uniqueClicks||0),lastClickedAt:e.lastClickedAt||``}))}function a(e=null){if(!e)return[];let t=[];e.createdAt&&t.push({stage:`submitted`,at:e.createdAt,tone:`info`,status:e.status}),e.queueId&&t.push({stage:`accepted`,at:e.createdAt||``,tone:`info`,status:`queued`,queueId:e.queueId});let n=Array.isArray(e.deliveryAttempts)?e.deliveryAttempts:[];for(let e of n){let n=o(e.status);t.push({stage:n,at:e.at||``,tone:s(n),status:e.status,queueId:e.queueId,recipient:e.recipient,relay:e.relay,response:e.response})}!n.length&&e.deliveredAt&&t.push({stage:`delivered`,at:e.deliveredAt,tone:`success`,status:`sent`,queueId:e.queueId});let r=Array.isArray(e.tracking?.events)?e.tracking.events:[];for(let e of r){let n=e.eventType===`click`?`clicked`:`opened`;t.push({stage:n,at:e.occurredAt||``,tone:e.source===`scanner`?`neutral`:n===`clicked`?`success`:`info`,status:e.source,source:e.source,targetOrigin:e.targetOrigin||``,trackingLinkId:e.trackingLinkId??null})}let i=Array.isArray(e.webhookDeliveries)?e.webhookDeliveries:[];for(let e of i)t.push({stage:`webhook`,at:e.lastAttemptAt||e.createdAt||``,tone:c(e.status),status:e.status,webhookId:e.webhookId,responseStatus:e.responseStatus});let a={submitted:0,accepted:1,delivered:2,pending:2,failed:2,opened:3,clicked:4,webhook:5};return t.map((e,t)=>({item:e,index:t,time:Date.parse(e.at)})).sort((e,t)=>{let n=Number.isNaN(e.time)?1/0:e.time,r=Number.isNaN(t.time)?1/0:t.time;return n===r?(a[e.item.stage]??99)-(a[t.item.stage]??99)||e.index-t.index:n-r}).map(({item:e})=>e)}function o(e){return e===`sent`?`delivered`:e===`deferred`?`pending`:e===`bounced`||e===`failed`?`failed`:`pending`}function s(e){return e===`delivered`?`success`:e===`pending`?`warning`:e===`failed`?`error`:e===`submitted`||e===`accepted`?`info`:`neutral`}function c(e){return e===`success`?`success`:e===`dead`?`error`:e===`pending`||e===`processing`?`warning`:`neutral`}export{i as a,a as i,n,e as o,r,t};