"disable examples";
"enable deepeval";

/*MD # JavaScript Parsing / Semantics

(used in fileindex etc)

MD*/

/*MD 

# Meta Nodes #Babylonian #Programming

- Issue: focus lost while using text elements in widgets ....
- saving, both losing changes when closing... and no feedback
  - even if not saving... we need some kind of auto backup for recovery.... maybe in local storage
- feedback while execution
- typing stuttering while example execution
  - Solution: remote/external non-blocking execution.... tools need to work on remote objects (LSP anybody?)
  - Benefit: might bring Babylonian to other languages + heavy loads... e.g. number crunching examples
- probes text annotations can extent while writing (probe on m[1]) breaks probe...
- external tools / UX of "Customs instance Editor" bad...
- shortcut support for probes and example creation? because... my hands are on the keyboard... UX baseline: like select + Ctrl+P
- multiline text editing in code mirror text widgets (e.g. examples..)
- long running examples may kill the system

MD*/
System.register(["src/plugin-babel.js", "src/external/babel/babel7default.js"], function (_export, _context) {
  "use strict";

  var parseForAST, loadPlugins, babelDefault, __SystemJSRewritingHack, babel, t;
  /* Context: {"context":{"prescript":"","postscript":""},"customInstances":[{"id":"a009_c6f3_e94e","name":"f1","code":"return `function f(x) { return x * 2}`;"},{"id":"bf7e_1c8f_26da","name":"metainfo","code":"return `/\\*\\nComponentBin: #Tool #Debugging #bar\\n\\*\\/ class Foo {}`;"}]} */function parseSource(filename, source) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
    try {
      globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
      return parseForAST(source).ast;
    } catch (e) {
      globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
      console.log('FileIndex, could not parse: ' + filename, e);
      return null;
    }
  }
  function parseModuleSemanticsFromSource(filename, source) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
    var ast = parseSource(filename, source);
    if (ast) {
      globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
      return parseModuleSemantics(ast);
    }
  }
  function parseModuleSemantics(ast) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
    let classes = [];
    let functions = [];
    let dependencies = [];
    let importDeclarations = new Map();
    let functionExports = [];
    let classExports = [];
    let unboundIdentifiers = [];
    let comments = [];

    /*MD ## Comments 
    
    ```javascript
      import * as javascript from "https://lively-kernel.org/lively4/lively4-jens/src/client/javascript.js"
    var url = "https://lively-kernel.org/lively4/lively4-jens/src/components/tools/lively-sync.js";
    (async () => {
      var source = await fetch(url).then(r => r.text())
      javascript.parseModuleSemanticsFromSource(url, source)
    
    })()
    ```
    
    MD*/

    babel.traverse(ast, {
      Program(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        for (let ea of path.node.body) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          let metaInfos = ['Keywords', 'Authors'];
          if (ea.leadingComments) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
            for (let comment of ea.leadingComments) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
              let info = {
                start: comment.start,
                end: comment.end
              };
              if (comment.value) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                let lines = comment.value.split("\n");
                info.firstline = lines[0];
                for (let line of lines) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                  let m = line.match(/([A-Za-z][A-Za-z0-9]*): (.*)/);
                  if (m) {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                    let key = m[1];
                    let value = m[2];
                    if (metaInfos.includes(key)) {
                      globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                      info[key] = value.split(/ +/);
                    }
                  }
                }
              }
              comments.push(info);
            }
          }
        }
      },
      ImportDeclaration(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        if (path.node.source && path.node.source.value) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          let specifierNames = [];
          let moduleUrl = path.node.source.value;
          if (path.node.specifiers) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
            path.node.specifiers.forEach(function (item) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
              if (item.type === "ImportNamespaceSpecifier") {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                specifierNames.push('*');
                importDeclarations.set('*', moduleUrl);
              } else {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                specifierNames.push(item.local.name);
                importDeclarations.set(item.local.name, moduleUrl);
              }
            });
          }
          let dependency = {
            url: path.node.source.value,
            names: specifierNames
          };
          dependencies.push(dependency);
        }
      },
      FunctionDeclaration(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        if (path.node.id) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          let funcNode = path.node;
          let func = {
            name: funcNode.id.name,
            start: funcNode.start,
            // start byte 
            end: funcNode.end,
            // end byte
            loc: funcNode.loc.end.line - funcNode.loc.start.line + 1,
            kind: funcNode.kind,
            static: funcNode.static,
            leadingComments: funcNode.leadingComments
          };
          functions.push(func);
        }
      },
      ClassDeclaration(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        let superClassName = '';
        let superClassUrl = '';
        if (path.node.id) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          let clazz = {
            name: path.node.id.name,
            start: path.node.start,
            // start byte 
            end: path.node.end,
            // end byte
            loc: path.node.loc.end.line - path.node.loc.start.line + 1
          };
          superClassName = path.node.superClass ? path.node.superClass.name : '';
          superClassUrl = importDeclarations.get(superClassName);
          let methods = [];
          if (path.node.body.body) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
            path.node.body.body.forEach(function (item) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
              if (item.type === "ClassMethod") {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
                let method = {
                  name: item.key.name,
                  loc: item.loc.end.line - item.loc.start.line + 1,
                  start: item.start,
                  kind: item.kind,
                  static: item.static,
                  end: item.end,
                  leadingComments: item.leadingComments
                };
                methods.push(method);
              }
            });
          }
          clazz.methods = methods;
          clazz.superClassName = superClassName;
          clazz.superClassUrl = superClassUrl;
          classes.push(clazz);
        }
      },
      ExportNamedDeclaration(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        if (t.isFunctionDeclaration(path.node.declaration)) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          functionExports.push(path.node.declaration.id.name);
        }
        if (t.isClassDeclaration(path.node.declaration)) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          classExports.push(path.node.declaration.id.name);
        }
      },
      Identifier(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        if (!hasASTBinding(path)) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          unboundIdentifiers.push(path.node.name);
        }
      }
    });
    // #TODO #BabylonianProgramming #Warning... adding a probe here breaks babylonian programming... because of to much data? 
    // #Research besides time, there are cutoffs needed for to heavy tracing loads.... and one time tasks that are to heavy like serializing the whole world, or going in a cycle? 
    // The bug was caused by stupid client code... (adding to data structure while iterating it) but it should not break the tools
    return {
      classes,
      functions,
      dependencies,
      functionExports,
      classExports,
      unboundIdentifiers,
      comments
    };
  }
  function getBindingDeclarationIdentifierPath(binding) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
    return getFirstSelectedIdentifierWithName(binding.path, binding.identifier.name);
  }
  function getFirstSelectedIdentifierWithName(startPath, name) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
    if (t.isIdentifier(startPath.node, {
      name: name
    })) {
      globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
      return startPath;
    }
    var first;
    startPath.traverse({
      Identifier(path) {
        globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
        if (!first && t.isIdentifier(path.node, {
          name: name
        })) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          first = path;
          path.stop();
        }
      }
    });
    return first;
  }
  function hasASTBinding(identifier) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
    if (!identifier.scope.hasBinding(identifier.node.name)) return false;
    const binding = identifier.scope.getBinding(identifier.node.name);
    if (!binding) return false;
    const identifierPaths = [...new Set([getBindingDeclarationIdentifierPath(binding), ...binding.referencePaths, ...binding.constantViolations.map(cv => getFirstSelectedIdentifierWithName(cv, binding.identifier.name))])];
    return identifierPaths.includes(identifier);
  }
  _export({
    parseSource: parseSource,
    parseModuleSemanticsFromSource: parseModuleSemanticsFromSource,
    parseModuleSemantics: parseModuleSemantics
  });
  return {
    setters: [function (_srcPluginBabelJs) {
      parseForAST = _srcPluginBabelJs.parseForAST;
      loadPlugins = _srcPluginBabelJs.loadPlugins;
    }, function (_srcExternalBabelBabel7defaultJs) {
      babelDefault = _srcExternalBabelBabel7defaultJs.default;
    }],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._src_client_javascript_js = _recorder_._src_client_javascript_js || {};
      Object.defineProperty(_recorder_._src_client_javascript_js, "hasASTBinding", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return hasASTBinding;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          hasASTBinding = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "getFirstSelectedIdentifierWithName", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return getFirstSelectedIdentifierWithName;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          getFirstSelectedIdentifierWithName = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "getBindingDeclarationIdentifierPath", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return getBindingDeclarationIdentifierPath;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          getBindingDeclarationIdentifierPath = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "parseModuleSemantics", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return parseModuleSemantics;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          _export("parseModuleSemantics", parseModuleSemantics = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "parseModuleSemanticsFromSource", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return parseModuleSemanticsFromSource;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          _export("parseModuleSemanticsFromSource", parseModuleSemanticsFromSource = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "parseSource", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return parseSource;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          _export("parseSource", parseSource = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "loadPlugins", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return loadPlugins;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          loadPlugins = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "parseForAST", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return parseForAST;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          parseForAST = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_javascript_js, "babelDefault", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return babelDefault;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          babelDefault = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      babel = babelDefault.babel;
      Object.defineProperty(_recorder_._src_client_javascript_js, "babel", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return babel;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          babel = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      t = babel.types;
      Object.defineProperty(_recorder_._src_client_javascript_js, "t", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          return t;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/javascript.js"]++;
          t = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
    }
  };
});