import { PlatformAdapter } from './base.js';
export class AppLovinAdapter extends PlatformAdapter {
    getName() {
        return 'AppLovin';
    }
    getSizeLimit() {
        // AppLovin: 5MB
        return 5 * 1024 * 1024;
    }
    getDefaultFormat() {
        return 'html';
    }
    async modifyHTML(html, assets) {
        // AppLovin 需要 MRAID v2.0 支持
        const appLovinScriptCode = `
      window.mraid = window.mraid || {
        getVersion: function() { return '2.0'; },
        isReady: function() { return true; },
        open: function(url) { 
          console.log('AppLovin CTA: opening store');
          if (url) { window.open(url, '_blank'); return; }
          var ua = navigator.userAgent || '';
          var isIOS = /iPad|iPhone|iPod/.test(ua) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
          var _s = window.__PLAYCRAFT_STORE_URLS__ || {};
          window.open(isIOS ? (_s.ios || '') : (_s.android || ''), '_blank');
        },
        close: function() { 
          console.warn('AppLovin: close() disabled'); 
        },
        addEventListener: function(event, listener) {
          if (event === 'ready') setTimeout(listener, 0);
        },
        removeEventListener: function(event, listener) {},
        getState: function() { return 'default'; },
        getPlacementType: function() { return 'interstitial'; },
        isViewable: function() { return true; },
        getMaxSize: function() { return { width: window.innerWidth, height: window.innerHeight }; },
        getCurrentPosition: function() { return { x: 0, y: 0, width: window.innerWidth, height: window.innerHeight }; },
        getScreenSize: function() { return { width: screen.width, height: screen.height }; }
      };
    `;
        const appLovinScript = await this.minifyPlatformScript(appLovinScriptCode, 'applovin-mraid');
        const comment = `<!-- AppLovin: 必须支持横竖屏, 禁止退出按钮, CTA 用 mraid.open() -->`;
        // 在 </head> 之前插入平台特定的 API
        html = html.replace('</head>', `${appLovinScript}${comment}</head>`);
        // 注入统一的 CTA 适配器
        html = await this.injectCTAAdapterAsync(html);
        html = await this.injectTrackingAdapterAsync(html);
        return html;
    }
    getPlatformScript() {
        return `
      // AppLovin Playable Ad
      // 使用 mraid.open() 跳转应用商店
      // 禁止自定义退出按钮
      
      if (typeof mraid !== 'undefined') {
        mraid.addEventListener('ready', function() {
          console.log('MRAID ready');
        });
      }
    `;
    }
    validateOptions() {
        if (this.options.format && this.options.format !== 'html') {
            console.warn('警告: AppLovin 使用单文件 HTML 格式');
        }
    }
}
