"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XcodebuildService = void 0;
const path = require("path");
const yok_1 = require("../../common/yok");
class XcodebuildService {
    constructor($exportOptionsPlistService, $xcodebuildArgsService, $xcodebuildCommandService) {
        this.$exportOptionsPlistService = $exportOptionsPlistService;
        this.$xcodebuildArgsService = $xcodebuildArgsService;
        this.$xcodebuildCommandService = $xcodebuildCommandService;
    }
    async buildForDevice(platformData, projectData, buildConfig) {
        const args = await this.$xcodebuildArgsService.getBuildForDeviceArgs(platformData, projectData, buildConfig);
        await this.$xcodebuildCommandService.executeCommand(args, {
            cwd: platformData.projectRoot,
            stdio: buildConfig && buildConfig.buildOutputStdio,
        });
        const archivePath = await this.createDevelopmentArchive(platformData, projectData, buildConfig);
        return archivePath;
    }
    async buildForSimulator(platformData, projectData, buildConfig) {
        const args = await this.$xcodebuildArgsService.getBuildForSimulatorArgs(platformData, projectData, buildConfig);
        await this.$xcodebuildCommandService.executeCommand(args, {
            cwd: platformData.projectRoot,
            stdio: buildConfig.buildOutputStdio,
        });
    }
    async buildForAppStore(platformData, projectData, buildConfig) {
        const args = await this.$xcodebuildArgsService.getBuildForDeviceArgs(platformData, projectData, buildConfig);
        await this.$xcodebuildCommandService.executeCommand(args, {
            cwd: platformData.projectRoot,
            stdio: buildConfig && buildConfig.buildOutputStdio,
        });
        const archivePath = await this.createDistributionArchive(platformData, projectData, buildConfig);
        return archivePath;
    }
    async createDevelopmentArchive(platformData, projectData, buildConfig) {
        const archivePath = path.join(platformData.getBuildOutputPath(buildConfig), projectData.projectName + ".xcarchive");
        const output = await this.$exportOptionsPlistService.createDevelopmentExportOptionsPlist(archivePath, projectData, buildConfig);
        const args = [
            "-exportArchive",
            "-archivePath",
            archivePath,
            "-exportPath",
            output.exportFileDir,
            "-exportOptionsPlist",
            output.exportOptionsPlistFilePath,
        ];
        await this.$xcodebuildCommandService.executeCommand(args, {
            cwd: platformData.projectRoot,
            stdio: buildConfig.buildOutputStdio,
        });
        return output.exportFilePath;
    }
    async createDistributionArchive(platformData, projectData, buildConfig) {
        const archivePath = path.join(platformData.getBuildOutputPath(buildConfig), projectData.projectName + ".xcarchive");
        const output = await this.$exportOptionsPlistService.createDistributionExportOptionsPlist(archivePath, projectData, buildConfig);
        const provision = buildConfig.provision || buildConfig.mobileProvisionIdentifier;
        const args = [
            "-exportArchive",
            "-archivePath",
            archivePath,
            "-exportPath",
            output.exportFileDir,
            "-exportOptionsPlist",
            output.exportOptionsPlistFilePath,
            provision ? "" : "-allowProvisioningUpdates",
        ];
        await this.$xcodebuildCommandService.executeCommand(args, {
            cwd: platformData.projectRoot,
        });
        return output.exportFilePath;
    }
}
exports.XcodebuildService = XcodebuildService;
yok_1.injector.register("xcodebuildService", XcodebuildService);
