"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 constants_1 = require("../common/constants");
class XcconfigService {
    constructor($childProcess, $fs, $xcprojService) {
        this.$childProcess = $childProcess;
        this.$fs = $fs;
        this.$xcprojService = $xcprojService;
    }
    getPluginsXcconfigFilePaths(projectRoot) {
        return {
            [constants_1.Configurations.Debug.toLowerCase()]: this.getPluginsDebugXcconfigFilePath(projectRoot),
            [constants_1.Configurations.Release.toLowerCase()]: this.getPluginsReleaseXcconfigFilePath(projectRoot)
        };
    }
    getPluginsDebugXcconfigFilePath(projectRoot) {
        return path.join(projectRoot, "plugins-debug.xcconfig");
    }
    getPluginsReleaseXcconfigFilePath(projectRoot) {
        return path.join(projectRoot, "plugins-release.xcconfig");
    }
    mergeFiles(sourceFile, destinationFile) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.$fs.exists(destinationFile)) {
                this.$fs.writeFile(destinationFile, "");
            }
            yield this.$xcprojService.checkIfXcodeprojIsRequired();
            const escapedDestinationFile = destinationFile.replace(/'/g, "\\'");
            const escapedSourceFile = sourceFile.replace(/'/g, "\\'");
            const mergeScript = `require 'xcodeproj'; Xcodeproj::Config.new('${escapedDestinationFile}').merge(Xcodeproj::Config.new('${escapedSourceFile}')).save_as(Pathname.new('${escapedDestinationFile}'))`;
            yield this.$childProcess.exec(`ruby -e "${mergeScript}"`);
        });
    }
    readPropertyValue(xcconfigFilePath, propertyName) {
        if (this.$fs.exists(xcconfigFilePath)) {
            const text = this.$fs.readText(xcconfigFilePath);
            let property;
            let isPropertyParsed = false;
            text.split(/\r?\n/).forEach((line) => {
                line = line.replace(/\/(\/)[^\n]*$/, "");
                if (line.indexOf(propertyName) >= 0) {
                    const parts = line.split("=");
                    if (parts.length > 1 && parts[1]) {
                        property = parts[1].trim();
                        isPropertyParsed = true;
                        if (property[property.length - 1] === ';') {
                            property = property.slice(0, -1);
                        }
                    }
                }
            });
            if (isPropertyParsed) {
                return property;
            }
        }
        return null;
    }
}
exports.XcconfigService = XcconfigService;
$injector.register("xcconfigService", XcconfigService);
