import { Logger } from './logger.js';
export class CaptchaSolver {
    static async solve(provider, apiKey, siteKey, url) {
        if (provider === '2captcha') {
            return await this.solve2Captcha(apiKey, siteKey, url);
        }
        else {
            return await this.solveAntiCaptcha(apiKey, siteKey, url);
        }
    }
    static async solve2Captcha(apiKey, siteKey, url) {
        try {
            // 1. Create Task
            const createTaskRes = await fetch(`https://2captcha.com/in.php?key=${apiKey}&method=userrecaptcha&googlekey=${siteKey}&pageurl=${url}&json=1`);
            const createData = await createTaskRes.json();
            if (createData.status !== 1) {
                throw new Error(`2Captcha Task Creation Failed: ${createData.request}`);
            }
            const taskId = createData.request;
            Logger.log(`[2Captcha] Task created: ${taskId}. Waiting for solution...`);
            // 2. Poll for Result
            for (let i = 0; i < 40; i++) { // Max 2 mins
                await new Promise(r => setTimeout(r, 5000));
                const res = await fetch(`https://2captcha.com/res.php?key=${apiKey}&action=get&id=${taskId}&json=1`);
                const data = await res.json();
                if (data.status === 1) {
                    Logger.log('[2Captcha] Solution received!');
                    return data.request;
                }
                if (data.request !== 'CAPCHA_NOT_READY') {
                    throw new Error(`2Captcha Polling Failed: ${data.request}`);
                }
            }
            throw new Error('2Captcha Timeout');
        }
        catch (err) {
            Logger.error('[2Captcha] Error', err);
            throw err;
        }
    }
    static async solveAntiCaptcha(apiKey, siteKey, url) {
        try {
            // 1. Create Task
            const createTaskRes = await fetch('https://api.anti-captcha.com/createTask', {
                method: 'POST',
                body: JSON.stringify({
                    clientKey: apiKey,
                    task: {
                        type: 'NoCaptchaTaskProxyless',
                        websiteURL: url,
                        websiteKey: siteKey
                    }
                })
            });
            const createData = await createTaskRes.json();
            if (createData.errorId !== 0) {
                throw new Error(`Anti-Captcha Task Creation Failed: ${createData.errorDescription}`);
            }
            const taskId = createData.taskId;
            Logger.log(`[Anti-Captcha] Task created: ${taskId}. Waiting for solution...`);
            // 2. Poll for Result
            for (let i = 0; i < 40; i++) {
                await new Promise(r => setTimeout(r, 5000));
                const res = await fetch('https://api.anti-captcha.com/getTaskResult', {
                    method: 'POST',
                    body: JSON.stringify({
                        clientKey: apiKey,
                        taskId: taskId
                    })
                });
                const data = await res.json();
                if (data.status === 'ready') {
                    Logger.log('[Anti-Captcha] Solution received!');
                    return data.solution.gRecaptchaResponse;
                }
                if (data.errorId !== 0) {
                    throw new Error(`Anti-Captcha Polling Failed: ${data.errorDescription}`);
                }
            }
            throw new Error('Anti-Captcha Timeout');
        }
        catch (err) {
            Logger.error('[Anti-Captcha] Error', err);
            throw err;
        }
    }
}
