System.register(["reactive-jsx", "src/components/widgets/lively-morph.js", "src/client/fileindex.js", "src/client/graphics.js"], function (_export, _context) {
  "use strict";

  var _childSpread, _childElement, _attributeExpression, _attributeStringLiteral, _childExpression, _children, _attributes, _element, Morph, FileIndex, pt, LivelyGenericSearch, __SystemJSRewritingHack;
  _export("default", void 0);
  return {
    setters: [function (_reactiveJsx) {
      _childSpread = _reactiveJsx.childSpread;
      _childElement = _reactiveJsx.childElement;
      _attributeExpression = _reactiveJsx.attributeExpression;
      _attributeStringLiteral = _reactiveJsx.attributeStringLiteral;
      _childExpression = _reactiveJsx.childExpression;
      _children = _reactiveJsx.children;
      _attributes = _reactiveJsx.attributes;
      _element = _reactiveJsx.element;
    }, function (_srcComponentsWidgetsLivelyMorphJs) {
      Morph = _srcComponentsWidgetsLivelyMorphJs.default;
    }, function (_srcClientFileindexJs) {
      FileIndex = _srcClientFileindexJs.default;
    }, function (_srcClientGraphicsJs) {
      pt = _srcClientGraphicsJs.pt;
    }],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._src_components_tools_lively_generic_search_js = _recorder_._src_components_tools_lively_generic_search_js || {};
      Object.defineProperty(_recorder_._src_components_tools_lively_generic_search_js, "Morph", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return Morph;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          Morph = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_components_tools_lively_generic_search_js, "FileIndex", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return FileIndex;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          FileIndex = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_components_tools_lively_generic_search_js, "pt", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return pt;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          pt = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      _export("default", LivelyGenericSearch = class LivelyGenericSearch extends Morph {
        get input() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return this.get('#input');
        }
        get outer() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return this.get('#outer');
        }
        get inner() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return this.get('#inner');
        }
        get list() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return this.get('#list');
        }

        // #TODO: mark as meta
        async initialize() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          this.windowTitle = "LivelyGenericSearch";
          this.startSearching = (() => this.calculateListToShow()).throttle(300);
          this.setupInput();
          lively.html.registerKeys(this); // automatically installs handler for some methods

          this.addEventListener("focusout", () => this.close());
          this.addEventListener("click", evt => this.onClick(evt));
          this.init();
        }
        setupInput() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          this.input.addEventListener("keyup", evt => {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            this.onKeyInput(evt);
          });
          this.input.addEventListener('input', evt => this.inputChanged(evt));
        }
        inputChanged(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          // lively.success('input is now ${this.input.value}', evt)
          this.startSearching();
        }
        close() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          try {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            this.remove();
          } catch (e) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          } // no errors please..
        }

        onKeyInput(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          const keyActions = new Map([[13, evt => {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            this.jumpToSelectedItem(evt);
            this.close();
          }],
          // ENTER
          [27, evt => this.close()] // ESCAPE
          ]);

          keyActions.getOrCreate(evt.keyCode, keyCode => evt => {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          })(evt);
        }
        onUpDown(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          evt.stopPropagation();
          evt.preventDefault();
          const currentItem = this.get('.selected');
          if (!currentItem) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            return;
          }
          const nextItem = currentItem.previousElementSibling || this.getAllSubmorphs('.item').last;
          if (!nextItem) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            return;
          }
          currentItem.classList.remove('selected');
          nextItem.classList.add('selected');
        }
        onDownDown(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          evt.stopPropagation();
          evt.preventDefault();
          const currentItem = this.get('.selected');
          if (!currentItem) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            return;
          }
          var nextItem = currentItem.nextElementSibling || this.getAllSubmorphs('.item').first;
          if (!nextItem) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            return;
          }
          currentItem.classList.remove('selected');
          nextItem.classList.add('selected');
        }
        async jumpToSelectedItem(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          this.jumpToItem(this.get('.selected'), evt);
        }
        async jumpToItem(item, evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          if (!item) return;
          lively.openBrowser(item.getAttribute('file'), !evt.shiftKey);
          this.close();
        }
        onClick(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          var item = evt.composedPath().find(ea => ea.classList.contains("item"));
          this.jumpToItem(item, evt);
        }
        setFocus() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          this.input.focus();
        }
        init() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          lively.setPosition(this, pt(500, 100), 'fixed');
          this.files = this.getFiles();
          this.startSearching();
        }
        get searchRoots() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          if (!this._roots) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            this._roots = lively.preferences.get("ExtraSearchRoots");
          }
          return this._roots;
        }
        async getFiles() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          var result = [];
          await FileIndex.current().db.files.each(file => result.push(file));
          return result;
        }
        async calculateListToShow() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          var pattern = this.input.value;
          var search = new RegExp(pattern, 'ig');
          const filteredFiles = (await this.files).filter(file => {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            if (file.url.startsWith(lively4url)) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
              const relativePath = file.url.replace(/.*\//ig, '');
              return relativePath.match(search);
            } else {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
              var inSearchRoot = this.searchRoots.find(ea => file.url.startsWith(ea));
              if (inSearchRoot) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
                const relativePath = file.url.replace(/.*\//ig, '');
                return relativePath.match(search);
              }
              return false;
            }
          });
          const cutFiles = filteredFiles.slice(0, 50);
          this.updateList(cutFiles);
        }
        async updateList(filesToDisplay) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          this.list.innerHTML = '';
          filesToDisplay = filesToDisplay.slice(0, 50);
          filesToDisplay.forEach(file => {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            let name = file.url;
            var inSearchRoot = this.searchRoots.find(ea => file.url.startsWith(ea));
            if (inSearchRoot) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
              name = name.replace(inSearchRoot, '');
            } else {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
              name = name.replace(lively4url, '');
            }
            if (this.input.value.length >= 3) {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
              var regex = new RegExp(this.input.value, 'ig');
              var text = name;
              const realResult = [];
              var result;
              var realStart = 0;
              var counter = 0;
              while ((result = regex.exec(text)) !== null) {
                globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
                var start = result.index;
                var l = result[0].length;
                var normal = _element("span", _attributes(), _children(_childExpression(text.substring(realStart, start))), {
                  file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                  end: {
                    column: 70,
                    line: 167
                  },
                  start: {
                    column: 23,
                    line: 167
                  }
                });
                var highlight = _element("span", _attributes(_attributeStringLiteral("style", "color: red")), _children(_childExpression(result[0])), {
                  file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                  end: {
                    column: 69,
                    line: 168
                  },
                  start: {
                    column: 26,
                    line: 168
                  }
                });
                realResult.push(normal);
                realResult.push(highlight);
                realStart = start + l;
                if (counter++ > 10) {
                  globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
                  break;
                }
              }
              realResult.push(_element("span", _attributes(), _children(_childExpression(text.substring(realStart, text.length))), {
                file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                end: {
                  column: 77,
                  line: 174
                },
                start: {
                  column: 24,
                  line: 174
                }
              }));
              this.list.appendChild(_element("div", _attributes(_attributeStringLiteral("class", "item"), _attributeExpression("file", file.url)), _children(_childElement(_element("span", _attributes(), _children(_childSpread(realResult)), {
                file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                end: {
                  column: 92,
                  line: 177
                },
                start: {
                  column: 64,
                  line: 177
                }
              }))), {
                file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                end: {
                  column: 98,
                  line: 177
                },
                start: {
                  column: 30,
                  line: 177
                }
              }));
            } else {
              globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
              this.list.appendChild(_element("div", _attributes(_attributeStringLiteral("class", "item"), _attributeExpression("file", file.url)), _children(_childElement(_element("span", _attributes(), _children(_childExpression(name)), {
                file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                end: {
                  column: 83,
                  line: 179
                },
                start: {
                  column: 64,
                  line: 179
                }
              }))), {
                file: 'https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js',
                end: {
                  column: 89,
                  line: 179
                },
                start: {
                  column: 30,
                  line: 179
                }
              }));
            }
          });
          const first = this.list.querySelector('.item');
          if (first) {
            globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
            first.classList.add('selected');
          }
        }

        // this method is autmatically registered through the ``registerKeys`` method
        onKeyDown(evt) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
        } // lively.notify("Key Down: " + evt.charCode)

        livelyPreMigrate() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
        } // is called on the old object before the migration

        livelyMigrate(other) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          // whenever a component is replaced with a newer version during development
          // this method is called on the new object during migration, but before initialization
          this.input.value = other.input.value;
          this.input.focus();
        }
        livelyInspect(contentNode, inspector) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
        } // do nothing
      });
      Object.defineProperty(_recorder_._src_components_tools_lively_generic_search_js, "LivelyGenericSearch", {
        get() {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          return LivelyGenericSearch;
        },
        set(thisIsVererySecretVariableName) {
          globalThis.systemActivity["https://lively-kernel.org/lively4/lively4-ansgar/src/components/tools/lively-generic-search.js"]++;
          _export("default", LivelyGenericSearch = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
    }
  };
});