"use strict";
var code_entity_1 = require("../codeGeneration/code-entity");
var code_printer_1 = require("../codeGeneration/code-printer");
var MessageContractGenerator = (function () {
    function MessageContractGenerator($fs, $messagesService, $staticConfig) {
        this.$fs = $fs;
        this.$messagesService = $messagesService;
        this.$staticConfig = $staticConfig;
        this.pendingModels = {};
    }
    MessageContractGenerator.prototype.generate = function (definitionsPath) {
        var _this = this;
        return (function () {
            var interfacesFile = new code_entity_1.Block();
            var implementationsFile = new code_entity_1.Block();
            definitionsPath = definitionsPath || "\"" + (_this.$staticConfig.CLIENT_NAME ? "" : "../") + ".d.ts\"";
            implementationsFile.writeLine("///<reference path=" + definitionsPath + "/>");
            implementationsFile.writeLine('"use strict";');
            implementationsFile.writeLine("//");
            implementationsFile.writeLine("// automatically generated code; do not edit manually!");
            implementationsFile.writeLine("//");
            implementationsFile.writeLine("");
            interfacesFile.writeLine("///<reference path=" + definitionsPath + "/>");
            interfacesFile.writeLine("//");
            interfacesFile.writeLine("// automatically generated code; do not edit manually!");
            interfacesFile.writeLine("//");
            var messagesClass = new code_entity_1.Block("export class Messages implements IMessages");
            var messagesInterface = new code_entity_1.Block("interface IMessages");
            _.each(_this.$messagesService.pathsToMessageJsonFiles, function (jsonFilePath) {
                var jsonContents = _this.$fs.readJson(jsonFilePath).wait(), implementationBlock = new code_entity_1.Block(), interfaceBlock = new code_entity_1.Block();
                _this.generateFileRecursive(jsonContents, "", implementationBlock, 0, { shouldGenerateInterface: false });
                _this.generateFileRecursive(jsonContents, "", interfaceBlock, 0, { shouldGenerateInterface: true });
                messagesClass.addBlock(implementationBlock);
                messagesInterface.addBlock(interfaceBlock);
            });
            interfacesFile.addBlock(messagesInterface);
            implementationsFile.addBlock(messagesClass);
            implementationsFile.writeLine("$injector.register('messages', Messages);");
            var codePrinter = new code_printer_1.CodePrinter();
            return {
                interfaceFile: codePrinter.composeBlock(interfacesFile),
                implementationFile: codePrinter.composeBlock(implementationsFile)
            };
        }).future()();
    };
    MessageContractGenerator.prototype.generateFileRecursive = function (jsonContents, propertyValue, block, depth, options) {
        var _this = this;
        _.each(jsonContents, function (val, key) {
            var newPropertyValue = propertyValue + key, separator = options.shouldGenerateInterface || depth ? ":" : "=", endingSymbol = options.shouldGenerateInterface || !depth ? ";" : ",";
            if (typeof val === "string") {
                var actualValue = options.shouldGenerateInterface ? "string" : "\"" + newPropertyValue + "\"";
                block.writeLine("" + key + separator + " " + actualValue + endingSymbol);
                newPropertyValue = propertyValue;
                return;
            }
            var newBlock = new code_entity_1.Block(key + " " + separator + " ");
            newBlock.endingCharacter = endingSymbol;
            _this.generateFileRecursive(val, newPropertyValue + ".", newBlock, depth + 1, options);
            block.addBlock(newBlock);
        });
    };
    return MessageContractGenerator;
})();
exports.MessageContractGenerator = MessageContractGenerator;
$injector.register("messageContractGenerator", MessageContractGenerator);
