package com.atlassian.stash.internal.pull.comment.drift;

import com.atlassian.stash.internal.pull.InternalPullRequestDiffCommentAnchor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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

/**
 * Orphans all comments without performing any real processing.
 * <p>
 * Because it immediately orphans all comments, this strategy is run with the lowest priority. It is intended
 * to serve as a catch-all for any comments which cannot be processed by any other strategy.
 *
 * @since 1.3
 */
@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class OrphanAllCommentDriftStrategy implements CommentDriftStrategy {

    /**
     * {@link DriftContext#orphan(InternalPullRequestDiffCommentAnchor) Orphans} all remaining comments.
     *
     * @param context the context containing comments to process
     */
    @Override
    public void apply(@Nonnull DriftContext context) {
        Iterator<InternalPullRequestDiffCommentAnchor> iterator = context.iterator();
        while (iterator.hasNext()) {
            context.orphan(iterator.next());
            iterator.remove();
        }
    }

    /**
     * Retrieves the name of this strategy.
     *
     * @return {@code "OrphanAll"}
     */
    @Override
    public String getName() {
        return "OrphanAll";
    }
}
