namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers;

/// <summary>
/// Pure static helper that converts S7 area + db-number + offset + data-type
/// into the canonical S7 address string (e.g. DB10.DBD20:REAL, M0.0:X).
/// Extracted so unit tests can call it without bUnit.
/// </summary>
public static class S7AddressBuilder
{
    /// <summary>Builds the canonical S7 address string for the given area, DB number, offset, and data type.</summary>
    /// <param name="area">DB / M / I / Q</param>
    /// <param name="dbNumber">Only relevant when area == "DB".</param>
    /// <param name="offset">Byte offset (decimal).</param>
    /// <param name="s7Type">X / B / W / D / REAL</param>
    /// <returns>The canonical S7 address string (e.g. <c>DB10.DBD20:REAL</c>).</returns>
    public static string Build(string area, int dbNumber, int offset, string s7Type)
    {
        if (area == "DB")
        {
            // e.g. DB10.DBD20:REAL  /  DB1.DBX0.0:X  /  DB5.DBW4:W
            var qualifier = s7Type switch
            {
                "X"    => $"DBX{offset}.0",
                "B"    => $"DBB{offset}",
                "W"    => $"DBW{offset}",
                "D"    => $"DBD{offset}",
                "REAL" => $"DBD{offset}",
                _      => $"DBD{offset}",
            };
            return $"DB{dbNumber}.{qualifier}:{s7Type}";
        }
        else
        {
            // e.g. M0.0:X  /  I4:B  /  Q2:W
            var offsetStr = s7Type == "X" ? $"{offset}.0" : $"{offset}";
            return $"{area}{offsetStr}:{s7Type}";
        }
    }
}
