package p.studio.compiler.messages;

import p.studio.utilities.structures.ReadOnlyCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;

public class BuildingIssueSink implements ReadOnlyCollection<BuildingIssue> {
    private final ArrayList<BuildingIssue> issues = new ArrayList<>();
    private boolean hasErrors = false;

    protected BuildingIssueSink() {
    }

    public static BuildingIssueSink empty() {
        return new BuildingIssueSink();
    }

    @Override
    public Collection<BuildingIssue> asCollection() {
        return issues;
    }

    public BuildingIssueSink report(final Consumer<BuildingIssue.BuildingIssueBuilder> consumer) {
        final var builder = BuildingIssue.builder();
        consumer.accept(builder);
        final var issue = builder.build();
        hasErrors |= issue.isError();
        issues.add(issue);
        return this;
    }

    public BuildingIssueSink merge(final BuildingIssueSink issues) {
        hasErrors |= issues.hasErrors();
        this.issues.addAll(issues.issues);
        return this;
    }

    public boolean hasErrors() {
        return hasErrors;
    }
}
