namespace Engine.Asset.Mesh.Loader;

public interface IMeshLoader
{
  public Mesh LoadMesh(TextReader parReader, MeshLoaderParameters parParameters = MeshLoaderParameters.Default);

  internal static Mesh Optimize(Mesh parMesh)
  {
    var optimizedMesh = new Mesh();
    var vertexMap = new Dictionary<Mesh.Vertex, uint>();
    uint index = 0;

    foreach (var vertex in parMesh.Vertices)
    {
      if (vertexMap.TryGetValue(vertex, out var existingIndex))
      {
        optimizedMesh.IndicesInternal.Add(existingIndex);
      }
      else
      {
        vertexMap.Add(vertex, index);
        optimizedMesh.VerticesInternal.Add(vertex);
        optimizedMesh.IndicesInternal.Add(index);

        index++;
      }
    }

    return optimizedMesh;
  }
}