"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.GradleBuildArgsService = void 0;
const path = require("path");
const constants_1 = require("../../common/constants");
const yok_1 = require("../../common/yok");
class GradleBuildArgsService {
    constructor($androidToolsInfo, $hooksService, $analyticsService, $staticConfig, $projectData, $logger) {
        this.$androidToolsInfo = $androidToolsInfo;
        this.$hooksService = $hooksService;
        this.$analyticsService = $analyticsService;
        this.$staticConfig = $staticConfig;
        this.$projectData = $projectData;
        this.$logger = $logger;
    }
    getBuildTaskArgs(buildData) {
        return __awaiter(this, void 0, void 0, function* () {
            const args = this.getBaseTaskArgs(buildData);
            args.unshift(this.getBuildTaskName(buildData));
            if (yield this.$analyticsService.isEnabled(this.$staticConfig.TRACK_FEATURE_USAGE_SETTING_NAME)) {
                args.push("-PgatherAnalyticsData=true");
            }
            yield this.$hooksService.executeBeforeHooks("build-task-args", {
                hookArgs: { args },
            });
            return args;
        });
    }
    getCleanTaskArgs(buildData) {
        const args = this.getBaseTaskArgs(buildData);
        args.unshift("clean");
        return args;
    }
    getBaseTaskArgs(buildData) {
        const args = this.getBuildLoggingArgs();
        const toolsInfo = this.$androidToolsInfo.getToolsInfo({
            projectDir: buildData.projectDir,
        });
        this.$projectData.initializeProjectData(buildData.projectDir);
        args.push(`-PcompileSdk=android-${toolsInfo.compileSdkVersion}`, `-PtargetSdk=${toolsInfo.targetSdkVersion}`, `-PbuildToolsVersion=${toolsInfo.buildToolsVersion}`, `-PgenerateTypings=${toolsInfo.generateTypings}`, `-PappPath=${this.$projectData.getAppDirectoryPath()}`, `-PappResourcesPath=${this.$projectData.getAppResourcesDirectoryPath()}`);
        if (buildData.gradleArgs) {
            args.push(buildData.gradleArgs);
        }
        if (buildData.release) {
            args.push("-Prelease", `-PksPath=${path.resolve(buildData.keyStorePath)}`, `-Palias=${buildData.keyStoreAlias}`, `-Ppassword=${buildData.keyStoreAliasPassword}`, `-PksPassword=${buildData.keyStorePassword}`);
        }
        return args;
    }
    getBuildLoggingArgs() {
        const args = [];
        const logLevel = this.$logger.getLevel();
        if (logLevel === "TRACE") {
            args.push("--stacktrace", "--debug");
        }
        else if (logLevel === "INFO") {
            args.push("--quiet");
        }
        return args;
    }
    getBuildTaskName(buildData) {
        const baseTaskName = buildData.androidBundle ? "bundle" : "assemble";
        const buildTaskName = buildData.release
            ? `${baseTaskName}${constants_1.Configurations.Release}`
            : `${baseTaskName}${constants_1.Configurations.Debug}`;
        return buildTaskName;
    }
}
exports.GradleBuildArgsService = GradleBuildArgsService;
yok_1.injector.register("gradleBuildArgsService", GradleBuildArgsService);
