System.register(["src/client/preferences.js", "src/external/babel/babel7-helpers.js"], function (_export, _context) {
  "use strict";

  var Preferences, addNamed, __SystemJSRewritingHack, AEXPR_IDENTIFIER_NAME, FLAG_SHOULD_NOT_REWRITE_IDENTIFIER;
  _export("default", function ({
    types: t,
    template,
    traverse
  }) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
    const GENERATED_IMPORT_IDENTIFIER = Symbol("generated import identifier");
    function addCustomTemplate(file, name, path) {
      globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
      let declar = file.declarations[name];
      if (declar) return declar;

      // let identifier = file.declarations[name] = file.addImport("active-expression-proxies", name, name);
      let identifier = file.declarations[name] = addNamed(path, name, "active-expression-proxies", {
        nameHint: name
      });
      identifier[GENERATED_IMPORT_IDENTIFIER] = true;
      identifier[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true;
      return identifier;
    }
    return {
      visitor: {
        Program: {
          enter(path, state) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
            function hasDirective(path, name) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
              let foundDirective = false;
              path.traverse({
                Directive(path) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                  if (path.get("value").node.value === name) {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                    foundDirective = true;
                  }
                }
              });
              return foundDirective;
            }
            function shouldTransform() {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
              const proxyDirective = hasDirective(path, 'use proxies for aexprs');
              const proxyPreference = typeof Preferences == 'undefined' ? true : Preferences.get('UseProxiesForAExprs');
              const inWorkspace = state.opts.executedIn === 'workspace';
              const inFile = state.opts.executedIn === 'file';
              if (inWorkspace) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                return proxyPreference;
              } else if (inFile) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                return proxyDirective;
              }
              return true;
              // throw new Error('This should not be possible');
            }

            if (!shouldTransform()) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
              return;
            }
            function replaceNode(path, wrapType, unwrap = false) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
              // do not wrap the same object twice
              if (path.node.__already_transformed__) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                return;
              }
              path.node.__already_transformed__ = true;
              let transformed;
              if (unwrap) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                transformed = t.callExpression(addCustomTemplate(state.file, 'unwrap', path), [path.node]);
              } else {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                transformed = t.callExpression(addCustomTemplate(state.file, 'wrap', path), [t.stringLiteral(wrapType), path.node]);
              }
              path.replaceWith(transformed);
            }
            path.traverse({
              ObjectExpression(path) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                // do not replace objects in calls to Object.defineProperty
                try {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                  if (path.parent.callee.property.name === "defineProperty") {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                    return;
                  }
                } catch (e) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                } // Once we can use the new ecma script2020 syntax this try-catch can be replaced by optional chaining
                // https://iolap.com/2019/09/27/whats-next-for-javascript-top-5-new-features-for-2020/

                replaceNode(path, 'Object');
              },
              CallExpression(path) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                // unwrap proxies in Object.defineProperty 
                try {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                  if (path.node.callee.property.name === "defineProperty" && path.node.arguments[2].type === "Identifier") {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                    path.node.arguments[2].__should_unwrap__ = true;
                  }
                } catch (e) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                } // Once we can use the new ecma script2020 syntax this try-catch can be replaced by optional chaining
                // https://iolap.com/2019/09/27/whats-next-for-javascript-top-5-new-features-for-2020/
              },

              NewExpression(path) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                replaceNode(path, path.node.callee.name);
              },
              ArrayExpression(path) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                replaceNode(path, 'Array');
              },
              Identifier(path) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                if (path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER]) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                  return;
                }

                // Check for a call to undeclared aexpr:
                if (t.isCallExpression(path.parent) && path.node.name === AEXPR_IDENTIFIER_NAME && !path.scope.hasBinding(AEXPR_IDENTIFIER_NAME)) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                  //logIdentifier("call to aexpr", path);
                  path.replaceWith(addCustomTemplate(state.file, AEXPR_IDENTIFIER_NAME, path));
                  return;
                }
                try {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                  if (path.parent.callee.property.name === "defineProperty" && path.parent.arguments[2] === path.node) {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                    replaceNode(path, 'Proxy', true);
                  }
                } catch (e) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
                } // Once we can use the new ecma script2020 syntax this try-catch can be replaced by optional chaining
                // https://iolap.com/2019/09/27/whats-next-for-javascript-top-5-new-features-for-2020/
              }
            });
          }
        }
      }
    };
  });
  return {
    setters: [function (_srcClientPreferencesJs) {
      Preferences = _srcClientPreferencesJs.default;
    }, function (_srcExternalBabelBabel7HelpersJs) {
      addNamed = _srcExternalBabelBabel7HelpersJs.addNamed;
    }],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._src_client_reactive_babel_plugin_active_expression_proxies_index_js = _recorder_._src_client_reactive_babel_plugin_active_expression_proxies_index_js || {};
      Object.defineProperty(_recorder_._src_client_reactive_babel_plugin_active_expression_proxies_index_js, "Preferences", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          return Preferences;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          Preferences = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_reactive_babel_plugin_active_expression_proxies_index_js, "addNamed", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          return addNamed;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          addNamed = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      AEXPR_IDENTIFIER_NAME = 'aexpr';
      Object.defineProperty(_recorder_._src_client_reactive_babel_plugin_active_expression_proxies_index_js, "AEXPR_IDENTIFIER_NAME", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          return AEXPR_IDENTIFIER_NAME;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          AEXPR_IDENTIFIER_NAME = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      FLAG_SHOULD_NOT_REWRITE_IDENTIFIER = Symbol('FLAG: should not rewrite identifier');
      Object.defineProperty(_recorder_._src_client_reactive_babel_plugin_active_expression_proxies_index_js, "FLAG_SHOULD_NOT_REWRITE_IDENTIFIER", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          return FLAG_SHOULD_NOT_REWRITE_IDENTIFIER;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-active-expression-proxies/index.js"]++;
          FLAG_SHOULD_NOT_REWRITE_IDENTIFIER = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
    }
  };
});