text: /*
      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-web\.wizzi\ittf\examples\json\step_1.js.ittf
  */
  'use strict';
  //
  // Example skeleton specific for the 'wizzi-web' plugin package
  //
  /**
       Examples: json_step_1
      
  */
  var util = require('util');
  var path = require('path');
  var fs = require('fs');
  var stringify = require('json-stringify-safe');
  var wizziUtils = require('wizzi-utils');
  // local disk filesystem
  var file = wizziUtils.file;
  // virtual filesystem
  var vfile = require('wizzi-utils').vfile;
  // defaults to local disk filesystem
  var fsfile = vfile();
  // utilities
  var verify = wizziUtils.verify;
  var mocks = wizziUtils.mocks;
  var wizziRepo = require('wizzi-repo');
  var fileInfoByPath = wizziUtils.fileInfoByPath;
  var async = require('async');
  var wizzi = null;
  var jsIndex = require('../../index');
  function createWizziFactory(globalContext, callback) {
      if (wizzi == null) {
          // The wizzi package will be a previous version from wizzi/node_modules
          wizzi = require('wizzi');
      }
      console.log('"wizzi" package version', wizzi.version);
      wizzi.fsnoaclFactory({
          plugins: {
              items: [
                  './index'
              ], 
              pluginsBaseFolder: path.resolve(__dirname, '..', '..')
          }, 
          globalContext: globalContext || {}
      }, callback);
  }
  function loadMTree(ittfDocumentUri, context, callback) {
      createWizziFactory({}, function(err, wf) {
          if (err) {
              return callback(err);
          }
          wf.loadMTree(ittfDocumentUri, context, callback);
      });
  }
  function loadWizziModel(ittfDocumentUri, context, callback) {
      var fi = fileInfoByPath(ittfDocumentUri);
      createWizziFactory({}, function(err, wf) {
          if (err) {
              return callback(err);
          }
          wf.loadModel(fi.schema, ittfDocumentUri, {
              mTreeBuildUpContext: context, 
              globalContext: {}
          }, callback);
      });
  }
  function loadModelAndGenerateArtifact(ittfDocumentUri, context, artifactName, callback) {
      var fi = fileInfoByPath(ittfDocumentUri);
      createWizziFactory({}, function(err, wf) {
          if (err) {
              return callback(err);
          }
          wf.loadModelAndGenerateArtifact(ittfDocumentUri, {
              modelRequestContext: context, 
              artifactRequestContext: {}
          }, artifactName, function(err, artifact) {
              if (err) {
                  return callback(err);
              }
              callback(null, artifact);
          });
      });
  }
  function loadAndTransformModel(ittfDocumentUri, context, transformerName, callback) {
      var fi = fileInfoByPath(ittfDocumentUri);
      createWizziFactory({}, function(err, wf) {
          if (err) {
              return callback(err);
          }
          wf.loadModel(fi.schema, ittfDocumentUri, {
              mTreeBuildUpContext: context, 
              globalContext: {}
          }, function(err, wizziModel) {
              if (err) {
                  return callback(err);
              }
              wf.transformModel(wizziModel, transformerName, context, callback);
          });
      });
  }
  function executeWizziJob(ittfDocumentUri, context, callback) {
      createWizziFactory({}, function(err, wf) {
          if (err) {
              return callback(err);
          }
          wf.executeJob({
              name: path.basename(ittfDocumentUri), 
              path: ittfDocumentUri, 
              productionOptions: wizzi.productionOptions({
                  indentSpaces: 4, 
                  basedir: __dirname, 
                  verbose: 2
              }), 
              modelContext: context || {}, 
              jobContext: {}
          }, callback);
      });
  }
  function executeGenerateModelTypes(wfschemaIttfDocumentUri, outputPackagePath, wfschemaName, mTreeBuildUpContext, callback) {
      createWizziFactory({}, function(err, wf) {
          if (err) {
              return callback(err);
          }
          wf.generateModelTypes(wfschemaIttfDocumentUri, outputPackagePath, wfschemaName, mTreeBuildUpContext, callback);
      });
  }
  function getFiles(srcpath, schema) {
      return fs.readdirSync(srcpath).filter((file) => {
              return fs.lstatSync(path.join(srcpath, file)).isFile() && verify.endsWith(file, (schema === 'ittf' ? '.ittf' : '.' + schema + '.ittf'));
          })
      ;
  }
  var json_step_1 = function(step_callback) {
      heading1('EXAMPLE');
      heading2('basic json generations');
      const example_ittfs = [
          'basic', 
          'ittfprint', 
          'basic'
      ];
      async.mapSeries(example_ittfs, function(ittf, callback) {
          console.log('======================================================================================');
          console.log('generate file', ittf + '.json.ittf');
          console.log('--------------------------------------------------------------------------------------');
          loadModelAndGenerateArtifact(path.join(__dirname, 'ittf', ittf + '.json.ittf'), {}, "json/document", function(err, artifactText) {
              if (err) {
                  console.log('-------------------------------------------------------------------');
                  console.log('--- Test error ----------------------------------------------------');
                  console.log('err', err);
                  console.log('err.toString()', err.toString());
                  if (err.inner) {
                      console.log('err.inner.toString()', err.inner.toString());
                  }
                  console.log('-------------------------------------------------------------------');
                  console.log('-------------------------------------------------------------------');
                  throw 'Test error';
              }
              printValue(ittf, artifactText);
              return callback(null, ittf);
          });
      }, function(err, result) {
          if (err) {
              console.log('-------------------------------------------------------------------');
              console.log('--- Test error ----------------------------------------------------');
              console.log('err', err);
              console.log('err.toString()', err.toString());
              if (err.inner) {
                  console.log('err.inner.toString()', err.inner.toString());
              }
              console.log('-------------------------------------------------------------------');
              console.log('-------------------------------------------------------------------');
              throw 'Test error';
          }
          console.log(result);
      });
  };
  json_step_1.__name = 'json_step_1';
  function heading1(text) {
      console.log('');
      console.log('*'.repeat(120));
      console.log('** level 0 - step 1 - json_step_1 - ' + text);
      console.log('*'.repeat(120));
      console.log('');
  }
  function heading2(text) {
      console.log('');
      console.log('   ', '-'.repeat(100));
      console.log('   ','-- json_step_1 - ' + text);
      console.log('   ', '-'.repeat(100));
      console.log('');
  }
  function printArray(name, arr, fields, format) {
      if (format === 'dashes') {
          console.log('   ', '-'.repeat(100));
      }
      console.log('   ', '* array ' + name + ' : ');
      var i, i_items=arr, i_len=arr.length, item;
      for (i=0; i<i_len; i++) {
          item = arr[i];
          console.log('    {', i);
          var keys = fields || Object.keys(item);
          var j, j_items=keys, j_len=keys.length, k;
          for (j=0; j<j_len; j++) {
              k = keys[j];
              printValue(k, item[k]);
          }
      }
  }
  function printValue(k, v, format, p1) {
      console.log('--- value ------------------------------------------------------ start');
      if (format === 'dashes' || format === 'meter') {
          console.log('   ', '-'.repeat(100));
      }
      if (format === 'json') {
          v = stringify(v, null, 4);
      }
      if (verify.isNotEmpty(v)) {
          var lines = verify.splitLines(v, {
              numbered: true
          });
          if (lines.length === 1) {
              console.log('   ', k, ':', lines[0].text);
          }
          else {
              for (var i=0; i<lines.length; i++) {
                  if (i === 0) {
                      console.log('   ', k, ':', lines[0].numFmt, lines[0].text);
                  }
                  else {
                      console.log('   ', spaces(k.length+1), ' ', lines[i].numFmt, lines[i].text);
                  }
              }
          }
      }
      else if (verify.isObject(v)) {
          console.log('   ', k, ':', util.inspect(v));
      }
      else {
          console.log('   ', k, ':', v);
      }
      if (format === 'meter') {
          meterLine(p1, '     ' + new Array(1 + k.length).join(' '));
      }
      console.log('--- value ------------------------------------------------------ end');
  }
  function printObject(k, v, format, p1) {
      console.log('--- object ------------------------------------------------------ start');
      if (format === 'dashes' || format === 'meter') {
          console.log('   ', '-'.repeat(100));
      }
      console.log('   ', k, '{');
      __printObject(v, 2, 6);
      if (format === 'meter') {
          meterLine(p1, '     ' + new Array(1 + k.length).join(' '));
      }
      console.log('--- object ------------------------------------------------------ end');
  }
  function __printObject(v, level, limit) {
      if (level < limit) {
          var indent = new Array(1 + level * 4).join(' ');
          var prop;
          for (var k in v) {
              prop = v[k];
              if (verify.isObject(prop)) {
                  console.log(indent, k, '{');
                  __printObject(prop, level+1, limit);
              }
              else if (verify.isFunction(prop)) {
                  console.log(indent, k, 'function');
              }
              else if (verify.isArray(prop)) {
                  console.log(indent, k, '[');
                  var indent2 = new Array(1 + (level+1) * 4).join(' ');
                  var i, i_items=prop, i_len=prop.length, item;
                  for (i=0; i<i_len; i++) {
                      item = prop[i];
                      if (verify.isObject(item)) {
                          __printObject(item, level+1, limit);
                      }
                      else if (verify.isFunction(item)) {
                          console.log(indent2, 'function');
                      }
                      else {
                          console.log(indent2, item);
                      }
                  }
              }
              else {
                  console.log(indent, k, prop);
              }
          }
      }
  }
  function printLines(lines) {
      console.log('lines from liner ----------------------------------- start');
      var i, i_items=lines, i_len=lines.length, l;
      for (i=0; i<i_len; i++) {
          l = lines[i];
          console.log('line', 'indent: ' + l.indent, 'r,c: ' + l.row + ',' + l.col, l.name + ' / ' + l.value, (l.hasMacro ? 'hasMacro' : ''), (l.tagSuffix ? 'tagSuffix: ' + l.tagSuffix : ''));
      }
      console.log('lines from liner ------------------------------------- end');
  }
  function printNodes_deep(n, indent) {
      var mTreeModel = n.model || n.mTreeBrick;
      console.log(' ', new Array(indent).join('  '), 'r,c: ' + n.row + ',' + n.col, n.name + ' / ' + n.value, (n.hasMacro ? 'hasMacro' : ''), (n.tagSuffix ? 'tagSuffix: ' + n.tagSuffix : ''), (mTreeModel.$params ? '$params: ' + mTreeModel.$params : ''), (mTreeModel.sourceKey ? 'sk: ' + mTreeModel.sourceKey : ''));
      var i, i_items=n.children, i_len=n.children.length, c;
      for (i=0; i<i_len; i++) {
          c = n.children[i];
          printNodes_deep(c, indent + 1);
      }
  }
  function printNodes(nodes, title) {
      console.log('--- nodes ' + (title || '') + ' ------------------------------------------------- start');
      if (nodes.length != 1) {
          console.log('Invalid nodes array, must be of length == 1');
      }
      else {
          // log 'nodes.mTreeBrick', nodes[0].mTreeBrick
          // log 'nodes[0]', nodes[0]
          var mTreeModel = nodes[0].model || nodes[0].mTreeBrick;
          if (mTreeModel) {
              console.log(' ', 'nodes.uri', mTreeModel.uri);
              console.log(' ', 'nodes.$schema', mTreeModel.$schema);
              console.log(' ', 'nodes.sourceKey', mTreeModel.sourceKey);
              if (mTreeModel.$params) {
                  console.log(' ', 'nodes.$params', mTreeModel.$params);
              }
              if (mTreeModel.frontMatter) {
                  console.log(' ', 'nodes.frontMatter', mTreeModel.frontMatter);
              }
          }
          printNodes_deep(nodes[0], 1);
      }
      console.log('--- nodes ' + (title || '') + ' --------------------------------------------------- end');
  }
  function printEvaluatedNodes_deep(n, indent) {
      console.log(' ', new Array(indent).join('  '), 'r,c: ' + n.r + ',' + n.c, n.n + ' / ' + n.v, 'sk: ' + n.s, 'u: ' + n.u);
      var i, i_items=n.children, i_len=n.children.length, c;
      for (i=0; i<i_len; i++) {
          c = n.children[i];
          printEvaluatedNodes_deep(c, indent + 1);
      }
  }
  function printEvaluatedNodes(evaluated, title) {
      console.log('--- evaluated nodes ' + (title || '') + ' ------------------------------------------------- start');
      if (evaluated && evaluated.nodes && evaluated.nodes.length == 1) {
          if (evaluated.frontMatter) {
              console.log(' ', 'evaluated.frontMatter', evaluated.frontMatter);
          }
          printEvaluatedNodes_deep(evaluated.nodes[0], 1);
      }
      else {
          console.log('Invalid evaluated object', evaluated);
      }
      console.log('--- evaluated nodes ' + (title || '') + ' --------------------------------------------------- end');
  }
  function spaces(len) {
      return new Array(len).join(' ');
  }
  function meterLine(len, indent) {
      var sb = [];
      var numW = len < 10 ? 1 : ( len < 100 ? 2 : 3 );
      var x;
      for (var i=0; i<numW; i++) {
          for (var j=0; j<len; j++) {
              x = formatNum(j, numW);
              sb.push(x.substr(i,1));
          }
          console.log(indent, sb.join(''));
          sb = [];
      }
  }
  function formatNum(num, len) {
      var x = num.toString();
      return new Array(1 + len-x.length).join(' ') + x;
  }
  module.exports = json_step_1;
  if (typeof require != 'undefined' && require.main === module) {
      json_step_1();
  }
  