"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.solveTests = exports.validateTests = exports.generateTests = exports.createTemplate = void 0;
const helpers_1 = require("./helpers");
const logger_1 = require("./logger");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const createTemplate = (directory) => {
    // const spinner = logger.startSpinner('Creating problem template...');
    logger_1.logger.info('Creating problem template...');
    try {
        const pwd = process.cwd();
        const problemDir = path_1.default.resolve(pwd, directory);
        const templateDir = path_1.default.resolve(__dirname, '../template');
        fs_1.default.mkdirSync(problemDir, { recursive: true });
        (0, helpers_1.copyTemplate)(templateDir, problemDir);
        // spinner.succeed(
        //   `Template created successfully at ${logger.highlight(directory)}`
        // );
        logger_1.logger.info(`Next steps:`);
        logger_1.logger.log(`  1. cd ${directory}`);
        logger_1.logger.log(`  2. Edit config.json to configure your problem`);
        logger_1.logger.log(`  3. Run ${logger_1.logger.highlight('polyman generate-tests')}`);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        // spinner.fail('Failed to create template');
        logger_1.logger.error(message);
        process.exit(1);
    }
};
exports.createTemplate = createTemplate;
const generateTests = async (generatorName) => {
    logger_1.logger.section('Test Generation');
    try {
        const { generators } = (0, helpers_1.readConfigFile)();
        if (!generators || generators.length === 0) {
            logger_1.logger.warning('No test generators defined in the configuration file.');
            return;
        }
        logger_1.logger.info(`Found ${generators.length} generator(s)`);
        for (const generator of generators) {
            if (generator.name === generatorName || generatorName === 'all') {
                await (0, helpers_1.runGenerator)(generator);
            }
        }
        logger_1.logger.success('All test cases generated successfully!');
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        logger_1.logger.error(`Failed to generate tests: ${message}`);
        process.exit(1);
    }
};
exports.generateTests = generateTests;
const validateTests = async (arg) => {
    logger_1.logger.section('Test Validation');
    try {
        const config = (0, helpers_1.readConfigFile)();
        if (!config.validator) {
            throw new Error('No validator defined in the configuration file.');
        }
        if (arg === 'all') {
            await (0, helpers_1.runValidator)(config.validator);
            logger_1.logger.success('All tests validated successfully!');
        }
        else {
            const testNumber = parseInt(arg, 10);
            if (!isNaN(testNumber)) {
                await (0, helpers_1.runValidator)(config.validator, testNumber, testNumber);
                logger_1.logger.success(`Test ${testNumber} validated successfully!`);
            }
            else {
                const generator = config.generators?.find(g => g.name === arg);
                if (!generator) {
                    throw new Error(`No generator named "${arg}" found in the configuration file.`);
                }
                const [start, end] = generator['tests-range'];
                await (0, helpers_1.runValidator)(config.validator, start, end);
                logger_1.logger.success(`Tests generated by "${arg}" validated successfully!`);
            }
        }
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        logger_1.logger.error(`Failed to validate tests: ${message}`);
        process.exit(1);
    }
};
exports.validateTests = validateTests;
const solveTests = async (solutionName, arg) => {
    logger_1.logger.section(`Running Solution: ${solutionName}`);
    try {
        const config = (0, helpers_1.readConfigFile)();
        if (!config.solutions || config.solutions.length === 0) {
            throw new Error('No solutions defined in the configuration file.');
        }
        let found = false;
        if (arg === 'all') {
            for (const solution of config.solutions) {
                if (solution.name === solutionName || solutionName === 'all') {
                    found = true;
                    await (0, helpers_1.runSolution)(solution, config['time-limit'], config['memory-limit']);
                }
            }
        }
        else {
            const testNumber = parseInt(arg, 10);
            if (!isNaN(testNumber)) {
                for (const solution of config.solutions) {
                    if (solution.name === solutionName || solutionName === 'all') {
                        found = true;
                        await (0, helpers_1.runSolution)(solution, config['time-limit'], config['memory-limit'], testNumber, testNumber);
                    }
                }
            }
            else {
                const generator = config.generators?.find(g => g.name === arg);
                if (!generator) {
                    throw new Error(`No generator named "${arg}" found in the configuration file.`);
                }
                const [start, end] = generator['tests-range'];
                for (const solution of config.solutions) {
                    if (solution.name === solutionName || solutionName === 'all') {
                        found = true;
                        await (0, helpers_1.runSolution)(solution, config['time-limit'], config['memory-limit'], start, end);
                    }
                }
            }
        }
        if (!found) {
            throw new Error(`Solution named "${solutionName}" not found or no solutions defined.`);
        }
        logger_1.logger.success('Solutions run completed successfully!');
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        logger_1.logger.error(`Failed to run solution: ${message}`);
        process.exit(1);
    }
};
exports.solveTests = solveTests;
//# sourceMappingURL=actions-old.js.map