namespace OpenNest.Data;
///
/// Moves the single legacy clamps.json (or its shared copy) onto a machine once. The legacy
/// rail is copied only while no machine has clamps of its own; after that it is never read.
///
public static class LegacyClampMigration
{
///
/// Whether (null when there is no legacy file) still has to be
/// copied: it lists at least one clamp and none of has clamps yet.
///
public static bool IsNeeded(ClampSettings? legacy, IReadOnlyCollection machines) =>
legacy is { ClampCount: > 0 } && machines.Count > 0 && machines.All(m => m.Clamps == null);
///
/// The machine that takes the legacy rail without asking: the nest defaults' machine when it
/// is on file, otherwise the only machine. Null when there are several and none is the
/// default, so the user has to choose.
///
public static MachineConfig? Target(IReadOnlyCollection machines, Guid? defaultMachineId) =>
machines.FirstOrDefault(m => m.Id == defaultMachineId)
?? (machines.Count == 1 ? machines.First() : null);
/// Gives the legacy rail, converted to the machine's units.
public static void Apply(ClampSettings legacy, MachineConfig machine)
{
ArgumentNullException.ThrowIfNull(legacy);
ArgumentNullException.ThrowIfNull(machine);
machine.Clamps = MachineClamps.FromSettings(legacy, machine.Units);
}
}