/*
    artifact generator: C:\my\wizzi\stfnbssl\wizzi\node_modules\wizzi-js\lib\artifacts\js\module\gen\main.js
    primary source IttfDocument: C:\my\wizzi\stfnbssl\wizzi\packages\wizzi-mtree\.wizzi\ittf\lib\jswizzi\jsWizziScriptCoder.js.ittf
*/
'use strict';
// generated by v6-wizzi-js.artifacts.js.module.main
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

/**
     Helper class for building JsWizzi scripts
    
*/
var JsWizziScriptCoder = (function () {
    function JsWizziScriptCoder() {
        _classCallCheck(this, JsWizziScriptCoder);
        this.stm = [];
        this.indentValue = 0;
        this.length = 0;
        this.line = '';
        this.map = [];
    }
    JsWizziScriptCoder.prototype.for = function(text, node) {
        if (this.line.length > 0) {
            this.w();
        }
        this.map[this.length] = node;
        this.w('for (' + text + ') {' + ' //' + node.id);
        this.indent();
    }
    JsWizziScriptCoder.prototype.if = function(text, node) {
        if (this.line.length > 0) {
            this.w();
        }
        this.map[this.length] = node;
        this.w('if (' + text + ') {' + ' //' + node.id);
        this.indent();
    }
    JsWizziScriptCoder.prototype.elif = function(text, node) {
        if (this.line.length > 0) {
            this.w();
        }
        this.map[this.length] = node;
        this.w('else if (' + text + ') {' + ' //' + node.id);
        this.indent();
    }
    JsWizziScriptCoder.prototype.else = function() {
        if (this.line.length > 0) {
            this.w();
        }
        this.w('else {');
        this.indent();
    }
    JsWizziScriptCoder.prototype.while = function(text, node) {
        if (this.line.length > 0) {
            this.w();
        }
        this.map[this.length] = node;
        this.w('while (' + text + ') {' + ' //' + node.id);
        this.indent();
    }
    JsWizziScriptCoder.prototype.end = function() {
        if (this.line.length > 0) {
            this.w();
        }
        this.deindent();
        this.w('}');
    }
    JsWizziScriptCoder.prototype.w = function(text, node) {
        var ind = spaces(this.indentValue * 4);
        this.stm.push(ind + this.line + (text || ''));
        if (node) {
            this.map[this.length] = node;
        }
        this.line = '';
        this.length++;
    }
    JsWizziScriptCoder.prototype.write = function(text) {
        this.line += (text || '');
    }
    JsWizziScriptCoder.prototype.indent = function() {
        this.indentValue++;
    }
    JsWizziScriptCoder.prototype.deindent = function() {
        this.indentValue = Math.max(0, (this.indentValue - 1));
    }
    JsWizziScriptCoder.prototype.toCode = function() {
        if (this.line.length > 0) {
            this.w();
        }
        return this.stm.join('\n');
    }
    JsWizziScriptCoder.prototype.getErrorLines = function(esprimaException) {
        var start = Math.max(0, (esprimaException.lineNumber - 4));
        var end = Math.min(this.stm.length, (esprimaException.lineNumber + 4));
        var ret = [];
        for (var i = start; i < end; i++) {
            ret.push(formatLineNumber(i + 1) + ' ' + this.stm[i]);
            if (i == esprimaException.lineNumber - 1) {
                var col = esprimaException.column;
                ret.push(spaces(col + 4) + '* ' + esprimaException.description);
            }
        }
        return ret;
    }
    JsWizziScriptCoder.prototype.getNodeFromScriptMap = function(line) {
        if (this.map[line]) {
            return this.map[line];
        }
        else if (this.map[line-1]) {
            return this.map[line-1];
        }
        else if (this.map[line+1]) {
            return this.map[line+1];
        }
        else {
            return null;
        }
    }
    return JsWizziScriptCoder;
})();

function spaces(num) {
    if (num < 1) {
        return '';
    }
    return Array(num + 1).join(" ");
}
function formatLineNumber(num) {
    if (num > 999) {
        return num;
    }
    if (num > 99) {
        return ('0' + num);
    }
    if (num > 9) {
        return ('00' + num);
    }
    return ('000' + num);
}
module.exports = JsWizziScriptCoder;
