import { PlatformAdapter } from './base.js';
export class UnityAdapter extends PlatformAdapter {
    getName() {
        return 'Unity Ads';
    }
    getSizeLimit() {
        // Unity Ads: 5MB
        return 5 * 1024 * 1024;
    }
    getDefaultFormat() {
        return 'html';
    }
    async modifyHTML(html, assets) {
        // Unity Ads 需要 MRAID 3.0 支持
        const unityScriptCode = `
      window.mraid = window.mraid || {
        getVersion: function() { return '3.0'; },
        isReady: function() { return true; },
        open: function(url) { 
          console.log('Unity Ads 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);
          window.open(isIOS ? 'https://apps.apple.com/us/app/8-ball-pool/id543186831' : 'https://play.google.com/store/apps/details?id=com.miniclip.eightballpool', '_blank');
        },
        close: function() { window.close(); },
        addEventListener: function(event, listener) {
          if (event === 'ready' || event === 'viewableChange') {
            setTimeout(function() { listener({ viewable: true }); }, 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 unityScript = await this.minifyPlatformScript(unityScriptCode, 'unity-mraid');
        const comment = `<!-- Unity Ads: MRAID 3.0, 等待viewableChange, 使用mraid.open() -->`;
        // 在 </head> 之前插入
        html = html.replace('</head>', `${unityScript}${comment}</head>`);
        // 注入统一的 CTA 适配器
        html = await this.injectCTAAdapterAsync(html);
        html = await this.injectTrackingAdapterAsync(html);
        return html;
    }
    getPlatformScript() {
        return `
      // Unity Ads Playable
      // 等待 viewableChange 事件
      
      if (typeof mraid !== 'undefined') {
        mraid.addEventListener('viewableChange', function(viewable) {
          if (viewable) {
            console.log('Unity Ads: playable is viewable, starting game');
            // 这里启动游戏内容
          }
        });
      }
    `;
    }
    validateOptions() {
        if (this.options.format && this.options.format !== 'html') {
            console.warn('警告: Unity Ads 使用单文件 HTML 格式');
        }
    }
}
