"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildPluginCommand = void 0;
const os_1 = require("os");
const path = require("path");
const constants = require("../../constants");
const yok_1 = require("../../common/yok");
class BuildPluginCommand {
    constructor($androidPluginBuildService, $errors, $logger, $fs, $options, $tempService) {
        this.$androidPluginBuildService = $androidPluginBuildService;
        this.$errors = $errors;
        this.$logger = $logger;
        this.$fs = $fs;
        this.$options = $options;
        this.$tempService = $tempService;
        this.allowedParameters = [];
        this.pluginProjectPath = path.resolve(this.$options.path || ".");
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const platformsAndroidPath = path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android");
            let pluginName = "";
            const pluginPackageJsonPath = path.join(this.pluginProjectPath, constants.PACKAGE_JSON_FILE_NAME);
            if (this.$fs.exists(pluginPackageJsonPath)) {
                const packageJsonContents = this.$fs.readJson(pluginPackageJsonPath);
                if (packageJsonContents && packageJsonContents["name"]) {
                    pluginName = packageJsonContents["name"];
                }
            }
            const tempAndroidProject = yield this.$tempService.mkdirSync("android-project");
            const options = {
                gradlePath: this.$options.gradlePath,
                gradleArgs: this.$options.gradleArgs,
                aarOutputDir: platformsAndroidPath,
                platformsAndroidDirPath: platformsAndroidPath,
                pluginName: pluginName,
                tempPluginDirPath: tempAndroidProject,
            };
            const androidPluginBuildResult = yield this.$androidPluginBuildService.buildAar(options);
            if (androidPluginBuildResult) {
                this.$logger.info(`${pluginName} successfully built aar at ${platformsAndroidPath}.${os_1.EOL}Temporary Android project can be found at ${tempAndroidProject}.`);
            }
            const migratedIncludeGradle = this.$androidPluginBuildService.migrateIncludeGradle(options);
            if (migratedIncludeGradle) {
                this.$logger.info(`${pluginName} include gradle updated.`);
            }
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.$fs.exists(path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android"))) {
                this.$errors.fail("No plugin found at the current directory, or the plugin does not need to have its platforms/android components built into an `.aar`.");
            }
            return true;
        });
    }
}
exports.BuildPluginCommand = BuildPluginCommand;
yok_1.injector.registerCommand("plugin|build", BuildPluginCommand);
