"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const ios_native_target_service_base_1 = require("./ios-native-target-service-base");
const constants_1 = require("../constants");
class IOSWatchAppService extends ios_native_target_service_base_1.NativeTargetServiceBase {
    constructor($fs, $pbxprojDomXcode, $xcode) {
        super($fs, $pbxprojDomXcode, $xcode);
        this.$fs = $fs;
        this.$pbxprojDomXcode = $pbxprojDomXcode;
        this.$xcode = $xcode;
    }
    addWatchAppFromPath({ watchAppFolderPath, projectData, platformData, pbxProjPath }) {
        return __awaiter(this, void 0, void 0, function* () {
            const targetUuids = [];
            const appPath = path.join(watchAppFolderPath, constants_1.IOS_WATCHAPP_FOLDER);
            const extensionPath = path.join(watchAppFolderPath, constants_1.IOS_WATCHAPP_EXTENSION_FOLDER);
            if (!this.$fs.exists(appPath) || !this.$fs.exists(extensionPath)) {
                return false;
            }
            const appFolder = this.getTargetDirectories(appPath)[0];
            const extensionFolder = this.getTargetDirectories(extensionPath)[0];
            const project = new this.$xcode.project(pbxProjPath);
            project.parseSync();
            const watchApptarget = this.addTargetToProject(appPath, appFolder, constants_1.IOSNativeTargetTypes.watchApp, project, platformData, project.getFirstTarget().uuid);
            this.configureTarget(appFolder, path.join(appPath, appFolder), `${projectData.projectIdentifiers.ios}.${IOSWatchAppService.WATCH_APP_IDENTIFIER}`, "watchapp.json", watchApptarget, project);
            targetUuids.push(watchApptarget.uuid);
            const watchExtensionTarget = this.addTargetToProject(extensionPath, extensionFolder, constants_1.IOSNativeTargetTypes.watchExtension, project, platformData, watchApptarget.uuid);
            this.configureTarget(extensionFolder, path.join(extensionPath, extensionFolder), `${projectData.projectIdentifiers.ios}.${IOSWatchAppService.WATCH_APP_IDENTIFIER}.${IOSWatchAppService.WACTCH_EXTENSION_IDENTIFIER}`, "extension.json", watchExtensionTarget, project);
            targetUuids.push(watchExtensionTarget.uuid);
            this.$fs.writeFile(pbxProjPath, project.writeSync({ omitEmptyValues: true }));
            this.prepareSigning(targetUuids, projectData, pbxProjPath);
            return true;
        });
    }
    removeWatchApp({ pbxProjPath }) {
        const project = new this.$xcode.project(pbxProjPath);
        project.parseSync();
        project.removeTargetsByProductType(constants_1.IOSNativeTargetProductTypes.watchApp);
        project.removeTargetsByProductType(constants_1.IOSNativeTargetProductTypes.watchExtension);
        this.$fs.writeFile(pbxProjPath, project.writeSync({ omitEmptyValues: true }));
    }
    configureTarget(targetName, targetPath, identifier, configurationFileName, target, project) {
        const targetConfigurationJsonPath = path.join(targetPath, configurationFileName);
        const identifierParts = identifier.split(".");
        identifierParts.pop();
        const wkAppBundleIdentifier = identifierParts.join(".");
        this.setXcodeTargetBuildConfigurationProperties([
            { name: "PRODUCT_BUNDLE_IDENTIFIER", value: identifier },
            { name: "SDKROOT", value: "watchos" },
            { name: "TARGETED_DEVICE_FAMILY", value: constants_1.IOSDeviceTargets.watchos },
            { name: "WATCHOS_DEPLOYMENT_TARGET", value: 5.2 },
            { name: "WK_APP_BUNDLE_IDENTIFIER", value: wkAppBundleIdentifier }
        ], targetName, project);
        this.setConfigurationsFromJsonFile(targetConfigurationJsonPath, target.uuid, targetName, project);
        project.addToHeaderSearchPaths(targetPath, target.pbxNativeTarget.productName);
    }
}
IOSWatchAppService.WATCH_APP_IDENTIFIER = "watchkitapp";
IOSWatchAppService.WACTCH_EXTENSION_IDENTIFIER = "watchkitextension";
exports.IOSWatchAppService = IOSWatchAppService;
$injector.register("iOSWatchAppService", IOSWatchAppService);
