"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SessionCommand_1 = require("../utils/SessionCommand");
const fs = require("fs");
const path = require("path");
const prompts = require("prompts");
const rigoActions_1 = require("../utils/rigoActions");
const session_1 = require("../managers/session");
const console_1 = require("../utils/console");
const creatorUtilities_1 = require("../utils/creatorUtilities");
// This function list the names of the exercise directories inside the ./exercises folder, if the exercises folder doesn't exist, it will look for the ./.learn/exercises folder
const listExercises = async () => {
    const exercisesDir = path.join(process.cwd(), "exercises");
    const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises");
    const exercises = fs.readdirSync(exercisesDir).filter(file => {
        return fs.statSync(path.join(exercisesDir, file)).isDirectory();
    });
    if (exercises.length > 0) {
        return exercises;
    }
    return fs.readdirSync(learnExercisesDir).filter(file => {
        return fs.statSync(path.join(learnExercisesDir, file)).isDirectory();
    });
};
const getReadmeForExercise = async (exercise) => {
    const readmePath = path.join(process.cwd(), "exercises", exercise, "README.md");
    return fs.readFileSync(readmePath, "utf8");
};
class BuildCommand extends SessionCommand_1.default {
    async init() {
        const { flags } = this.parse(BuildCommand);
        await this.initSession(flags);
    }
    async run() {
        var _a, _b;
        const { flags } = this.parse(BuildCommand);
        const exercises = await listExercises();
        const exercisesToTranslate = await prompts([
            {
                type: "multiselect",
                name: "exercises",
                message: "Select the exercises to translate",
                choices: exercises.map(exercise => ({
                    title: exercise,
                    value: exercise,
                })),
            },
            {
                type: "text",
                name: "output_language",
                message: "Write the languages to translate to comma separated. Example: English, Spanish, French",
                initial: "Spanish",
            },
        ]);
        const configObject = (_a = this.configManager) === null || _a === void 0 ? void 0 : _a.get();
        if (configObject) {
            // build exerises
            console_1.default.debug("Building exercises");
            (_b = this.configManager) === null || _b === void 0 ? void 0 : _b.buildIndex();
        }
        let sessionPayload = await session_1.default.getPayload();
        if (!sessionPayload || !sessionPayload.rigobot) {
            console_1.default.error("You must be logged in to upload a LearnPack package");
            try {
                sessionPayload = await session_1.default.login();
            }
            catch (error) {
                console_1.default.error("Error trying to authenticate");
                console_1.default.error(error.message || error);
            }
        }
        const rigoToken = sessionPayload.rigobot.key;
        await Promise.all(exercisesToTranslate.exercises.map(async (exercise) => {
            await Promise.all(exercisesToTranslate.output_language
                .split(",")
                .map(async (language) => {
                const readme = await getReadmeForExercise(exercise);
                const response = await (0, rigoActions_1.translateExercise)(rigoToken, {
                    text_to_translate: readme,
                    output_language: language,
                    exercise_slug: exercise,
                });
                await (0, creatorUtilities_1.saveTranslatedReadme)(exercise, response.parsed.output_language_code, response.parsed.translation);
                console_1.default.success(`Translated ${exercise} to ${language} successfully`);
            }));
        }));
        this.exit(0);
    }
}
BuildCommand.description = "List all the lessons, the user is able of select many of them to translate to the given languages";
exports.default = BuildCommand;
