import { MiniGamePlatform } from '../platform/MiniGamePlatform'; export class InterstitialAd { private static _keys: string[] = []; static init(keys: string[]) { this._keys = keys ?? []; MiniGamePlatform.init(); } static showInterstitialAd(onAdClose?: Function, onFail?: Function, onSuccess?: Function) { MiniGamePlatform.init(); const pf = MiniGamePlatform.pf; if (!pf?.createInterstitialAd) { onAdClose?.(true); return; } if (this._keys.length === 0) { onAdClose?.(false); return; } const keyIndex = Math.floor(Math.random() * this._keys.length); const interstitialAd = pf.createInterstitialAd({ adUnitId: this._keys[keyIndex], }); if (!interstitialAd) { onFail?.(); return; } interstitialAd.onClose?.(() => { console.log('[InterstitialAd] onClose'); try { interstitialAd.destroy?.(); } catch { /* ignore */ } onAdClose?.(); }); const loadResult = interstitialAd.load?.(); const afterLoad = () => { const showResult = interstitialAd.show?.(); if (!showResult?.then) return; showResult.then(() => { console.log('[InterstitialAd] show ok'); onSuccess?.(); }).catch((err: unknown) => { console.log('[InterstitialAd] show fail', err); onFail?.(); }); }; if (loadResult?.then) { loadResult.then(afterLoad).catch((err: unknown) => { console.log('[InterstitialAd] load fail', err); onFail?.(); }); } else { afterLoad(); } } }