"use strict";
const path = require("path");
const assert = require("assert");
class ProjectDataService {
    constructor($fs, $staticConfig) {
        this.$fs = $fs;
        this.$staticConfig = $staticConfig;
    }
    initialize(projectDir) {
        if (!this.projectFilePath) {
            this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
        }
    }
    getValue(propertyName) {
        this.loadProjectFile();
        return this.projectData ? this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName] : null;
    }
    setValue(key, value) {
        this.loadProjectFile();
        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);
    }
    removeProperty(propertyName) {
        this.loadProjectFile();
        delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName];
        this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent);
    }
    removeDependency(dependencyName) {
        this.loadProjectFile();
        delete this.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName];
        this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent);
    }
    loadProjectFile() {
        assert.ok(this.projectFilePath, "Initialize method of projectDataService is not called.");
        if (!this.$fs.exists(this.projectFilePath)) {
            this.$fs.writeJson(this.projectFilePath, {
                "description": "NativeScript Application",
                "license": "SEE LICENSE IN <your-license-filename>",
                "readme": "NativeScript Application",
                "repository": "<fill-your-repository-here>"
            });
        }
        let projectFileContent = this.$fs.readText(this.projectFilePath);
        this.projectFileIndent = projectFileContent ? this.detectIndent(projectFileContent) : "\t";
        this.projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null);
    }
    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);
