!7

cmodule StatementFulfillments > DynObjectTable<GazelleFulfillment> {
  transient ReliableSingleThread rstSearch = dm_rst(this, r search);
  transient Flag cancel;
  
  transient GazelleContextCache_v2 contextCache;
  
  start {
    set fieldsInOrder;
    addCountToName();
    set withSearcher;
    
    print("Making cache");
    time "Made cache" {
      contextCache = new GazelleContextCache_v2(this);
      contextCache.grabRules = func -> L<T3<S>> { dm_gazelle_rulesOnFacts() };
      ownResource(contextCache.listenToMessages());
      contextCache.fill();
    }

    dm_vmBus_onMessage_q('newDiscordLine, voidfunc(O line) {
      new L<GazelleFulfillment> list;
      gazelle_gatherFulfillments(ll(dm_discord_importLine(line)), list, maxEntries := 100);
      addAll(list);
    });
    
    dm_vmBus_onMessage('gazelleRuleCreated, rstSearch); // TODO: optimize
  }
  
  visualize {
    ret withCenteredButtons(super.visualize(), "Search Chat", rstSearch);
  }
  
  void search {
    new Flag cancel;
    this.cancel = cancel;
    new L<GazelleFulfillment> list;
    
    // "john doe is a guy"
    L<GazelleLine> lines = dm_discord_allLines();
    print("Scanning " + n2(lines, "line"));
    
    gazelle_gatherFulfillments(lines, list, maxEntries := 1000, +cancel, ctx := contextCache!);
    
    print("Scan yielded " + n2(list, "entry", "entries"));
    setList(list);
  }
  
  // API
  
  L<GazelleFulfillment> entriesForRule(S ruleID) {
    ret filterWhere(list(), rule := ruleID);
  }
  
  L<GazelleFulfillment> entriesForContext(S context) {
    ret filterWhere(list(), +context);
  }
  
  void triggerSearch {
    rstSearch.trigger();
  }
}