package ai.decompile.analysis.model.dto;

import ai.decompile.analysis.model.entity.StaticXref;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public record XrefResponse(
    UUID id,
    UUID callerId,
    String callerName,
    String resolvedCallerName,
    UUID calleeId,
    String calleeName,
    String resolvedCalleeName,
    String type,
    String address) {

  public static XrefResponse from(StaticXref xref) {
    return from(xref, Map.of());
  }

  public static XrefResponse from(StaticXref xref, Map<String, String> nameResolution) {
    return new XrefResponse(
        xref.getId(),
        xref.getCaller().getId(),
        xref.getCaller().getName(),
        resolve(xref.getCaller().getName(), nameResolution),
        xref.getCallee().getId(),
        xref.getCallee().getName(),
        resolve(xref.getCallee().getName(), nameResolution),
        xref.getType(),
        xref.getAddress());
  }

  private static String resolve(String name, Map<String, String> resolution) {
    if (Objects.isNull(name) || resolution.isEmpty()) {
      return null;
    }

    return resolution.get(name);
  }
}
