"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const lib_1 = require("../../lib");
const sections = ['init', 'declaration'];
class RenderEngine {
    constructor(context) {
        this.context = context;
        this.componentContent = {
            'init': '',
            'declaration': ''
        };
    }
    toTemplate(element) {
        return Object.assign({ __element__: element }, element, { renderEngine: this, variableInput: (data) => {
                // console.log('GIVEN DATA', data);
                if (data.variableIndex !== undefined) {
                    return `subVariable(variables.${lib_1.variablenamify(data.variable)}, ${data.variableIndex})`;
                }
                else {
                    return `variables['${lib_1.variablenamify(data.variable)}']`;
                }
            }, variablenamify: lib_1.variablenamify });
    }
    renderHTML(element) {
        let html = this._render(element, '/content.html', false);
        for (let section of sections) {
            let ts = this._render(element, `/${section}.ts`, true);
            if (ts) {
                this.componentContent[section] += ts + `\r\n`;
            }
        }
        return html;
    }
    renderTS(section, element) {
        return this.componentContent[section];
    }
    _render(element, filename, allowMissingTemplate) {
        if (!element) {
            throw new schematics_1.SchematicsException(`Missing data to render`);
        }
        if (!element.type) {
            throw new schematics_1.SchematicsException(`Element to render does not have a "type" field. ${JSON.stringify(element)}`);
        }
        let templatePath = `files-includes/layout-builder/v1/components/${element.type}`;
        console.info(`rendering: "${element.type}" path: ${templatePath}`); //, 'data', element);
        const source = schematics_1.url(templatePath);
        // let rule = apply(
        //     source,
        //     [
        //         template({
        //             ...element,
        //             ...this.toTemplate()
        //         })
        //     ]
        // );
        // console.log('RENDERED SOURCE CONTENT: ', rule);
        let tree = source(this.context);
        // console.log('TREE: ', tree);
        if (!tree.exists(filename)) {
            if (allowMissingTemplate) {
                return '';
            }
            throw new schematics_1.SchematicsException(`Please create template file "${filename}" in "${templatePath}"`);
        }
        let templateData = Object.assign({}, this.toTemplate(element));
        let result = schematics_1.applyContentTemplate(templateData)(tree.get(filename));
        if (!result) {
            throw new schematics_1.SchematicsException(`applyContentTemplate returned null.`);
        }
        let fileContent = result.content.toString();
        // console.info(`Template result: `, fileContent);
        // let templateRule = template(templateData);
        // let exec = templateRule(tree, this.context);
        // let exec = rule(this.context);
        // if (exec instanceof Observable) {
        //     throw new SchematicsException('Async is not supported in render function');
        //     // tree = await exec.toPromise();
        // }
        // else {
        //     tree = exec as any;
        // }
        // // tree.visit((path) => {
        // //     console.info('Tree', path);
        // // })
        // let fileContentBuffer = tree.read(FILE_NAME);
        // if (!fileContentBuffer) {
        //     throw new SchematicsException(
        //         `Please create template file "${FILE_NAME}" in "${templatePath}"`
        //     )
        // }
        // let fileContent = fileContentBuffer.toString();
        // console.log('RENDERED SOURCE CONTENT: ', fileContent);
        return fileContent;
    }
}
exports.RenderEngine = RenderEngine;
//# sourceMappingURL=render-engine.js.map