package p.studio.lsp.events;

import p.studio.utilities.events.EventSubscription;
import p.studio.utilities.events.TypedEventBus;
import p.studio.workspaces.WorkspaceId;

import java.util.Objects;
import java.util.function.Consumer;

public final class StudioWorkspaceEventBus {
    private final WorkspaceId workspaceId;
    private final StudioEventBus globalBus;
    private final TypedEventBus localBus;

    public StudioWorkspaceEventBus(WorkspaceId workspaceId, StudioEventBus globalBus) {
        this.workspaceId = Objects.requireNonNull(workspaceId, "workspaceId");
        this.globalBus = Objects.requireNonNull(globalBus, "globalBus");
        this.localBus = new TypedEventBus();
    }

    public WorkspaceId workspaceId() {
        return workspaceId;
    }

    public <T extends StudioEvent> EventSubscription subscribe(Class<T> eventType, Consumer<? super T> consumer) {
        return localBus.subscribe(eventType, consumer);
    }

    public void publish(StudioEvent event) {
        localBus.publish(event);
        globalBus.publish(event);
    }
}
