"use strict";
const path = require("path");
const assert = require("assert");
class ProjectDataService {
    constructor($fs, $staticConfig, $errors, $logger) {
        this.$fs = $fs;
        this.$staticConfig = $staticConfig;
        this.$errors = $errors;
        this.$logger = $logger;
    }
    initialize(projectDir) {
        if (!this.projectFilePath) {
            this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
        }
    }
    getValue(propertyName) {
        return (() => {
            this.loadProjectFile().wait();
            return this.projectData ? this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName] : null;
        }).future()();
    }
    setValue(key, value) {
        return (() => {
            this.loadProjectFile().wait();
            if (!this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) {
                this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = Object.create(null);
            }
            this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][key] = value;
            this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait();
        }).future()();
    }
    removeProperty(propertyName) {
        return (() => {
            this.loadProjectFile().wait();
            delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName];
            this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait();
        }).future()();
    }
    removeDependency(dependencyName) {
        return (() => {
            this.loadProjectFile().wait();
            delete this.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName];
            this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait();
        }).future()();
    }
    loadProjectFile() {
        return (() => {
            assert.ok(this.projectFilePath, "Initialize method of projectDataService is not called.");
            if (!this.$fs.exists(this.projectFilePath).wait()) {
                this.$fs.writeJson(this.projectFilePath, {
                    "description": "NativeScript Application",
                    "license": "SEE LICENSE IN <your-license-filename>",
                    "readme": "NativeScript Application",
                    "repository": "<fill-your-repository-here>"
                }).wait();
            }
            let projectFileContent = this.$fs.readText(this.projectFilePath).wait();
            this.projectFileIndent = projectFileContent ? this.detectIndent(projectFileContent) : "\t";
            this.projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null);
        }).future()();
    }
    detectIndent(content) {
        const leadingSpace = content.match(/(^[ ]+)\S/m);
        if (leadingSpace) {
            return leadingSpace[1].length;
        }
        return "\t";
    }
}
ProjectDataService.DEPENDENCIES_KEY_NAME = "dependencies";
exports.ProjectDataService = ProjectDataService;
$injector.register("projectDataService", ProjectDataService);
