import { assert } from "../../../shared/src/asserts.js";
import { emptyArray, identity } from "../../../shared/src/sentinels.js";
import "compare-utf8";
import { throwFilterOutput } from "./filter-operators.js";
import { pushAccumulatedChanges } from "./push-accumulated.js";
class FanIn {
  #inputs;
  #schema;
  #output = throwFilterOutput;
  #accumulatedPushes = [];
  constructor(fanOut, inputs) {
    this.#inputs = inputs;
    this.#schema = fanOut.getSchema();
    for (const input of inputs) {
      input.setFilterOutput(this);
      assert(this.#schema === input.getSchema(), `Schema mismatch in fan-in`);
    }
  }
  setFilterOutput(output) {
    this.#output = output;
  }
  destroy() {
    for (const input of this.#inputs) {
      input.destroy();
    }
  }
  getSchema() {
    return this.#schema;
  }
  beginFilter() {
    this.#output.beginFilter();
  }
  endFilter() {
    this.#output.endFilter();
  }
  *filter(node) {
    return yield* this.#output.filter(node);
  }
  push(change) {
    this.#accumulatedPushes.push(change);
    return emptyArray;
  }
  *fanOutDonePushingToAllBranches(fanOutChangeType) {
    if (this.#inputs.length === 0) {
      assert(
        this.#accumulatedPushes.length === 0,
        "If there are no inputs then fan-in should not receive any pushes."
      );
      return;
    }
    yield* pushAccumulatedChanges(
      this.#accumulatedPushes,
      this.#output,
      this,
      fanOutChangeType,
      identity,
      identity
    );
  }
}
export {
  FanIn
};
//# sourceMappingURL=fan-in.js.map
