"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.ExportOptionsPlistService = void 0;
const path = require("path");
const mobileProvisionFinder = require("ios-mobileprovision-finder");
const yok_1 = require("../../common/yok");
const constants = require("../../constants");
class ExportOptionsPlistService {
    constructor($fs, $tempService, $projectData) {
        this.$fs = $fs;
        this.$tempService = $tempService;
        this.$projectData = $projectData;
    }
    getExtensionProvisions() {
        const provisioningJSONPath = path.join(this.$projectData.getAppResourcesDirectoryPath(), constants.iOSAppResourcesFolderName, constants.NATIVE_EXTENSION_FOLDER, constants.EXTENSION_PROVISIONING_FILENAME);
        if (!this.$fs.exists(provisioningJSONPath)) {
            return "";
        }
        const provisioningJSON = this.$fs.readJson(provisioningJSONPath);
        return Object.entries(provisioningJSON)
            .map(([id, provision]) => {
            return `<key>${id}</key>\n	<string>${provision}</string>`;
        })
            .join("\n");
    }
    createDevelopmentExportOptionsPlist(archivePath, projectData, buildConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            const exportOptionsMethod = this.getExportOptionsMethod(projectData, archivePath);
            const provision = buildConfig.provision || buildConfig.mobileProvisionIdentifier;
            const iCloudContainerEnvironment = buildConfig.iCloudContainerEnvironment;
            let plistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>method</key>
	<string>${exportOptionsMethod}</string>`;
            if (provision) {
                plistTemplate += `    <key>provisioningProfiles</key>
<dict>
	<key>${projectData.projectIdentifiers.ios}</key>
	<string>${provision}</string>
	${this.getExtensionProvisions()}
</dict>`;
            }
            plistTemplate += `
    <key>uploadBitcode</key>
    <false/>
    <key>compileBitcode</key>
    <false/>`;
            if (iCloudContainerEnvironment) {
                plistTemplate += `
    <key>iCloudContainerEnvironment</key>
    <string>${iCloudContainerEnvironment}</string>`;
            }
            plistTemplate += `
</dict>
</plist>`;
            const exportOptionsPlistFilePath = yield this.$tempService.path({
                prefix: "export-",
                suffix: ".plist",
            });
            this.$fs.writeFile(exportOptionsPlistFilePath, plistTemplate);
            const exportFileDir = path.resolve(path.dirname(archivePath));
            const exportFilePath = path.join(exportFileDir, projectData.projectName + ".ipa");
            return { exportFileDir, exportFilePath, exportOptionsPlistFilePath };
        });
    }
    createDistributionExportOptionsPlist(archivePath, projectData, buildConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            const provision = buildConfig.provision || buildConfig.mobileProvisionIdentifier;
            const teamId = buildConfig.teamId;
            let plistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
`;
            if (teamId) {
                plistTemplate += `    <key>teamID</key>
    <string>${teamId}</string>
`;
            }
            if (provision) {
                plistTemplate += `    <key>provisioningProfiles</key>
    <dict>
        <key>${projectData.projectIdentifiers.ios}</key>
        <string>${provision}</string>
        ${this.getExtensionProvisions()}
    </dict>`;
            }
            plistTemplate += `    <key>method</key>
    <string>app-store</string>
    <key>uploadBitcode</key>
    <false/>
    <key>compileBitcode</key>
    <false/>
    <key>uploadSymbols</key>
    <false/>
</dict>
</plist>`;
            const exportOptionsPlistFilePath = yield this.$tempService.path({
                prefix: "export-",
                suffix: ".plist",
            });
            this.$fs.writeFile(exportOptionsPlistFilePath, plistTemplate);
            const exportFileDir = path.resolve(path.dirname(archivePath));
            const exportFilePath = path.join(exportFileDir, projectData.projectName + ".ipa");
            return { exportFileDir, exportFilePath, exportOptionsPlistFilePath };
        });
    }
    getExportOptionsMethod(projectData, archivePath) {
        const embeddedMobileProvisionPath = path.join(archivePath, "Products", "Applications", `${projectData.projectName}.app`, "embedded.mobileprovision");
        const provision = mobileProvisionFinder.provision.readFromFile(embeddedMobileProvisionPath);
        return {
            Development: "development",
            AdHoc: "ad-hoc",
            Distribution: "app-store",
            Enterprise: "enterprise",
        }[provision.Type];
    }
}
exports.ExportOptionsPlistService = ExportOptionsPlistService;
yok_1.injector.register("exportOptionsPlistService", ExportOptionsPlistService);
