import { assert } from "../../../shared/src/asserts.js";
import { identity } from "../../../shared/src/sentinels.js";
import {} from "./data.js";
import { throwFilterOutput, } from "./filter-operators.js";
import { pushAccumulatedChanges } from "./push-accumulated.js";
/**
 * The FanIn operator merges multiple streams into one.
 * It eliminates duplicates and must be paired with a fan-out operator
 * somewhere upstream of the fan-in.
 *
 *  issue
 *    |
 * fan-out
 * /      \
 * a      b
 *  \    /
 * fan-in
 *   |
 */
export 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;
    }
    filter(node, cleanup) {
        return this.#output.filter(node, cleanup);
    }
    push(change) {
        this.#accumulatedPushes.push(change);
    }
    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;
        }
        pushAccumulatedChanges(this.#accumulatedPushes, this.#output, this, fanOutChangeType, identity, identity);
    }
}
//# sourceMappingURL=fan-in.js.map