import { PlatformAdapter } from './base.js';
export class MintegralAdapter extends PlatformAdapter {
    getName() {
        return 'Mintegral';
    }
    getSizeLimit() {
        // Mintegral: 5MB (ZIP)
        return 5 * 1024 * 1024;
    }
    getDefaultFormat() {
        return 'zip';
    }
    async modifyHTML(html, assets) {
        // Mintegral 要求 charset 为 utf-8，确保存在
        if (!html.includes('charset') && html.includes('<head>')) {
            html = html.replace('<head>', '<head>\n<meta charset="utf-8">');
        }
        // Mintegral SDK polyfill（仅本地浏览器测试用的兜底）
        // 真实 Mintegral 环境中 SDK 会注入 window.install 等函数，这些 polyfill 不会生效
        const mintegralScriptCode = `
      window.install = window.install || function() {
        var ua = navigator.userAgent || '';
        var isIOS = /iPad|iPhone|iPod/.test(ua) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
        var _s = (typeof window !== 'undefined' && window.__PLAYCRAFT_STORE_URLS__) || {};
        var url = isIOS ? (_s.ios || '') : (_s.android || '');
        if (url) window.open(url, '_blank');
      };
      window.gameReady = window.gameReady || function() {};
      window.gameEnd = window.gameEnd || function() {};
      window.gameRetry = window.gameRetry || function() {};
      if (typeof window.gameStart !== 'function') { window.gameStart = function() {}; }
      if (typeof window.gameClose !== 'function') { window.gameClose = function() {}; }
      window.HttpAPI = window.HttpAPI || { sendPoint: function() {} };
    `;
        const mintegralScript = await this.minifyPlatformScript(mintegralScriptCode, 'mintegral-sdk');
        const comment = `<!-- Mintegral: ZIP格式, 5MB限制, 使用window.install()跳转, 支持横竖屏 -->`;
        // 在 </head> 之前插入 polyfill 和注释，如果没有 </head> 则在文件开头插入
        if (html.includes('</head>')) {
            html = html.replace('</head>', `${mintegralScript}${comment}\n</head>`);
        }
        else {
            html = `${mintegralScript}${comment}\n${html}`;
        }
        // 注入统一的 CTA 适配器
        html = await this.injectCTAAdapterAsync(html);
        html = await this.injectTrackingAdapterAsync(html);
        return html;
    }
    getPlatformScript() {
        return `
      // Mintegral Playable Ad
      // CTA: window.install && window.install();
      // Lifecycle: gameReady -> gameStart -> gameEnd/gameRetry -> gameClose
      
      // 资源加载完成后通知 SDK
      if (typeof window.gameReady === 'function') {
        window.gameReady();
      }
    `;
    }
    validateOptions() {
        if (this.options.format && this.options.format !== 'zip') {
            console.warn('警告: Mintegral 要求 ZIP 格式，将自动使用 ZIP');
        }
    }
}
