var ansispan = function (str) {
  Object.keys(ansispan.foregroundColors).forEach(function (ansi) {
    var span = '<span style="color: ' + ansispan.foregroundColors[ansi] + '">';
    str = str.replace(
      new RegExp('\033\\[' + ansi + 'm', 'g'),
      span
    ).replace(
      new RegExp('\033\\[0;' + ansi + 'm', 'g'),
      span
    );
  });
  str = str.replace(/\033\[1m/g, '<b>').replace(/\033\[22m/g, '</b>');
  str = str.replace(/\033\[3m/g, '<i>').replace(/\033\[23m/g, '</i>');

  str = str.replace(/\033\[m/g, '</span>');
  str = str.replace(/\033\[0m/g, '</span>');
  return str.replace(/\033\[39m/g, '</span>')
    .replace(/\[90m/g, '  ');
};

ansispan.foregroundColors = {
  '30': 'black',
  '31': 'red',
  '32': 'green',
  '33': 'yellow',
  '34': 'blue',
  '35': 'purple',
  '36': 'cyan',
  '37': 'white'
};

;(function(Path){
  'use strict';


  var log = console.log;
  /*var log = function() {
    console.log.apply(console, ['Define'].concat([].slice.call(arguments)));
  };*/



  window.onerror = function(message, source, lineno, colno, error) {
    fetch(source)
      .then(function(resp) {
        return resp.text();
      })
      .then(function(data) {
        if(data.match(/^(unknown|Error|error)/)!==null){
          var erDiv = D.div({cls: 'error-block', renderTo: document.body},
            D.input({attr: {type: 'button', value: 'Hide'}, on: {click: function() {
                  erDiv.parentNode.removeChild(erDiv);
            }}}))
          var dataEl = D.div();
          erDiv.appendChild(dataEl)
          dataEl.innerHTML = ansispan(data)
        }
      })
      .catch()
    console.error(source, message, error);
  };

  var head = document.getElementsByTagName( 'head' )[ 0 ];
  var cssLoader = function( fileName ){
    var link = document.createElement( 'link' );
    link.setAttribute( 'rel', 'stylesheet' );
    link.setAttribute( 'type', 'text/css' );
    link.setAttribute( 'href', fileName );
    head.appendChild( link );
    return true;
  };
  var jsLoader = function( fileName ){

    var script = document.createElement( 'script' );
    script.setAttribute( 'type', script.type = 'text/javascript' );
    script.onload = function(a,b,c){
      /*if(fileName.indexOf('Fields')>-1){
        //debugger
        console.log('akkk',a,b,c)
      }*/
    };

    script.onerror = function(a,b,c){
      if(fileName.indexOf('Fields')>-1)debugger
      console.log('kkk',a,b,c)
    };
    script.setAttribute( 'src', script.src = fileName );
    head.appendChild( script );
  };

  var InstantLoaders = [
    { name: '.scss', loader: cssLoader },
    { name: '.css', loader: cssLoader },
    { name: '.jsx', loader: jsLoader },
    { name: '.js', loader: jsLoader },
    { name: '.tsx', loader: jsLoader },
    { name: '.ts', loader: jsLoader },
    { name: '.svg', loader: jsLoader },
  ];

  var definitions = {};
  var waiting = {};

  var resolve = function(base, file) {
    return Path.resolve(Path.dirname(base), file);
  };
  var _define = function(name) {
    var definition = definitions[name];
    if(definition.notResolved === 0){
      definition.fn.apply(null, definition.deps.map(function(dep) {
        return dep === 'exports' ? definition.exports : definitions[dep].exports
      }));

      (waiting[definition.fileName] || []).forEach(function(name) {
        definitions[name].notResolved--;
        _define(name)
      });
    }
  };
  window.define = function(fileName, deps, fn) {
    fileName = Path.trim(fileName);
    deps = deps.map(function(dep) {
      return dep === 'exports' ? 'exports': resolve(fileName, dep);
    });

    if(!(fileName in definitions) || definitions[fileName].notResolved !== 0){
      definitions[ fileName ] = { fileName: fileName, deps: deps, fn: fn, exports: {} };

      var notResolved = 0;
      for( var i = 0, _i = deps.length; i < _i; i++ ){
        const dep = deps[ i ];
        if( dep === 'exports' )
          continue;
        var skip = false;
        if( !( dep in definitions ) ){
          var matched = false;
          for( var j = 0, _j = InstantLoaders.length; j < _j; j++ ){
            const instantLoader = InstantLoaders[ j ];
            if( dep.substr( -instantLoader.name.length ).toLowerCase() === instantLoader.name ){
              if( !( dep in definitions ) ){
                definitions[ dep ] = { exports: {} };
                if( instantLoader.loader( dep ) ){
                  skip = true;
                  definitions[ dep ].notResolved = 0;
                }else{
                  definitions[ dep ].loading = true;
                }
                matched = true;
              }
              break;
            }
          }
          if(!matched){
            if( !( dep in definitions ) ){
              definitions[ dep ] = { exports: {} };
              if( jsLoader( dep ) ){
                skip = true;
              }else{
                definitions[ dep ].loading = true;
              }
              matched = true;
            }
          }

        }else{
          skip = definitions[ dep ].notResolved === 0;
        }
        if( !skip ){
          ( waiting[ dep ] || ( waiting[ dep ] = [] ) ).push( fileName );
          notResolved++;
        }
      }
      definitions[ fileName ].notResolved = notResolved;
    }else{
      definitions[ fileName ].fn = fn;
    }
    _define(fileName)
  };
  window.define.definitions = definitions;
  window.define.waiting = waiting;

})(window.__Path);