import { Linking } from 'react-native'; import { UserService } from "./services/UserService"; import { BalanceService } from "./services/BalanceService" import type { RewardDTO } from "./domain/RewardDTO"; import { MCGenderEnum } from "./domain/MCGenderEnum"; // NATIVE CAMPAIGNS: ADDED import type { MCCampaign, MCCampaignResponse } from '../nativesdk/domain'; import { CampaignService } from '../nativesdk/services/CampaignService'; import { ImpressionService } from '../nativesdk/services/ImpressionService'; export class MCOfferwallSDK { private static _apikey: any; private static _userService: UserService; // matching the Android/iOS service lifetime between campaign requests. private static readonly _campaignService = new CampaignService(); //Session-level title used by Native Campaign // in-app details, matching SetToolbarTitle/setToolbarTitle on native SDKs. private static _toolbarTitle: string | null = null; static init(apikey: string): void { this._apikey=apikey; this._userService = new UserService(); } static getApikey(): string { return this._apikey; } static async setUserId(uid: string): Promise { await this._userService.setId(uid) } static async getUserId(): Promise { return await this._userService.getOrCreateId() } static async setGaid(gaid: string): Promise { await this._userService.setGaid(gaid) } static async getGaid(): Promise { return await this._userService.getGaid() } static async setIdfa(idfa: string): Promise { await this._userService.setIdfa(idfa) } static async getIdfa(): Promise { return await this._userService.getIdfa() } static async setGender(gender: MCGenderEnum): Promise { await this._userService.setGender(gender) } static async setAge(age: number): Promise { await this._userService.setAge(age) } static async setDarkMode(enabled: boolean | null): Promise { await this._userService.setDarkMode(enabled); } static async getDarkMode(): Promise { return await this._userService.getDarkMode(); } static async setAffSub1(sub1: string): Promise { await this._userService.setAffSub1(sub1) } static async setAffSub2(sub2: string): Promise { await this._userService.setAffSub2(sub2) } static async setAffSub3(sub3: string): Promise { await this._userService.setAffSub3(sub3) } static async setAffSub4(sub4: string): Promise { await this._userService.setAffSub4(sub4) } static async setAffSub5(sub5: string): Promise { await this._userService.setAffSub5(sub5) } static async getAffSub1(): Promise { return await this._userService.getAffSub1() } static async getAffSub2(): Promise { return await this._userService.getAffSub2() } static async getAffSub3(): Promise { return await this._userService.getAffSub3() } static async getAffSub4(): Promise { return await this._userService.getAffSub4() } static async getAffSub5(): Promise { return await this._userService.getAffSub5() } static async setConfig(key: string, value: string | null): Promise { await this._userService.setConfig(key, value); } static async getConfig(key: string): Promise { return await this._userService.getConfig(key); } static async getAllConfig(): Promise> { return await this._userService.getAllConfig(); } // NATIVE CAMPAIGNS: ADDED - API parity with Android/iOS native SDKs. static async setAdunitId(adunitId: string): Promise { await this._userService.setAdunitId(adunitId); this._campaignService.clearCache(); } static async getAdunitId(): Promise { return this._userService.getAdunitId(); } static async setLimit(limit: number): Promise { await this._userService.setLimit(limit); this._campaignService.clearCache(); } static async getLimit(): Promise { return this._userService.getLimit(); } static async setOpenInApp(enabled: boolean): Promise { await this._userService.setOpenInApp(enabled); } static async getOpenInApp(): Promise { return this._userService.getOpenInApp(); } static setToolbarTitle(title: string | null): void { this._toolbarTitle = title; } static getToolbarTitle(): string | null { return this._toolbarTitle; } static async getCampaigns(useCache = false): Promise { return this._campaignService.getCampaigns(useCache); } static clearCampaignCache(): void { this._campaignService.clearCache(); } static trackImpression(campaign: MCCampaign | null | undefined): void { ImpressionService.track(campaign?.links.trackingPixelUrl); } //Public default click action, equivalent to // Android OnClick(campaign) / iOS onClick(campaign). A mounted // MCNativeAdView supplies openInApp so it can present its WebView modal. static async onClick( campaign: MCCampaign | null | undefined, openInApp?: (url: string) => void ): Promise { const url = campaign?.links.detailUrl; if (!url) return; if ((await this.getOpenInApp()) && openInApp) { openInApp(url); return; } await Linking.openURL(url); } static async GetReward(adunit_id: string): Promise { const balanceService = new BalanceService(); const uid = await this.getUserId(); try { const reward = await balanceService.getBalance(uid, adunit_id); if (reward) { console.log('Reward received:', reward); return reward; } else { console.log('No reward received'); return null; } } catch (error) { console.error('Error fetching balance:', error); throw error; } } }