"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
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 base_1 = tslib_1.__importDefault(require("../base"));
const utils_1 = require("../utils");
const logger_1 = tslib_1.__importDefault(require("../utils/logger"));
const git_1 = require("../utils/git");
const collection_1 = require("../utils/collection");
const constants_1 = require("../constants");
class Push extends base_1.default {
    async run() {
        const { flags } = this.parse(Push);
        this.userConfig = Object.assign(this.userConfig, flags);
        try {
            await utils_1.checkInitStatus();
        }
        catch (err) {
            logger_1.default.log('error', err.message);
            this.exit(1);
        }
        let remoteToPush = flags.remote;
        if (!remoteToPush) {
            const remotes = await git_1.git.getRemotes(true);
            if (remotes.length === 0) {
                logger_1.default.log('error', 'Remote repository has not been configured.');
                this.exit(1);
            }
            else {
                // Select the first remote by default.
                remoteToPush = remotes[0].name;
            }
        }
        await collection_1.initializeTutureBranch();
        try {
            // Checkout tuture branch and add tuture.yml.
            await git_1.git.checkout(constants_1.TUTURE_BRANCH);
            if (!fs_extra_1.default.existsSync(collection_1.collectionPath)) {
                logger_1.default.log('error', `Cannot push empty tuture branch. Please commit your tutorial with ${chalk_1.default.bold('tuture commit')}.`);
                this.exit(1);
            }
            logger_1.default.log('info', `Starting to push to ${remoteToPush}.`);
            await git_1.git.push(remoteToPush, constants_1.TUTURE_BRANCH);
            logger_1.default.log('success', `Pushed to ${remoteToPush} successfully.`);
        }
        catch (err) {
            logger_1.default.log('error', String(err.message).trim());
        }
    }
}
exports.default = Push;
Push.description = 'Push the tuture branch to remote';
Push.flags = {
    help: command_1.flags.help({ char: 'h' }),
    remote: command_1.flags.string({ char: 'r', description: 'name of remote to push' }),
};
