export const prerender = true;
---
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
import { getCollection } from 'astro:content';
import { getLatestRelease } from '@lib/github';
import {
  compareVersionsDesc,
  entriesForVersion,
  groupByCategory,
  isReleased,
  uniqueVersions,
} from '@lib/changelog';
import { sidebar } from '../../data/sidebar';

const showUnreleased = import.meta.env.DEV;

const releaseData = await getLatestRelease('gruntwork-io', 'terragrunt');
const latestVersion = (releaseData?.tag_name ?? 'v0.0.0').replace(/^v/, '');

const allEntries = await getCollection('changelog');

const versions = uniqueVersions(allEntries)
  .filter((version) => showUnreleased || isReleased(version, latestVersion))
  .sort(compareVersionsDesc);

const releases = versions.map((version) => {
  const entries = entriesForVersion(allEntries, version);
  return {
    version,
    groups: groupByCategory(entries),
    total: entries.length,
  };
});

const headings = [
  ...releases.map((r) => ({ depth: 2, slug: r.version, text: r.version })),
  { depth: 2, slug: 'prior-releases', text: 'Prior Releases' },
];
---

<StarlightPage
  frontmatter={{
    title: 'Changelog',
    description: 'A log of notable changes to Terragrunt.',
  }}
  headings={headings}
  sidebar={sidebar}
>
  <div data-pagefind-ignore>
    <p>This page tracks notable changes to Terragrunt. For the full list of releases and their assets, see the <a href="https://github.com/gruntwork-io/terragrunt/releases">GitHub Releases Page</a>.</p>
    <hr />
    {releases.map((release) => (
      <section class="release-summary">
        <h2 id={release.version}><a href={`/process/changelog/${release.version}`}>{release.version}</a></h2>
        <ul class="category-list">
          {release.groups.map((group) => (
            <li>
              <a href={`/process/changelog/${release.version}#${group.category.slug}`}>
                <span class="category-label">{group.category.label}</span>
                <span class="category-count">{group.entries.length}</span>
              </a>
            </li>
          ))}
        </ul>
      </section>
    ))}
    <h2 id="prior-releases">Prior Releases</h2>
    <p>For all prior releases, see the <a href="https://github.com/gruntwork-io/terragrunt/releases">GitHub Releases Page</a>.</p>
  </div>
</StarlightPage>

<style>
  .release-summary {
    margin-bottom: 2rem;
  }
  .category-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    list-style: none;
    margin: 0.5rem 0;
    padding: 0;
  }
  .category-list li {
    font-size: 0.8125rem;
  }
  .category-list a {
    align-items: center;
    border: 1px solid var(--sl-color-docs-stroke);
    border-radius: 999px;
    color: inherit;
    display: inline-flex;
    gap: 0.375rem;
    padding: 0.25rem 0.625rem;
    text-decoration: none;
  }
  .category-list a:hover,
  .category-list a:focus-visible {
    background: var(--sl-color-bg);
    border-color: var(--sl-color-accent);
  }
  .category-count {
    color: var(--sl-color-white);
    font-weight: 600;
  }
</style>
