"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XcodebuildArgsService = void 0;
const path = require("path");
const constants = require("../../constants");
const constants_1 = require("../../common/constants");
const yok_1 = require("../../common/yok");
const _ = require("lodash");
const ios_project_service_1 = require("../ios-project-service");
class XcodebuildArgsService {
    constructor($devicePlatformsConstants, $devicesService, $fs, $iOSWatchAppService, $logger, $xcconfigService) {
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$devicesService = $devicesService;
        this.$fs = $fs;
        this.$iOSWatchAppService = $iOSWatchAppService;
        this.$logger = $logger;
        this.$xcconfigService = $xcconfigService;
    }
    async getBuildForSimulatorArgs(platformData, projectData, buildConfig) {
        let args = await this.getArchitecturesArgs(buildConfig);
        if (this.$iOSWatchAppService.hasWatchApp(platformData, projectData)) {
            args = args.concat(["CODE_SIGNING_ALLOWED=NO"]);
        }
        else {
            args = args.concat(["CODE_SIGN_IDENTITY="]);
        }
        let destination = "generic/platform=iOS Simulator";
        let isvisionOS = this.$devicePlatformsConstants.isvisionOS(buildConfig.platform);
        if (isvisionOS) {
            destination = "platform=visionOS Simulator";
            if (buildConfig._device) {
                destination += `,id=${buildConfig._device.deviceInfo.identifier}`;
            }
        }
        args = args
            .concat([
            "-destination",
            destination,
            "build",
            "-configuration",
            buildConfig.release ? constants_1.Configurations.Release : constants_1.Configurations.Debug,
        ])
            .concat(this.getBuildCommonArgs(platformData, projectData, isvisionOS ? ios_project_service_1.VisionSimulatorPlatformSdkName : ios_project_service_1.SimulatorPlatformSdkName))
            .concat(this.getBuildLoggingArgs())
            .concat(this.getXcodeProjectArgs(platformData, projectData));
        return args;
    }
    async getBuildForDeviceArgs(platformData, projectData, buildConfig) {
        const architectures = await this.getArchitecturesArgs(buildConfig);
        const archivePath = path.join(platformData.getBuildOutputPath(buildConfig), projectData.projectName + ".xcarchive");
        let destination = "generic/platform=iOS";
        let isvisionOS = this.$devicePlatformsConstants.isvisionOS(buildConfig.platform);
        if (isvisionOS) {
            destination = "platform=visionOS";
            if (buildConfig._device) {
                destination += `,id=${buildConfig._device.deviceInfo.identifier}`;
            }
        }
        const args = [
            "-destination",
            destination,
            "archive",
            "-archivePath",
            archivePath,
            "-configuration",
            buildConfig.release ? constants_1.Configurations.Release : constants_1.Configurations.Debug,
            "-allowProvisioningUpdates",
        ]
            .concat(this.getXcodeProjectArgs(platformData, projectData))
            .concat(architectures)
            .concat(this.getBuildCommonArgs(platformData, projectData, ios_project_service_1.DevicePlatformSdkName))
            .concat(this.getBuildLoggingArgs());
        return args;
    }
    async getArchitecturesArgs(buildConfig) {
        const args = [];
        if (this.$devicePlatformsConstants.isvisionOS(buildConfig.platform)) {
            args.push("ONLY_ACTIVE_ARCH=YES");
            return args;
        }
        const devicesArchitectures = buildConfig.buildForDevice
            ? await this.getArchitecturesFromConnectedDevices(buildConfig)
            : [];
        if (!buildConfig.buildForDevice || devicesArchitectures.length > 1) {
            args.push("ONLY_ACTIVE_ARCH=NO");
        }
        return args;
    }
    getXcodeProjectArgs(platformData, projectData) {
        const xcworkspacePath = path.join(platformData.projectRoot, `${projectData.projectName}.xcworkspace`);
        const skipPackageValidation = "-skipPackagePluginValidation";
        const extraArgs = [
            "-scheme",
            projectData.projectName,
            skipPackageValidation,
        ];
        const BUILD_SETTINGS_FILE_PATH = path.join(projectData.appResourcesDirectoryPath, platformData.normalizedPlatformName, constants.BUILD_XCCONFIG_FILE_NAME);
        const deployTargetProperty = "IPHONEOS_DEPLOYMENT_TARGET";
        const deployTargetVersion = this.$xcconfigService.readPropertyValue(BUILD_SETTINGS_FILE_PATH, deployTargetProperty);
        if (deployTargetVersion) {
            extraArgs.push(`${deployTargetProperty}=${deployTargetVersion}`);
        }
        const swiftUIBootProperty = "NS_SWIFTUI_BOOT";
        const swiftUIBootValue = this.$xcconfigService.readPropertyValue(BUILD_SETTINGS_FILE_PATH, swiftUIBootProperty);
        if (swiftUIBootValue) {
            extraArgs.push(`${swiftUIBootProperty}=${swiftUIBootValue}`);
        }
        if (this.$fs.exists(xcworkspacePath)) {
            return ["-workspace", xcworkspacePath, ...extraArgs];
        }
        const xcodeprojPath = path.join(platformData.projectRoot, `${projectData.projectName}.xcodeproj`);
        return ["-project", xcodeprojPath, ...extraArgs];
    }
    getBuildLoggingArgs() {
        return this.$logger.getLevel() === "INFO" ? ["-quiet"] : [];
    }
    getBuildCommonArgs(platformData, projectData, platformSdkName) {
        let args = [];
        if (!this.$iOSWatchAppService.hasWatchApp(platformData, projectData)) {
            args = args.concat(["-sdk", platformSdkName]);
        }
        args = args.concat([
            "BUILD_DIR=" + path.join(platformData.projectRoot, constants.BUILD_DIR),
            "SHARED_PRECOMPS_DIR=" +
                path.join(platformData.projectRoot, constants.BUILD_DIR, "sharedpch"),
        ]);
        return args;
    }
    async getArchitecturesFromConnectedDevices(buildConfig) {
        const platform = this.$devicePlatformsConstants.iOS.toLowerCase();
        await this.$devicesService.initialize({
            platform,
            deviceId: buildConfig.device,
            skipEmulatorStart: true,
        });
        const instances = this.$devicesService.getDevicesForPlatform(platform);
        const architectures = _(instances)
            .map((d) => d.deviceInfo.activeArchitecture)
            .filter((d) => !!d)
            .uniq()
            .value();
        return architectures;
    }
}
exports.XcodebuildArgsService = XcodebuildArgsService;
yok_1.injector.register("xcodebuildArgsService", XcodebuildArgsService);
