// In-app changelog feed.
//
// The release script (`scripts/release.mjs`) maintains a single
// `changelog.json` file alongside `latest.json` on update.netralax.cloud.
// The list is newest-first, capped at 200 entries server-side, and rewritten
// after every release.

const CHANGELOG_URL = 'https://update.netralax.cloud/windows/changelog.json';

export interface ChangelogEntry {
  version: string;
  pub_date: string;
  notes: string;
}

function isEntry(v: unknown): v is ChangelogEntry {
  if (!v || typeof v !== 'object') return false;
  const e = v as Record<string, unknown>;
  return (
    typeof e.version === 'string' &&
    typeof e.pub_date === 'string' &&
    typeof e.notes === 'string'
  );
}

export async function fetchChangelog(): Promise<ChangelogEntry[]> {
  const res = await fetch(CHANGELOG_URL, { cache: 'no-store' });
  if (!res.ok) {
    throw new Error('changelog request failed: ' + res.status);
  }
  const data: unknown = await res.json();
  if (!Array.isArray(data)) return [];
  return data.filter(isEntry);
}
