"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const crypto_1 = tslib_1.__importDefault(require("crypto"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const command_1 = require("@oclif/command");
const inquirer_1 = require("inquirer");
const logger_1 = tslib_1.__importDefault(require("../utils/logger"));
const base_1 = tslib_1.__importDefault(require("../base"));
const utils_1 = require("../utils");
const collection_1 = require("../utils/collection");
const git_1 = require("../utils/git");
const constants_1 = require("../constants");
class Init extends base_1.default {
    async promptInitGit(yes) {
        const response = yes
            ? { answer: true }
            : await inquirer_1.prompt([
                {
                    name: 'answer',
                    type: 'confirm',
                    message: 'You are not in a Git repository, do you want to initialize one?',
                    default: false,
                },
            ]);
        if (!response.answer) {
            this.exit(0);
        }
        else {
            await git_1.git.init();
            logger_1.default.log('success', 'Git repository is initialized!');
        }
    }
    async promptMetaData(yes) {
        const answer = yes
            ? { name: 'My Awesome Tutorial' }
            : await inquirer_1.prompt([
                {
                    name: 'name',
                    message: 'Tutorial Name',
                    default: 'My Awesome Tutorial',
                },
                {
                    name: 'description',
                    message: 'Description',
                },
                {
                    name: 'topics',
                    message: 'Topics',
                },
                {
                    name: 'categories',
                    message: 'Categories',
                },
            ]);
        answer.id = crypto_1.default.randomBytes(16).toString('hex');
        // TODO: process user input with inquirer built-ins
        const { topics, categories } = answer;
        if (topics) {
            answer.topics = topics.split(/\W+/);
        }
        else {
            delete answer.topics;
        }
        if (categories) {
            answer.categories = categories.split(/\W+/);
        }
        else {
            delete answer.categories;
        }
        return answer;
    }
    async run() {
        const { flags } = this.parse(Init);
        if (fs_extra_1.default.existsSync(constants_1.COLLECTION_PATH)) {
            logger_1.default.log('success', 'Tuture tutorial has already been initialized!');
            this.exit(0);
        }
        if (!fs_extra_1.default.existsSync('.git')) {
            await this.promptInitGit(flags.yes);
        }
        const meta = await this.promptMetaData(flags.yes);
        try {
            const steps = await utils_1.makeSteps(this.userConfig.ignoredFiles);
            steps.forEach((step) => {
                step.articleId = meta.id;
            });
            const collection = Object.assign(Object.assign({}, meta), { created: new Date(), articles: [meta], steps });
            const github = await git_1.inferGithubField();
            if (github) {
                logger_1.default.log('info', `Inferred github repository: ${chalk_1.default.underline(github)}. Feel free to revise or delete it.`);
                collection.github = github;
            }
            collection_1.saveCollection(collection);
            git_1.appendGitignore(this.userConfig);
            logger_1.default.log('success', 'Tuture tutorial has been initialized!');
        }
        catch (err) {
            await utils_1.removeTutureSuite();
            logger_1.default.log({
                level: 'error',
                message: err.message,
                error: err,
            });
            this.exit(1);
        }
    }
}
exports.default = Init;
Init.description = 'Initialize a tuture tutorial';
Init.flags = {
    help: command_1.flags.help({ char: 'h' }),
    yes: command_1.flags.boolean({
        char: 'y',
        description: 'do not ask for prompts',
    }),
};
