"use strict";
// import axios from 'axios';
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSubmission = createSubmission;
// const JUDGE0_API_BASE = 'https://judge0-ce.p.rapidapi.com';
// const RAPIDAPI_HOST = 'judge0-ce.p.rapidapi.com';
// // You must set your RapidAPI key in the environment variable JUDGE0_API_KEY
// const RAPIDAPI_KEY = process.env.JUDGE0_API_KEY;
// if (!RAPIDAPI_KEY) {
//   throw new Error('JUDGE0_API_KEY environment variable not set');
// }
// const axiosInstance = axios.create({
//   baseURL: JUDGE0_API_BASE,
//   headers: {
//     'X-RapidAPI-Host': RAPIDAPI_HOST,
//     'X-RapidAPI-Key': RAPIDAPI_KEY,
//     'Content-Type': 'application/json',
//   },
// });
// // Create a submission
// export async function createSubmission({ languageId, sourceCodeBase64, wait = true }: {
//   languageId: number;
//   sourceCodeBase64: string;
//   wait?: boolean;
// }) {
//   const payload = {
//     language_id: languageId,
//     source_code: sourceCodeBase64,
//   };
//   try {
//     const response = await axiosInstance.post(`/submissions?base64_encoded=true&wait=${wait}`, payload);
//     if (wait) {
//       // Immediate response with result
//       const status = response.data.status?.description || 'Unknown';
//       console.log('Submission status:', status);
//       return response.data;
//     } else {
//       // Get token and poll for result
//       const token = response.data.token;
//       if (!token) throw new Error('No token received from Judge0');
//       console.log('Submission token:', token);
//       // Poll for result
//       let result;
//       for (let i = 0; i < 10; i++) { // up to 10 attempts
//         await new Promise(res => setTimeout(res, 1000)); // wait 1s
//         result = await getSubmission(token);
//         const status = result.status?.description || 'Unknown';
//         console.log('Polling status:', status);
//         if (result.status?.id && result.status.id >= 3) { // 3: Accepted, 6: Compilation Error, etc.
//           break;
//         }
//       }
//       const finalStatus = result.status?.description || 'Unknown';
//       console.log('Final submission status:', finalStatus);
//       return result;
//     }
//   } catch (err) {
//     console.error('Error creating submission:', err);
//     throw err;
//   }
// }
// // Get submission result by token
// export async function getSubmission(token: string) {
//   try {
//     const response = await axiosInstance.get(`/submissions/${token}?base64_encoded=true`);
//     return response.data;
//   } catch (err) {
//     console.error('Error fetching submission:', err);
//     throw err;
//   }
// } 
const axios_1 = __importDefault(require("axios"));
const dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();
// Choose Extra CE or CE endpoint via environment flag
const useExtra = process.env.JUDGE0_EXTRA_CE === 'true';
const JUDGE0_API_BASE = useExtra
    ? "https://judge0-extra-ce.p.rapidapi.com"
    : "https://judge0-ce.p.rapidapi.com";
const RAPIDAPI_HOST = useExtra
    ? "judge0-extra-ce.p.rapidapi.com"
    : "judge0-ce.p.rapidapi.com";
const RAPIDAPI_KEY = process.env.JUDGE0_API_KEY;
const axiosInstance = axios_1.default.create({
    baseURL: JUDGE0_API_BASE,
    headers: {
        "X-RapidAPI-Host": RAPIDAPI_HOST,
        "X-RapidAPI-Key": RAPIDAPI_KEY,
        "Content-Type": "application/json"
    }
});
async function createSubmission(params) {
    const { languageId, sourceCodeBase64, stdinBase64, wait = true } = params;
    const payload = {
        language_id: languageId,
        source_code: sourceCodeBase64,
        ...(stdinBase64 && { stdin: stdinBase64 })
    };
    // (Filename and options already handled above if needed)
    try {
        const resp = await axiosInstance.post(`/submissions?base64_encoded=true&wait=${wait}`, payload);
        console.log("📨 Response →", resp.data);
        return resp.data;
    }
    catch (error) {
        console.error("❌ Submission failed:", error?.response?.data || error.message);
        throw error;
    }
}
//# sourceMappingURL=judge0.js.map