// A chain of effects that connect to the effect bus
export default class EffectChain {
    constructor(context, graph, input, output) {
        this.effects = [];
        this.context = context;
        this.graph = graph;
        this.inputNode = input;
        this.outputNode = output;
        this.updateConnections();
    }
    applyEffect(effect) {
        this.effects.push(effect);
        this.updateConnections();
    }
    removeEffect(effect) {
        this.effects.forEach((currEffect) => {
            if (effect === currEffect) {
                currEffect.disconnect();
            }
        });
        this.effects = this.effects.filter((currEffect) => effect !== currEffect);
        this.updateConnections();
    }
    updateConnections() {
        if (this.effects.length == 0) {
            this.inputNode.connect(this.outputNode);
            return;
        }
        let current = null;
        let previous = null;
        this.effects.forEach((effect) => {
            current = effect;
            if (previous) {
                current.connectInput(previous.getOutput());
            }
            else {
                current.connectInput(this.inputNode);
            }
            previous = current;
        });
        if (current) {
            current.connectOutput(this.outputNode);
        }
    }
}
