package com.webengage.we_notificationinbox_rn import com.facebook.react.bridge.Callback import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import com.webengage.sdk.android.Logger import com.webengage.inbox.react.NativeWEInboxModuleSpec class WEInboxModule(reactContext: ReactApplicationContext) : NativeWEInboxModuleSpec(reactContext) { private val weInboxModuleImpl: WEInboxModuleImpl? = try { WEInboxModuleImpl(reactContext) } catch (e: Exception) { Logger.e("WEInboxModule", "Failed to initialize WEInboxModuleImpl", e) null } init { Logger.d("WebEngage", "newArch: WEInboxModule (TurboModule) initialized") } private fun safeExecute(action: (WEInboxModuleImpl) -> Unit) { try { weInboxModuleImpl?.let(action) ?: Logger.e("WEInboxModule", "Module not initialized") } catch (e: Exception) { Logger.e("WEInboxModule", "Error executing action: ${e.message}", e) } } override fun getName(): String { return "WEInboxReact" } override fun initWENotificationInbox() { safeExecute { it.initWENotificationInbox() } } override fun getNotificationList(offset: String?, callback: Callback?) { if (callback == null) { Logger.e("WEInboxModule", "Callback is null in getNotificationList") return } Logger.d("WebEngage", "newArch: getNotificationList called via TurboModule") safeExecute { it.getNotificationList(offset, callback) } } override fun getNotificationCount(callback: Callback?) { if (callback == null) { Logger.e("WEInboxModule", "Callback is null in getNotificationCount") return } safeExecute { it.getNotificationCount(callback) } } override fun markRead(notificationItem: ReadableMap?) { if (notificationItem == null) { Logger.w("WEInboxModule", "Notification item is null in markRead") return } safeExecute { it.markRead(notificationItem) } } override fun markUnread(notificationItem: ReadableMap?) { if (notificationItem == null) { Logger.w("WEInboxModule", "Notification item is null in markUnread") return } safeExecute { it.markUnread(notificationItem) } } override fun trackClick(notificationItem: ReadableMap?) { if (notificationItem == null) { Logger.w("WEInboxModule", "Notification item is null in trackClick") return } safeExecute { it.trackClick(notificationItem) } } override fun trackView(notificationItem: ReadableMap?) { if (notificationItem == null) { Logger.w("WEInboxModule", "Notification item is null in trackView") return } safeExecute { it.trackView(notificationItem) } } override fun markDelete(notificationItem: ReadableMap?) { if (notificationItem == null) { Logger.w("WEInboxModule", "Notification item is null in markDelete") return } safeExecute { it.markDelete(notificationItem) } } override fun readAll(notificationList: ReadableArray?) { if (notificationList == null) { Logger.w("WEInboxModule", "Notification list is null in readAll") return } safeExecute { it.readAll(notificationList) } } override fun unReadAll(notificationList: ReadableArray?) { if (notificationList == null) { Logger.w("WEInboxModule", "Notification list is null in unReadAll") return } safeExecute { it.unReadAll(notificationList) } } override fun deleteAll(notificationList: ReadableArray?) { if (notificationList == null) { Logger.w("WEInboxModule", "Notification list is null in deleteAll") return } safeExecute { it.deleteAll(notificationList) } } override fun onNotificationIconClick() { safeExecute { it.onNotificationIconClick() } } }