System.register([], function (_export, _context) {
  "use strict";

  var __SystemJSRewritingHack, SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER, addNamed;
  _export("default", function ({
    types: t,
    template
  }) {
    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
    return {
      name: 'polymorphic-identifiers',
      visitor: {
        Program: {
          enter(path, state) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
            function addCustomTemplate(file, name) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
              const declar = file.declarations[name];
              if (declar) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                return declar;
              }

              // const identifier = file.declarations[name] = file.addImport("polymorphic-identifiers", name, name);
              const identifier = file.declarations[name] = addNamed(path, name, "polymorphic-identifiers");
              identifier[SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER] = true;
              return identifier;
            }
            function hasDirective(path, name) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
              let foundDirective = false;
              path.traverse({
                Directive(path) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                  if (path.get("value").node.value === name) {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                    foundDirective = true;
                  }
                }
              });
              return foundDirective;
            }
            const shouldTransform = state.opts.executedIn === 'workspace' || hasDirective(path, "pi");
            if (!shouldTransform) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
              return;
            }
            path.traverse({
              TaggedTemplateExpression(path) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                if (path.node.visitedByPI) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                  return;
                }
                path.node.visitedByPI = true;
                const tagTemplate = template(`MAKE_REF(TAG_NODE, {
                thisReference: this,
                evalFunction: str => eval(str)
              })`);
                const tagPath = path.get('tag');
                tagPath.replaceWith(tagTemplate({
                  MAKE_REF: addCustomTemplate(state.file, 'makeRef'),
                  TAG_NODE: tagPath.node
                }));
                path.replaceWith(t.memberExpression(path.node, t.identifier('access')));
                const parentPath = path.parentPath;
                if (parentPath.isBinaryExpression() && path.parentKey === 'left' && parentPath.node.operator === "<<") {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                  parentPath.replaceWith(parentPath.node.right);

                  // find something we can embed an assignment expression in
                  const preStatementAncestor = parentPath.find(p => {
                    globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
                    const parent = p.parentPath;
                    return parent && (parent.isStatement() || parent.isArrowFunctionExpression() && p.parentKey === "body");
                  });
                  const assignment = t.assignmentExpression('=', path.node, preStatementAncestor.node);
                  assignment.loc = preStatementAncestor.node.loc;
                  preStatementAncestor.replaceWith(assignment);
                }
              }
            });
          }
        }
      }
    };
  });
  return {
    setters: [],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._src_client_reactive_babel_plugin_polymorphic_identifiers_babel_plugin_polymorphic_identifiers_js = _recorder_._src_client_reactive_babel_plugin_polymorphic_identifiers_babel_plugin_polymorphic_identifiers_js || {};
      SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER = 'SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER';
      Object.defineProperty(_recorder_._src_client_reactive_babel_plugin_polymorphic_identifiers_babel_plugin_polymorphic_identifiers_js, "SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
          return SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
          SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      addNamed = lively4babel.babelHelperModuleImports.addNamed;
      Object.defineProperty(_recorder_._src_client_reactive_babel_plugin_polymorphic_identifiers_babel_plugin_polymorphic_identifiers_js, "addNamed", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
          return addNamed;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/client/reactive/babel-plugin-polymorphic-identifiers/babel-plugin-polymorphic-identifiers.js"]++;
          addNamed = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
    }
  };
});