import { PlatformAdapter } from './base.js';
export class IronSourceAdapter extends PlatformAdapter {
    getName() {
        return 'ironSource';
    }
    getSizeLimit() {
        // ironSource: 5MB
        return 5 * 1024 * 1024;
    }
    getDefaultFormat() {
        return 'html';
    }
    async modifyHTML(html, assets) {
        // ironSource 需要 MRAID 和 dapi API
        const ironSourceScriptCode = `
      window.mraid = window.mraid || {
        getVersion: function() { return '2.0'; },
        isReady: function() { return true; },
        open: function(url) {
          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.log('ironSource: close() handled by platform'); 
        },
        useCustomClose: function(useCustomClose) {
          console.warn('ironSource: useCustomClose() not allowed');
        },
        addEventListener: function(event, listener) {},
        removeEventListener: function(event, listener) {},
        getState: function() { return 'default'; },
        getPlacementType: function() { return 'interstitial'; },
        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 }; }
      };
      window.dapi = window.dapi || {
        isReady: function() { return true; },
        addEventListener: function(event, callback) {
          if (event === 'ready') setTimeout(callback, 0);
        },
        removeEventListener: function(event, callback) {}
      };
    `;
        const ironSourceScript = await this.minifyPlatformScript(ironSourceScriptCode, 'ironsource-mraid-dapi');
        const comment = `<!-- ironSource: 预留四角30x30px给平台UI -->`;
        // 在 </head> 之前插入平台特定的 API
        html = html.replace('</head>', `${ironSourceScript}${comment}</head>`);
        // 注入统一的 CTA 适配器
        html = await this.injectCTAAdapterAsync(html);
        html = await this.injectTrackingAdapterAsync(html);
        return html;
    }
    getPlatformScript() {
        return `
      // ironSource Playable Ad
      // 使用 mraid.open() 跳转应用商店
      // 禁止使用 close() 或 useCustomClose()
      
      if (typeof dapi !== 'undefined') {
        dapi.addEventListener('ready', function() {
          console.log('dapi ready');
        });
      }
    `;
    }
    validateOptions() {
        if (this.options.format && this.options.format !== 'html') {
            console.warn('警告: ironSource 使用单文件 HTML 格式');
        }
    }
}
