"use strict";
const helpers = require("./../common/helpers");
class PluginVariablesService {
    constructor($errors, $pluginVariablesHelper, $projectData, $projectDataService, $prompter, $fs) {
        this.$errors = $errors;
        this.$pluginVariablesHelper = $pluginVariablesHelper;
        this.$projectData = $projectData;
        this.$projectDataService = $projectDataService;
        this.$prompter = $prompter;
        this.$fs = $fs;
    }
    getPluginVariablePropertyName(pluginName) {
        return `${pluginName}-${PluginVariablesService.PLUGIN_VARIABLES_KEY}`;
    }
    savePluginVariablesInProjectFile(pluginData) {
        return (() => {
            let values = Object.create(null);
            this.executeForAllPluginVariables(pluginData, (pluginVariableData) => (() => {
                let pluginVariableValue = this.getPluginVariableValue(pluginVariableData).wait();
                this.ensurePluginVariableValue(pluginVariableValue, `Unable to find value for ${pluginVariableData.name} plugin variable from ${pluginData.name} plugin. Ensure the --var option is specified or the plugin variable has default value.`);
                values[pluginVariableData.name] = pluginVariableValue;
            }).future()()).wait();
            if (!_.isEmpty(values)) {
                this.$projectDataService.initialize(this.$projectData.projectDir);
                this.$projectDataService.setValue(this.getPluginVariablePropertyName(pluginData.name), values).wait();
            }
        }).future()();
    }
    removePluginVariablesFromProjectFile(pluginName) {
        this.$projectDataService.initialize(this.$projectData.projectDir);
        return this.$projectDataService.removeProperty(this.getPluginVariablePropertyName(pluginName));
    }
    interpolatePluginVariables(pluginData, pluginConfigurationFilePath) {
        return (() => {
            let pluginConfigurationFileContent = this.$fs.readText(pluginConfigurationFilePath).wait();
            this.executeForAllPluginVariables(pluginData, (pluginVariableData) => (() => {
                this.ensurePluginVariableValue(pluginVariableData.value, `Unable to find the value for ${pluginVariableData.name} plugin variable into project package.json file. Verify that your package.json file is correct and try again.`);
                pluginConfigurationFileContent = this.interpolateCore(pluginVariableData.name, pluginVariableData.value, pluginConfigurationFileContent);
            }).future()()).wait();
            this.$fs.writeFile(pluginConfigurationFilePath, pluginConfigurationFileContent).wait();
        }).future()();
    }
    interpolateAppIdentifier(pluginConfigurationFilePath) {
        return (() => {
            let pluginConfigurationFileContent = this.$fs.readText(pluginConfigurationFilePath).wait();
            let newContent = this.interpolateCore("nativescript.id", this.$projectData.projectId, pluginConfigurationFileContent);
            this.$fs.writeFile(pluginConfigurationFilePath, newContent).wait();
        }).future()();
    }
    interpolate(pluginData, pluginConfigurationFilePath) {
        return (() => {
            this.interpolatePluginVariables(pluginData, pluginConfigurationFilePath).wait();
            this.interpolateAppIdentifier(pluginConfigurationFilePath).wait();
        }).future()();
    }
    interpolateCore(name, value, content) {
        return content.replace(new RegExp(`{${name}}`, "gi"), value);
    }
    ensurePluginVariableValue(pluginVariableValue, errorMessage) {
        if (!pluginVariableValue) {
            this.$errors.failWithoutHelp(errorMessage);
        }
    }
    getPluginVariableValue(pluginVariableData) {
        return (() => {
            let pluginVariableName = pluginVariableData.name;
            let value = this.$pluginVariablesHelper.getPluginVariableFromVarOption(pluginVariableName);
            if (value) {
                value = value[pluginVariableName];
            }
            else {
                value = pluginVariableData.defaultValue;
                if (!value && helpers.isInteractive()) {
                    let promptSchema = {
                        name: pluginVariableName,
                        type: "input",
                        message: `Enter value for ${pluginVariableName} variable:`,
                        validate: (val) => !!val ? true : 'Please enter a value!'
                    };
                    let promptData = this.$prompter.get([promptSchema]).wait();
                    value = promptData[pluginVariableName];
                }
            }
            return value;
        }).future()();
    }
    executeForAllPluginVariables(pluginData, action) {
        return (() => {
            let pluginVariables = pluginData.pluginVariables;
            let pluginVariablesNames = _.keys(pluginVariables);
            _.each(pluginVariablesNames, pluginVariableName => action(this.createPluginVariableData(pluginData, pluginVariableName).wait()).wait());
        }).future()();
    }
    createPluginVariableData(pluginData, pluginVariableName) {
        return (() => {
            let variableData = pluginData.pluginVariables[pluginVariableName];
            variableData.name = pluginVariableName;
            this.$projectDataService.initialize(this.$projectData.projectDir);
            let pluginVariableValues = this.$projectDataService.getValue(this.getPluginVariablePropertyName(pluginData.name)).wait();
            variableData.value = pluginVariableValues ? pluginVariableValues[pluginVariableName] : undefined;
            return variableData;
        }).future()();
    }
}
PluginVariablesService.PLUGIN_VARIABLES_KEY = "variables";
exports.PluginVariablesService = PluginVariablesService;
$injector.register("pluginVariablesService", PluginVariablesService);
