"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
var BuildNames;
(function (BuildNames) {
    BuildNames["debug"] = "Debug";
    BuildNames["release"] = "Release";
})(BuildNames = exports.BuildNames || (exports.BuildNames = {}));
class NativeTargetServiceBase {
    constructor($fs, $pbxprojDomXcode, $xcode) {
        this.$fs = $fs;
        this.$pbxprojDomXcode = $pbxprojDomXcode;
        this.$xcode = $xcode;
    }
    addTargetToProject(extensionsFolderPath, extensionFolder, targetType, project, platformData, parentTarget) {
        const extensionPath = path.join(extensionsFolderPath, extensionFolder);
        const extensionRelativePath = path.relative(platformData.projectRoot, extensionPath);
        const files = this.$fs.readDirectory(extensionPath)
            .filter(filePath => !filePath.startsWith("."))
            .map(filePath => path.join(extensionPath, filePath));
        const target = project.addTarget(extensionFolder, targetType, extensionRelativePath, parentTarget);
        project.addBuildPhase([], 'PBXSourcesBuildPhase', 'Sources', target.uuid);
        project.addBuildPhase([], 'PBXResourcesBuildPhase', 'Resources', target.uuid);
        project.addBuildPhase([], 'PBXFrameworksBuildPhase', 'Frameworks', target.uuid);
        project.addPbxGroup(files, extensionFolder, extensionPath, null, { isMain: true, target: target.uuid, filesRelativeToProject: true });
        project.addToHeaderSearchPaths(extensionPath, target.pbxNativeTarget.productName);
        return target;
    }
    prepareSigning(targetUuids, projectData, projectPath) {
        const xcode = this.$pbxprojDomXcode.Xcode.open(projectPath);
        const signing = xcode.getSigning(projectData.projectName);
        if (signing !== undefined) {
            _.forEach(targetUuids, targetUuid => {
                if (signing.style === "Automatic") {
                    xcode.setAutomaticSigningStyleByTargetKey(targetUuid, signing.team);
                }
                else {
                    for (const config in signing.configurations) {
                        const signingConfiguration = signing.configurations[config];
                        xcode.setManualSigningStyleByTargetKey(targetUuid, signingConfiguration);
                        break;
                    }
                }
            });
        }
        xcode.save();
    }
    getTargetDirectories(folderPath) {
        return this.$fs.readDirectory(folderPath)
            .filter(fileName => {
            const filePath = path.join(folderPath, fileName);
            const stats = this.$fs.getFsStats(filePath);
            return stats.isDirectory() && !fileName.startsWith(".");
        });
    }
    setXcodeTargetBuildConfigurationProperties(properties, targetName, project) {
        properties.forEach(property => {
            const buildNames = property.buildNames || [BuildNames.debug, BuildNames.release];
            buildNames.forEach((buildName) => {
                project.addBuildProperty(property.name, property.value, buildName, targetName);
            });
        });
    }
    setConfigurationsFromJsonFile(jsonPath, targetUuid, targetName, project) {
        if (this.$fs.exists(jsonPath)) {
            const configurationJson = this.$fs.readJson(jsonPath) || {};
            _.forEach(configurationJson.frameworks, framework => {
                project.addFramework(framework, { target: targetUuid });
            });
            if (configurationJson.assetcatalogCompilerAppiconName) {
                project.addToBuildSettings("ASSETCATALOG_COMPILER_APPICON_NAME", configurationJson.assetcatalogCompilerAppiconName, targetUuid);
            }
            if (configurationJson.targetBuildConfigurationProperties) {
                const properties = [];
                _.forEach(configurationJson.targetBuildConfigurationProperties, (value, name) => properties.push({ value, name }));
                this.setXcodeTargetBuildConfigurationProperties(properties, targetName, project);
            }
        }
    }
}
exports.NativeTargetServiceBase = NativeTargetServiceBase;
