package com.atlassian.bitbucket.mesh.git.fetch;

import com.atlassian.bitbucket.mesh.git.ref.RefUpdate;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcFetchResponseFragment;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import javax.annotation.Nonnull;
import java.util.List;

import static com.atlassian.bitbucket.mesh.rpc.util.ByteStringUtils.toStringsLazily;

public class FetchResult {

    private final ImmutableList.Builder<String> failedRefs = ImmutableList.builder();
    private final ImmutableList.Builder<RefUpdate> refUpdates = ImmutableList.builder();

    public void add(@Nonnull RpcFetchResponseFragment fragment) {
        failedRefs.addAll(toStringsLazily(fragment.getFailedRefIdsList()));
        refUpdates.addAll(Lists.transform(fragment.getRefUpdatesList(), RefUpdate::of));
    }

    @Nonnull
    public List<String> getFailedRefs() {
        return failedRefs.build();
    }

    @Nonnull
    public List<RefUpdate> getRefUpdates() {
        return refUpdates.build();
    }
}
