package jetbrains.mps.vcs.core.mergedriver;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.baseLanguage.tuples.runtime.Tuples;
import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple;
import jetbrains.mps.util.FileUtil;

@GeneratedClass(node = "r:a178d3c3-970e-4352-b61c-4e55abc3bc24(jetbrains.mps.vcs.core.mergedriver)/3342666646761698548", model = "r:a178d3c3-970e-4352-b61c-4e55abc3bc24(jetbrains.mps.vcs.core.mergedriver)")
/*package*/ class SimpleMerger extends AbstractContentMerger {
  /*package*/ SimpleMerger() {
  }
  @Nullable
  @Override
  public Tuples._2<Integer, byte[]> mergeContents(FileContent baseContent, FileContent localContent, FileContent latestContent) {
    String baseAsString = contentAsString(baseContent);
    String localAsString = contentAsString(localContent);
    String latestAsString = contentAsString(latestContent);

    if (baseAsString.equals(localAsString)) {
      return MultiTuple.<Integer,byte[]>from(MERGED, latestContent.getData());
    }
    if (baseAsString.equals(latestAsString) || localAsString.equals(latestAsString)) {
      return MultiTuple.<Integer,byte[]>from(MERGED, localContent.getData());
    }
    return MultiTuple.<Integer,byte[]>from(CONFLICTS, localContent.getData());
  }
  private static String contentAsString(FileContent content) {
    return new String(content.getData(), FileUtil.DEFAULT_CHARSET).replace("\r\n", "\n");
  }
}
