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

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

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

import static java.util.Objects.requireNonNull;

/**
 * @since 7.16
 */
public class PullRequestRefPredicates {

    private static final List<String> PULL_REQUEST_REF_PREFIXES = ImmutableList.of(
            PullRequestRefBuilder.bitbucketServerDarkRef().toRef(),
            PullRequestRefBuilder.darkRef().toRef(),
            PullRequestRefBuilder.publicRef().toRef());

    public static boolean isNotPullRequestRefUpdate(@Nonnull RefUpdate refUpdate) {
        return !isPullRequestRefUpdate(refUpdate);
    }

    public static boolean isNotPullRequestRefUpdate(@Nonnull RpcProposedRefUpdate refUpdate) {
        return !isPullRequestRefUpdate(refUpdate);
    }

    public static boolean isPullRequestRef(@Nonnull String refId) {
        requireNonNull(refId, "refId");

        for (String prefix : PULL_REQUEST_REF_PREFIXES) {
            if (refId.startsWith(prefix)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isPullRequestRefUpdate(@Nonnull RefUpdate refUpdate) {
        return isPullRequestRef(requireNonNull(refUpdate, "refUpdate").getRefId());
    }

    public static boolean isPullRequestRefUpdate(@Nonnull RpcProposedRefUpdate refUpdate) {
        return isPullRequestRef(requireNonNull(refUpdate, "refUpdate").getRefId().toStringUtf8());
    }
}
