"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.XcconfigService = void 0;
const path = require("path");
const constants_1 = require("../common/constants");
const yok_1 = require("../common/yok");
class XcconfigService {
    constructor($childProcess, $fs) {
        this.$childProcess = $childProcess;
        this.$fs = $fs;
    }
    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, "");
            }
            const escapedDestinationFile = destinationFile.replace(/'/g, "\\'");
            const escapedSourceFile = sourceFile.replace(/'/g, "\\'");
            const mergeScript = `require 'xcodeproj';
		userConfig = Xcodeproj::Config.new('${escapedDestinationFile}')
		existingConfig = Xcodeproj::Config.new('${escapedSourceFile}')
		userConfig.attributes.each do |key,|
  			existingConfig.attributes.delete(key) if (userConfig.attributes.key?(key) && existingConfig.attributes.key?(key))
		end
		userConfig.merge(existingConfig).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;
yok_1.injector.register("xcconfigService", XcconfigService);
