"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.AndroidBundleToolService = void 0;
const path_1 = require("path");
const _ = require("lodash");
const helpers_1 = require("../../common/helpers");
const yok_1 = require("../../common/yok");
class AndroidBundleToolService {
    constructor($childProcess, $sysInfo, $errors) {
        this.$childProcess = $childProcess;
        this.$sysInfo = $sysInfo;
        this.$errors = $errors;
        this.aabToolPath = (0, path_1.resolve)((0, path_1.join)(__dirname, "../../../vendor/aab-tool/bundletool.jar"));
    }
    buildApks(options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!(0, helpers_1.hasValidAndroidSigning)(options.signingData)) {
                this.$errors.fail(`Unable to build "apks" without a full signing information.`);
            }
            const aabToolResult = yield this.execBundleTool([
                "build-apks",
                "--bundle",
                options.aabFilePath,
                "--output",
                options.apksOutputPath,
                "--ks",
                options.signingData.keyStorePath,
                "--ks-pass",
                `pass:${options.signingData.keyStorePassword}`,
                "--ks-key-alias",
                options.signingData.keyStoreAlias,
                "--key-pass",
                `pass:${options.signingData.keyStoreAliasPassword}`,
            ]);
            if (aabToolResult.exitCode !== 0 && aabToolResult.stderr) {
                this.$errors.fail(`Unable to build "apks" from the provided "aab". Error: ${aabToolResult.stderr}`);
            }
        });
    }
    installApks(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const aabToolResult = yield this.execBundleTool([
                "install-apks",
                "--apks",
                options.apksFilePath,
                "--device-id",
                options.deviceId,
            ]);
            if (aabToolResult.exitCode !== 0 && aabToolResult.stderr) {
                this.$errors.fail(`Unable to install "apks" on device "${options.deviceId}". Error: ${aabToolResult.stderr}`);
            }
        });
    }
    execBundleTool(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const javaPath = yield this.getJavaPath();
            const defaultArgs = ["-jar", this.aabToolPath];
            const result = yield this.$childProcess.trySpawnFromCloseEvent(javaPath, _.concat(defaultArgs, args));
            return result;
        });
    }
    getJavaPath() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.javaPath) {
                this.javaPath = yield this.$sysInfo.getJavaPath();
            }
            return this.javaPath;
        });
    }
}
exports.AndroidBundleToolService = AndroidBundleToolService;
yok_1.injector.register("androidBundleToolService", AndroidBundleToolService);
