using System.Text.Json.Nodes;
using Titano.Core.Project;
using Titano.Data.Sync;
namespace Titano.App.Editor;
///
/// Three-way merge of projects: the editor's edit (ours) and a change stored meanwhile by someone else
/// (theirs: an analysis filling Auto values, another client through the server), both made from basis.
/// Values are compared member by member down the JSON tree; a member only one side changed takes that side's value, a
/// member both changed takes the editor's (the user is looking at it). lists those members for
/// a source that asks the user instead (a server sequence, BRIEF §14.5).
///
public static class ProjectMerge
{
public static ProjectSettings Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs)
{
ArgumentNullException.ThrowIfNull(basis);
ArgumentNullException.ThrowIfNull(ours);
ArgumentNullException.ThrowIfNull(theirs);
return new Rebase(basis, theirs).Apply(ours);
}
///
/// The members both sides changed to different values, in document order: the path below the project
/// ("exposure.deflickerWindow.manual"), the editor's value and theirs as JSON (null when absent).
///
public static IReadOnlyList Conflicts(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs)
{
ArgumentNullException.ThrowIfNull(basis);
ArgumentNullException.ThrowIfNull(ours);
ArgumentNullException.ThrowIfNull(theirs);
var conflicts = new List();
Collect(JsonNode.Parse(basis.ToJson()), JsonNode.Parse(ours.ToJson()), JsonNode.Parse(theirs.ToJson()), string.Empty, conflicts);
return conflicts;
}
/// True when both projects store the same values.
public static bool SameValues(ProjectSettings a, ProjectSettings b) => a.ToJson() == b.ToJson();
private static JsonNode? Merge(JsonNode? basis, JsonNode? ours, JsonNode? theirs)
{
if (JsonNode.DeepEquals(ours, basis)) return theirs?.DeepClone();
if (JsonNode.DeepEquals(theirs, basis) || JsonNode.DeepEquals(theirs, ours)) return ours?.DeepClone();
if (ours is not JsonObject mine || theirs is not JsonObject other) return ours?.DeepClone();
var common = basis as JsonObject;
var merged = new JsonObject();
foreach (var name in mine.Select(p => p.Key).Union(other.Select(p => p.Key)))
{
var value = Merge(common?[name], mine[name], other[name]);
if (value is not null || mine.ContainsKey(name)) merged[name] = value;
}
return merged;
}
/// The members settles for the editor although both sides changed them.
private static void Collect(JsonNode? basis, JsonNode? ours, JsonNode? theirs, string path, List conflicts)
{
if (JsonNode.DeepEquals(ours, basis) || JsonNode.DeepEquals(theirs, basis) || JsonNode.DeepEquals(theirs, ours)) return;
if (ours is JsonObject mine && theirs is JsonObject other)
{
var common = basis as JsonObject;
foreach (var name in mine.Select(p => p.Key).Union(other.Select(p => p.Key)))
Collect(common?[name], mine[name], other[name], path.Length == 0 ? name : path + "." + name, conflicts);
return;
}
conflicts.Add(new SettingsDifference(path.Length == 0 ? "settings" : path, ours?.ToJsonString(), theirs?.ToJsonString()));
}
/// One outside change ( → ) applied to any number of projects made from the basis.
public sealed class Rebase(ProjectSettings basis, ProjectSettings theirs)
{
private readonly JsonNode? _basis = JsonNode.Parse(basis.ToJson());
private readonly JsonNode? _theirs = JsonNode.Parse(theirs.ToJson());
public ProjectSettings Apply(ProjectSettings ours)
{
ArgumentNullException.ThrowIfNull(ours);
return ProjectSettings.FromJson(Merge(_basis, JsonNode.Parse(ours.ToJson()), _theirs)?.ToJsonString() ?? "{}");
}
}
}