export type TitleFields = {
  /** YouTube video title */
  title?: string | null;
  /** On-video song / track title (art-track layouts) */
  songTitle?: string | null;
  artist?: string | null;
};

/** Resolve the title sent to YouTube, falling back to artist and song metadata. */
export function resolveYouTubeTitle(meta: TitleFields): string {
  const videoTitle = meta.title?.trim();
  if (videoTitle) return videoTitle;

  const artist = meta.artist?.trim();
  const song = meta.songTitle?.trim();
  if (artist && song) return `${artist} - ${song}`;
  if (song) return song;
  if (artist) return artist;

  return "";
}

/** Title burned into art-track layout (never the YouTube-only field alone when songTitle set). */
export function resolveBurnedSongTitle(
  meta: TitleFields,
  fallback = "Untitled",
): string {
  return meta.songTitle?.trim() || fallback;
}

export function validateYouTubeTitle(meta: TitleFields): string | null {
  const resolved = resolveYouTubeTitle(meta);
  if (!resolved) {
    return "Enter a video title, song title, or artist";
  }
  return null;
}
