package com.atlassian.bitbucket.mesh.transaction;

import com.atlassian.bitbucket.mesh.execution.ExecutionContext;
import com.atlassian.bitbucket.mesh.git.ref.RefUpdate;
import com.atlassian.bitbucket.mesh.repository.Repository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;

/**
 * Accessor for the {@link UpdateRefsContext} which is tied to the current {@link ExecutionContext}.
 */
public interface UpdateRefsContextHolder {

    /**
     * Clears the {@link UpdateRefsContext} from the current {@link ExecutionContext}, if present
     */
    void clear();

    /**
     * @return the current {@link UpdateRefsContext}; {@code null} if no {@link ExecutionContext} is active, or if no
     *         {@link UpdateRefsContext} has been {@link #set(Repository, Collection) set}
     */
    @Nullable
    UpdateRefsContext get();

    /**
     * Creates a new {@link UpdateRefsContext} and stores it in the current {@link ExecutionContext}, if one is active.
     * If another {@link UpdateRefsContext} was active, it is {@link UpdateRefsContext#close() closed} and replaced.
     *
     * @param repository the repository
     * @param refUpdates the ref updates
     */
    void set(@Nonnull Repository repository, @Nonnull Collection<RefUpdate> refUpdates);
}
