System.register(["reactive-jsx", "src/client/strings.js"], function (_export, _context) {
  "use strict";

  var _childSpread, _childText, _childElement, _attributeStringLiteral, _childExpression, _children, _attributeExpression, _attributes, _element, Strings;

  return {
    setters: [function (_reactiveJsx) {
      _childSpread = _reactiveJsx.childSpread;
      _childText = _reactiveJsx.childText;
      _childElement = _reactiveJsx.childElement;
      _attributeStringLiteral = _reactiveJsx.attributeStringLiteral;
      _childExpression = _reactiveJsx.childExpression;
      _children = _reactiveJsx.children;
      _attributeExpression = _reactiveJsx.attributeExpression;
      _attributes = _reactiveJsx.attributes;
      _element = _reactiveJsx.element;
    }, function (_srcClientStringsJs) {
      Strings = _srcClientStringsJs.default;
    }],
    execute: function () {
      const __SystemJSRewritingHack = {};
      _recorder_._src_client_markdown_js = _recorder_._src_client_markdown_js || {};
      Object.defineProperty(_recorder_._src_client_markdown_js, "Strings", {
        get() {
          return Strings;
        },

        set(thisIsVererySecretVariableName) {
          Strings = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      class Markdown {

        // #helper 
        static parseAndReplaceTextNode(ea, eaChild, regex, func) {
          var s = eaChild.textContent;
          var m;
          var replace = () => {
            m = regex.exec(s);
            if (m) {
              var replacement = func(m);
              if (replacement) {
                ea.insertBefore(new Text(s.slice(0, m.index)), eaChild);
                ea.insertBefore(replacement, eaChild);
                var rest = new Text(s.slice(m.index + m[0].length, s.length));
                ea.insertBefore(rest, eaChild);
                eaChild.remove();
                this.parseAndReplaceTextNode(ea, rest, regex, func // go on with the rest...
                );
              } else {
                if (regex.lastIndex) {
                  replace();
                }
              }
            }
          };
          replace();
        }

        // #helper 
        static parseAndReplace(element, regex, func) {
          element.querySelectorAll("*").forEach(ea => {
            // if (ea.localName == "code" || ea.localName == "pre") return; 
            ea.childNodes.forEach(eaChild => {
              if (eaChild instanceof Text) {
                this.parseAndReplaceTextNode(ea, eaChild, regex, func);
              }
            });
          });
        }

        static parseAndReplaceBibrefs(element) {
          this.parseAndReplace(element, /\@([A-Za-z]+[0-9][A-Z]+)/, m => {
            var link = _element("a", _attributes(_attributeExpression("click", evt => {
              evt.preventDefault();
              lively.openBrowser(link.href);
            }), _attributeExpression("href", "bib://" + m[1])), _children(_childExpression(m[1])), {
              file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
              end: {
                column: 41,
                line: 75
              },
              start: {
                column: 17,
                line: 72
              }
            });
            return link;
          });
        }

        static parseAndReplaceFigureRefs(element) {
          element.querySelectorAll("lively-drawio").forEach(ea => {
            var name = ea.getAttribute("alt");
            if (name && name !== null) {
              var titleString = ea.getAttribute("title");
              if (titleString == null) titleString = "";
              var title = _element("div", _attributes(_attributeStringLiteral("class", "figuretitle")), _children(_childElement(_element("a", _attributes(_attributeExpression("id", ea.getAttribute("alt"))), _children(), {
                file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                end: {
                  column: 80,
                  line: 87
                },
                start: {
                  column: 45,
                  line: 87
                }
              })), _childElement(_element("b", _attributes(), _children(_childText("Figure "), _childExpression(ea.getAttribute("alt")), _childText(". ")), {
                file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                end: {
                  column: 120,
                  line: 87
                },
                start: {
                  column: 80,
                  line: 87
                }
              })), _childExpression(titleString)), {
                file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                end: {
                  column: 139,
                  line: 87
                },
                start: {
                  column: 20,
                  line: 87
                }
              });
              ea.parentElement.appendChild(title);
            }
          }

          // late parse and convert latex figure references
          );this.parseAndReplace(element, /@fig:([A-Za-z0-9]+)/, m => _element("a", _attributes(_attributeExpression("href", "#" + m[1])), _children(_childExpression(m[1])), {
            file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
            end: {
              column: 94,
              line: 93
            },
            start: {
              column: 64,
              line: 93
            }
          }));
        }

        static allNodesBetween(startNode, endNode) {
          var nodes = [];
          var node = startNode;
          while (node && node != endNode) {
            if (node !== startNode) nodes.push(node);
            node = node.nextSibling;
          }

          return nodes;
        }

        static parseAndReplaceFootenotes(element) {
          var count = 1;
          var footnotes = [];
          // #TODO what about markup in that markup?: ^[*Exposé* shows an overview ]
          /*MD <browse://../Lively4DevelopmentExperience/content/Discussion.md> MD*/

          var stack = [];

          // hacky hacky hack hack hack!
          this.parseAndReplace(element, /((\^\[)|])/, m => {
            if (m[1] == "^[") {
              var footnote = { index: count++, content: "", id() {
                  return "footnote_" + this.index;
                } };
              footnotes.push(footnote);
              var link = _element("sup", _attributes(), _children(_childElement(_element("a", _attributes(_attributeExpression("href", "#" + footnote.id())), _children(_childExpression(footnote.index)), {
                file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                end: {
                  column: 74,
                  line: 120
                },
                start: {
                  column: 24,
                  line: 120
                }
              }))), {
                file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                end: {
                  column: 80,
                  line: 120
                },
                start: {
                  column: 19,
                  line: 120
                }
              });
              footnote.startNode = link;
              stack.push(footnote);
              return link;
            } else {
              var last = stack.pop();
              if (last) {
                var marker = _element("b", _attributes(), _children(_childText("XXXX")), {
                  file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                  end: {
                    column: 34,
                    line: 127
                  },
                  start: {
                    column: 23,
                    line: 127
                  }
                });
                last.endNode = marker;
                return marker;
              }
            }
          });

          footnotes.forEach(ea => {
            if (ea.startNode && ea.endNode) {
              var region = this.allNodesBetween(ea.startNode, ea.endNode);
              ea.content = _element("span", _attributes(), _children(_childSpread(region)), {
                file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
                end: {
                  column: 45,
                  line: 138
                },
                start: {
                  column: 21,
                  line: 138
                }
              }); // will get removed...
              ea.endNode.remove // ged rid of the marker
              ();
            }
          });

          if (footnotes.length > 0) {
            var footnotesDiv = _element("div", _attributes(), _children(_childElement(_element("h3", _attributes(), _children(_childText("Footnotes")), {
              file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
              end: {
                column: 48,
                line: 145
              },
              start: {
                column: 30,
                line: 145
              }
            })), _childElement(_element("ol", _attributes(), _children(_childSpread(footnotes.map(ea => _element("li", _attributes(), _children(_childElement(_element("a", _attributes(_attributeExpression("id", ea.id())), _children(), {
              file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
              end: {
                column: 100,
                line: 145
              },
              start: {
                column: 80,
                line: 145
              }
            })), _childExpression(ea.content)), {
              file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
              end: {
                column: 117,
                line: 145
              },
              start: {
                column: 76,
                line: 145
              }
            })))), {
              file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
              end: {
                column: 124,
                line: 145
              },
              start: {
                column: 48,
                line: 145
              }
            }))), {
              file: 'https://lively-kernel.org/lively4/lively4-petrinet/src/client/markdown.js',
              end: {
                column: 130,
                line: 145
              },
              start: {
                column: 25,
                line: 145
              }
            });
            element.appendChild(footnotesDiv);
          }
        }

        /*MD
        <style>* {background-color:lightgray}</style>
        ```javascript
        // we have to "quote" the @ because we are still in markdown... and replacing those references
        Markdown.extractReferences(`Hello @`+`Foo1981HHC World\nggg @`+`Bar2019X`)
        ```
        <script>
        import Markdown from 'src/client/markdown.js';
        <pre>Result:{eval(this.parentElement.querySelector("code").textContent)}</pre>
        </script>
        MD*/

        static extractReferences(source) {
          return Strings.matchAll(/@([A-Z][a-z]+[0-9][0-9][0-9][0-9][A-Z]+)/g, source).map(ea => ea[1]);
        }

        static parseAndReplaceLatex(element) {
          this.parseAndReplaceFigureRefs(element);
          this.parseAndReplaceBibrefs(element);
          this.parseAndReplaceFootenotes(element);
        }

      }

      _export("default", Markdown);

      Object.defineProperty(_recorder_._src_client_markdown_js, "Markdown", {
        get() {
          return Markdown;
        },

        set(thisIsVererySecretVariableName) {
          _export("default", Markdown = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
    }
  };
});