import * as vscode from "vscode";
import * as path from "path";

export function getFileNameFromUri(uri: vscode.Uri | null): string | null {
  return uri ? path.basename(uri.fsPath) : null;
}

export function getFileNameFromUriString(
  uriString: string | null,
): string | null {
  if (!uriString) {
    return null;
  }

  try {
    const decodedUriString = decodeURIComponent(uriString);
    if (decodedUriString.startsWith("file:/")) {
      const uri = vscode.Uri.parse(decodedUriString, true);
      return path.basename(uri.fsPath);
    } else {
      return path.basename(decodedUriString);
    }
  } catch (error) {
    console.error(`Error processing URI/path string: ${uriString}`, error);
    return path.basename(decodeURIComponent(uriString));
  }
}
