/**
  Resolves Obsidian Wikilinks [[Target|Alias]] into plain text or HTML.
 */

export function resolveWikilinks(markdown: string): string {
  // 1. Process Embed Images ![[path/to/image.png]]
  let result = markdown.replace(/!\[\[([^\]]+)\]\]/g, (_match, p1) => {
    const parts = p1.split('|');
    const imagePath = parts[0].trim();
    const altText = parts[1] ? parts[1].trim() : imagePath;
    return `![${altText}](${imagePath})`;
  });

  // 2. Process Standard Wikilinks [[Target|Alias]] -> Alias or Target
  result = result.replace(/\[\[([^\]]+)\]\]/g, (_match, p1) => {
    const parts = p1.split('|');
    if (parts.length > 1) {
      return parts[1].trim(); // Use alias
    }
    const target = parts[0].trim();
    // Return note title without path extension
    const baseName = target.split('/').pop()?.replace(/\.md$/, '') || target;
    return baseName;
  });

  return result;
}
