"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IOSWatchAppService = void 0;
const path = require("path");
const constants_1 = require("../constants");
const yok_1 = require("../common/yok");
class IOSWatchAppService {
    constructor($fs, $pbxprojDomXcode, $xcode, $iOSNativeTargetService) {
        this.$fs = $fs;
        this.$pbxprojDomXcode = $pbxprojDomXcode;
        this.$xcode = $xcode;
        this.$iOSNativeTargetService = $iOSNativeTargetService;
    }
    async addWatchAppFromPath({ watchAppFolderPath, projectData, platformData, pbxProjPath, }) {
        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.$iOSNativeTargetService.getTargetDirectories(appPath)[0];
        const extensionFolder = this.$iOSNativeTargetService.getTargetDirectories(extensionPath)[0];
        const project = new this.$xcode.project(pbxProjPath);
        project.parseSync();
        const watchApptarget = this.$iOSNativeTargetService.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.$iOSNativeTargetService.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.$iOSNativeTargetService.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 }));
    }
    hasWatchApp(platformData, projectData) {
        const watchAppPath = path.join(projectData.getAppResourcesDirectoryPath(), platformData.normalizedPlatformName, constants_1.IOS_WATCHAPP_FOLDER);
        return this.$fs.exists(watchAppPath);
    }
    configureTarget(targetName, targetPath, identifier, configurationFileName, target, project) {
        const targetConfigurationJsonPath = path.join(targetPath, configurationFileName);
        const identifierParts = identifier.split(".");
        identifierParts.pop();
        const wkAppBundleIdentifier = identifierParts.join(".");
        this.$iOSNativeTargetService.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.$iOSNativeTargetService.setConfigurationsFromJsonFile(targetConfigurationJsonPath, target.uuid, targetName, project);
        project.addToHeaderSearchPaths(targetPath, target.pbxNativeTarget.productName);
    }
}
exports.IOSWatchAppService = IOSWatchAppService;
IOSWatchAppService.WATCH_APP_IDENTIFIER = "watchkitapp";
IOSWatchAppService.WACTCH_EXTENSION_IDENTIFIER = "watchkitextension";
yok_1.injector.register("iOSWatchAppService", IOSWatchAppService);
