"use strict";
var util = require("util");
var path = require("path");
var fiberBootstrap = require("../fiber-bootstrap");
var MessagesService = (function () {
    function MessagesService($fs) {
        this.$fs = $fs;
        this._pathsToMessageJsonFiles = null;
        this._messageJsonFilesContentsCache = null;
        this._pathsToMessageJsonFiles = [this.pathToDefaultMessageJson];
    }
    Object.defineProperty(MessagesService.prototype, "pathToDefaultMessageJson", {
        get: function () {
            return path.join(__dirname, "..", "resources", "messages", "errorMessages.json");
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(MessagesService.prototype, "messageJsonFilesContents", {
        get: function () {
            if (!this._messageJsonFilesContentsCache || !this._messageJsonFilesContentsCache.length) {
                this.refreshMessageJsonContentsCache();
            }
            return this._messageJsonFilesContentsCache;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(MessagesService.prototype, "pathsToMessageJsonFiles", {
        get: function () {
            if (!this._pathsToMessageJsonFiles) {
                throw new Error("No paths to message json files provided.");
            }
            return this._pathsToMessageJsonFiles;
        },
        set: function (pathsToMessageJsonFiles) {
            this._pathsToMessageJsonFiles = pathsToMessageJsonFiles.concat(this.pathToDefaultMessageJson);
            this.refreshMessageJsonContentsCache();
        },
        enumerable: true,
        configurable: true
    });
    MessagesService.prototype.getMessage = function (id) {
        var _this = this;
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        var keys = id.split("."), result = this.getFormatedMessage.apply(this, [id].concat(args));
        _.each(this.messageJsonFilesContents, function (jsonFileContents) {
            var messageValue = _this.getMessageFromJsonRecursive(keys, jsonFileContents, 0);
            if (messageValue) {
                result = _this.getFormatedMessage.apply(_this, [messageValue].concat(args));
                return false;
            }
        });
        return result;
    };
    MessagesService.prototype.getMessageFromJsonRecursive = function (keys, jsonContents, index) {
        if (index >= keys.length) {
            return null;
        }
        var jsonValue = jsonContents[keys[index]];
        if (!jsonValue) {
            return null;
        }
        if (typeof jsonValue === "string") {
            return jsonValue;
        }
        return this.getMessageFromJsonRecursive(keys, jsonValue, index + 1);
    };
    MessagesService.prototype.refreshMessageJsonContentsCache = function () {
        var _this = this;
        fiberBootstrap.run(function () {
            _this._messageJsonFilesContentsCache = [];
            _.each(_this.pathsToMessageJsonFiles, function (path) {
                if (!_this.$fs.exists(path).wait()) {
                    throw new Error("Message json file " + path + " does not exist.");
                }
                _this._messageJsonFilesContentsCache.push(_this.$fs.readJson(path).wait());
            });
        });
    };
    MessagesService.prototype.getFormatedMessage = function (message) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return ~message.indexOf("%") ? util.format.apply(util, [message].concat(args)) : message;
    };
    return MessagesService;
})();
exports.MessagesService = MessagesService;
$injector.register("messagesService", MessagesService);
