import { ResponseError } from "vscode-languageclient/node";
import type { ErrorDetails } from "../types";

export function formatErrorMessage(
  error: unknown,
  context: string,
): ErrorDetails {
  let detailedMsg = context;
  let userFriendlyMsg = context;

  if (error instanceof ResponseError) {
    detailedMsg += ` Server Error (Code: ${error.code}): ${error.message}`;
    userFriendlyMsg = `${context}: ${error.message} (Server Error ${error.code})`;
  } else if (error instanceof Error) {
    detailedMsg += ` Reason: ${error.message}`;
    userFriendlyMsg = `${context}: ${error.message}`;
    if (
      error.message.includes("timeout") ||
      error.message.includes("Connection got disposed")
    ) {
      userFriendlyMsg += " Check server connection.";
    }
  } else {
    detailedMsg += ` An unknown error occurred: ${String(error)}`;
    userFriendlyMsg += " An unknown error occurred. Check logs.";
  }

  return { detailed: detailedMsg, userFriendly: userFriendlyMsg };
}
