// apps/desktop/src/main/ipc/ads-handlers.ts // IPC handlers for Multi-Ad Mediation, Header Bidding, and Revenue Settlement import { ipcMain } from 'electron' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { ok, err } from '@d3ro/core/errors' import type { AdMediationConfig, AdMediationAuctionRequest, AdImpressionEvent, PublisherAccountConfig, } from '@d3ro/core/types' import { getAdMediationEngine } from '../services/ads/AdMediationEngine' import { getAdSettlementService } from '../services/ads/AdSettlementService' export function registerAdsHandlers(): void { const engine = getAdMediationEngine() const settlement = getAdSettlementService() // 1. Get Mediation Config ipcMain.handle(IPC_CHANNELS.ADS.GET_CONFIG, async () => { return ok(engine.getConfig()) }) // 2. Set Mediation Config ipcMain.handle(IPC_CHANNELS.ADS.SET_CONFIG, async (_event, config: Partial) => { return ok(engine.setConfig(config)) }) // 3. Request Header Bidding Auction ipcMain.handle(IPC_CHANNELS.ADS.REQUEST_AUCTION, async (_event, request: AdMediationAuctionRequest) => { try { const result = await engine.runAuction(request) return ok(result) } catch (e) { return err('AD_AUCTION_FAILED', e instanceof Error ? e.message : String(e)) } }) // 4. Record Impression ipcMain.handle(IPC_CHANNELS.ADS.RECORD_IMPRESSION, async (_event, eventPayload: Omit) => { engine.recordImpression(eventPayload) return ok(true) }) // 5. Record Click ipcMain.handle(IPC_CHANNELS.ADS.RECORD_CLICK, async (_event, { adId, networkId }: { adId: string; networkId: string }) => { engine.recordClick(adId, networkId) return ok(true) }) // 6. Claim Rewarded Video Quota ipcMain.handle(IPC_CHANNELS.ADS.CLAIM_REWARD, async (_event, { adId, networkId }: { adId: string; networkId: string }) => { const result = await engine.claimReward(adId, networkId) return ok(result) }) // 7. Get Revenue & Settlement Stats ipcMain.handle(IPC_CHANNELS.ADS.GET_REVENUE_STATS, async (_event, { period }: { period?: string } = {}) => { return ok(engine.getRevenueStats(period)) }) // 8. Get Settlement Records ipcMain.handle(IPC_CHANNELS.ADS.GET_SETTLEMENTS, async () => { const stats = engine.getRevenueStats() return ok(stats.settlements) }) // 9. Request Payout ipcMain.handle(IPC_CHANNELS.ADS.REQUEST_PAYOUT, async (_event, { settlementId }: { settlementId: string }) => { const result = settlement.requestPayout(settlementId) return ok(result) }) // 10. Get/Set Publisher Account ipcMain.handle(IPC_CHANNELS.ADS.GET_PUBLISHER_ACCOUNT, async () => { return ok(settlement.getPublisherAccount()) }) ipcMain.handle(IPC_CHANNELS.ADS.SET_PUBLISHER_ACCOUNT, async (_event, account: Partial) => { return ok(settlement.setPublisherAccount(account)) }) }