// generator: wizzi-lab-artifatcs/lib/js/module/gen/main.js, utc time: Tue, 14 Feb 2017 15:38:34 GMT
var util = require('util');
var wizzi = require('wizzi-core').wizzi;
var statement = require('./statement');
var xfunction = require('./function');
var xclass = require('./class');
var module_es6 = require('./kinds/es6/module');
var xclass_es6 = require('./kinds/es6/class');
var xclass_react = require('./kinds/react/class');
var md = module.exports = {};
var myname = 'js.module.main';

md.gen = function(model, ctx, callback) {
    if (model.wzElement != 'module') {
        wizzi.errors.NodeError(myname + '.gen. Model wzElement must be "module". Found: ' + model.wzElement, model);
    }
    // log 'model.kind, ecma', model.kind, model.ecma
    ctx.__jskind = model.kind;
    ctx.__ecma = model.ecma;
    // log 'model', util.inspect(model, { depth: 1 })
    // log 'model.wzModelState', util.inspect(model.wzModelState)
    if (model.kind === 'nodejs.bin') {
        ctx.w('#!/usr/bin/env node');
    }
    ctx.w('// generator: /wizzi-labs/wizzi-lab-site/ittf/lib/artifacts/js/module/gen/main.js, utc time: ' + new Date().toUTCString());
    emitResources(model.resources, ctx);
    if (!model.no_strict) {
        ctx.w("'use strict';");
    }
    module_es6.gen(model, ctx);
    var i, i_len=model.statements.length, item;
    for (i=0; i<i_len; i++) {
        item = model.statements[i];
        if (item.wzElement === 'xclass') {
            // log 'ctx.__jskind', ctx.__jskind
            if (ctx.__jskind === 'react') {
                // log 'model.kind', model.kind
                xclass_react.gen(item, ctx);
            }
            else if (ctx.__ecma === 'es6') {
                xclass_es6.gen(item, ctx);
            }
            else {
                xclass.gen(item, ctx);
            }
            ctx.w("");
        }
        else if (item.wzElement === 'xfunction') {
            new xfunction.gen(item, ctx);
            ctx.w("");
        }
        else {
            statement.gen(item, ctx);
        }
    }
    callback(null, ctx);
};
function emitResources(requestedResources, ctx) {
    if (requestedResources.length > 0 && ctx.values.jsResources) {
        var resourceRepo = ctx.values.jsResources;
        resourceRepo.clearJsDependencies();
        var i, i_len=requestedResources.length, item;
        for (i=0; i<i_len; i++) {
            item = requestedResources[i];
            resourceRepo.addJsDependency(item.wzName);
        }
        resourceRepo.emitJsDependencies(ctx);
    }
}
