"use strict";
const code_entity_1 = require("../codeGeneration/code-entity");
const code_printer_1 = require("../codeGeneration/code-printer");
class MessageContractGenerator {
    constructor($fs, $messagesService, $staticConfig) {
        this.$fs = $fs;
        this.$messagesService = $messagesService;
        this.$staticConfig = $staticConfig;
        this.pendingModels = {};
    }
    generate() {
        return (() => {
            let interfacesFile = new code_entity_1.Block();
            let implementationsFile = new code_entity_1.Block();
            implementationsFile.writeLine("//");
            implementationsFile.writeLine("// automatically generated code; do not edit manually!");
            implementationsFile.writeLine("//");
            implementationsFile.writeLine("");
            interfacesFile.writeLine("//");
            interfacesFile.writeLine("// automatically generated code; do not edit manually!");
            interfacesFile.writeLine("//");
            let messagesClass = new code_entity_1.Block("export class Messages implements IMessages");
            let messagesInterface = new code_entity_1.Block("interface IMessages");
            _.each(this.$messagesService.pathsToMessageJsonFiles, jsonFilePath => {
                let 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);");
            let codePrinter = new code_printer_1.CodePrinter();
            return {
                interfaceFile: codePrinter.composeBlock(interfacesFile),
                implementationFile: codePrinter.composeBlock(implementationsFile)
            };
        }).future()();
    }
    generateFileRecursive(jsonContents, propertyValue, block, depth, options) {
        _.each(jsonContents, (val, key) => {
            let newPropertyValue = propertyValue + key, separator = options.shouldGenerateInterface || depth ? ":" : "=", endingSymbol = options.shouldGenerateInterface || !depth ? ";" : ",";
            if (typeof val === "string") {
                let actualValue = options.shouldGenerateInterface ? "string" : `"${newPropertyValue}"`;
                block.writeLine(`${key}${separator} ${actualValue}${endingSymbol}`);
                newPropertyValue = propertyValue;
                return;
            }
            let newBlock = new code_entity_1.Block(`${key} ${separator} `);
            newBlock.endingCharacter = endingSymbol;
            this.generateFileRecursive(val, newPropertyValue + ".", newBlock, depth + 1, options);
            block.addBlock(newBlock);
        });
    }
}
exports.MessageContractGenerator = MessageContractGenerator;
$injector.register("messageContractGenerator", MessageContractGenerator);
